Invidious (yewtu.be) embed

Replace YouTube embeds with yewtu.be embeds.

当前为 2022-01-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Invidious (yewtu.be) embed
  3. // @description Replace YouTube embeds with yewtu.be embeds.
  4. // @author Backend & SkauOfArcadia
  5. // @homepage https://skau.neocities.org/
  6. // @contactURL https://t.me/SkauOfArcadia
  7. // @include *
  8. // @exclude *://www.youtube.com/*
  9. // @exclude *://*.google.com/*
  10. // @exclude *://web.archive.org/web/*
  11. // @version 2.56
  12. // @namespace https://greasyfork.org/users/751327
  13. // ==/UserScript==
  14. var a = -1; //defines autoplay value on the initial page load
  15. var b = -1; //defines autoplay for embedded videos that appear on page interaction
  16. // -1 = will only autoplay if the embed has the "autoplay=1" parameter
  17. // 0 = disables autoplay
  18. // 1 = enables autoplay
  19.  
  20. var observer = new MutationObserver(mutate);
  21. observer.observe(document, {
  22. childList: true,
  23. attributes: true,
  24. subtree: true
  25. });
  26.  
  27. function mutate() {
  28. go(b);
  29. }
  30.  
  31. function go(auto) {
  32. var frames = document.getElementsByTagName("iframe");
  33. frames = Object.values(frames).filter(youtubeiFrame);
  34.  
  35. for (var i = 0; i < frames.length; i++) {
  36. var frame = frames[i];
  37. var src = frame.getAttribute('src');
  38. if (window.location.hostname.indexOf('duckduckgo.com') !== -1 && src.indexOf('/video_frame?url=') === 0) {
  39. src = decodeURIComponent(src.replace('/video_frame?url=', ''));
  40. }
  41. var invid = src.
  42. replace('www.', '').
  43. replace('youtube.com', 'yewtu.be').
  44. replace('youtube-nocookie.com', 'yewtu.be')
  45. .replace('youtu.be/', 'yewtu.be/embed/');
  46.  
  47. let params = new URLSearchParams();
  48. if (invid.indexOf('?') !== -1) {
  49. params = new URLSearchParams(invid.split('?').pop());
  50. invid = invid.split('?')[0];
  51. params.delete('controls');
  52. }
  53.  
  54. if (auto !== -1) {
  55. params.set('autoplay', auto);
  56. } else if (!params.get('autoplay')) {
  57. params.set('autoplay', 0);
  58. }
  59.  
  60. if ((frame.width === '0' || frame.width === '1' || frame.style.width === '0px' || frame.style.width === '1px')
  61. && (frame.height === '0' || frame.height === '1' || frame.style.height === '0px' || frame.style.height === '1px')
  62. && params.get('autoplay') === '1') {
  63. params.set('listen', 1);
  64. }
  65.  
  66. invid += '?' + params;
  67. frame.setAttribute('src', invid);
  68. }
  69. }
  70.  
  71. function youtubeiFrame(el) {
  72. if (el.hasAttribute('src')) {
  73. return (el.getAttribute('src').indexOf('youtube') !== -1 || el.getAttribute('src').indexOf('youtu.be') !== -1);
  74. }
  75. return false;
  76. }
  77.  
  78. go(a);