Allow full screen on embedded Youtube

ViolentMonkey script

当前为 2021-03-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Allow full screen on embedded Youtube
  3. // @namespace mwisnicki@gmail.com
  4. // @homepage https://github.com/mwisnicki/userscripts/blob/master/allowfullscreen-youtube-embed.user.js
  5. // @match *://*/*
  6. // @grant none
  7. // @version 4
  8. // @author mwisnicki@gmail.com
  9. // @description ViolentMonkey script
  10. // ==/UserScript==
  11.  
  12. const URLs = [
  13. "https://www.youtube.com/embed/",
  14. "https://youtube.com/embed/",
  15. "https://www.youtube-nocookie.com/embed/"
  16. ];
  17.  
  18. const SELECTOR = URLs.map(url => `iframe[src^="${url}"]:not([allowfullscreen])`).join(', ');
  19.  
  20. function forceReloadIframe(iframe) {
  21. // force reload
  22. // TODO maybe there's a way to refresh state without reload?
  23. const span = document.createElement("span");
  24. iframe.replaceWith(span);
  25. span.replaceWith(iframe);
  26. }
  27.  
  28. function fixVideos() {
  29. const iframes = document.body.querySelectorAll(SELECTOR)
  30. for (const iframe of iframes) {
  31. iframe.setAttribute("allowfullscreen","");
  32. forceReloadIframe(iframe);
  33. console.log("Forced Youtube allowfullscreen on %o", iframe);
  34. }
  35. }
  36.  
  37. fixVideos();
  38.  
  39. new MutationObserver(fixVideos).observe(document, { childList: true, subtree: true });