Invidious (yewtu.be) embed

Replace YouTube embeds with yewtu.be embeds.

当前为 2022-09-10 提交的版本,查看 最新版本

  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 *://*.youtube.com/*
  9. // @exclude *://*.google.com/*
  10. // @exclude *://turntable.fm/*
  11. // @exclude *://web.archive.org/web/*
  12. // @exclude *://*/embed/*
  13. // @exclude *://*/watch?v=*
  14. // @inject-into content
  15. // @version 2022.09-1
  16. // @grant none
  17. // @allFrames true
  18. // @namespace https://greasyfork.org/users/751327
  19. // ==/UserScript==
  20. (function() {
  21. "use strict";
  22. const instance = "yewtu.be";
  23. const a = -1; //defines autoplay value on the initial page load
  24. const b = -1; //defines autoplay for embedded videos that appear on page interaction
  25. // -1 = will only autoplay if the embed has the "autoplay=1" parameter
  26. // 0 = disables autoplay
  27. // 1 = enables autoplay
  28. const dash = true; //defines if the quality=dash parameter will be used
  29.  
  30. var observer = new MutationObserver(mutate);
  31. observer.observe(document, {
  32. childList: true,
  33. attributes: true,
  34. subtree: true
  35. });
  36.  
  37. function mutate() {
  38. go(b);
  39. }
  40.  
  41. function go(auto) {
  42. let frames = document.body.querySelectorAll('iframe[src], embed[src]');
  43. frames = Object.values(frames).filter(youtubeiFrame);
  44.  
  45. for (let i = 0; i < frames.length; i++) {
  46. let frame = frames[i];
  47. let invid = frame.src;
  48. let params = new URLSearchParams();
  49. if (invid.indexOf('?') !== -1) {
  50. params = new URLSearchParams(invid.substring(invid.indexOf('?') + 1));
  51. invid = invid.split('?')[0];
  52. } else if (invid.indexOf('&') !== -1) {
  53. params = new URLSearchParams(invid.substring(invid.indexOf('&') + 1));
  54. invid = invid.split('&')[0];
  55. }
  56. params.delete('controls');
  57. if (params.get('v'))
  58. {
  59. invid = 'https://' + instance + '/embed/' + params.get('v');
  60. params.delete('v');
  61. }
  62. else if (invid.toLowerCase().indexOf('/embed/') !== -1)
  63. {
  64. invid = 'https://' + instance + '/embed/' + invid.substring(invid.toLowerCase().indexOf('/embed/') + 7).split('/')[0];
  65. }
  66. else if (invid.toLowerCase().indexOf('/v/') !== -1)
  67. {
  68. invid = 'https://' + instance + '/embed/' + invid.substring(invid.toLowerCase().indexOf('/v/') + 3).split('/')[0];
  69. }
  70. else
  71. {
  72. invid = 'https://' + instance + '/embed/' + invid.split('/')[3];
  73. }
  74.  
  75. if (auto !== -1) {
  76. params.set('autoplay', auto);
  77. } else if (!params.get('autoplay')) {
  78. params.set('autoplay', 0);
  79. }
  80.  
  81. if ((parseInt(frame.width, 10) <= 4 || parseInt(frame.style.width, 10) <= 4)
  82. && (parseInt(frame.height, 10) <= 4 || parseInt(frame.style.height, 10) <= 4)
  83. && params.get('autoplay') === '1') {
  84. params.set('listen', 1);
  85. }
  86.  
  87. if(dash){ params.set('quality', 'dash'); }
  88.  
  89. invid += '?' + params;
  90. frame.setAttribute('src', invid);
  91. if (frame.hasAttribute('srcdoc')) { frame.removeAttribute('srcdoc'); }
  92. if (frame.tagName.toLowerCase() === 'embed'){
  93. let newframe = document.createElement('iframe');
  94. newframe.innerHTML = frame.innerHTML;
  95. frame.getAttributeNames().forEach(attrName => { newframe.setAttribute(attrName, frame.getAttribute(attrName)); });
  96. frame.parentNode.replaceChild(newframe, frame);
  97. }
  98. }
  99. //Replace thumbnail embeds
  100. let thumbs = document.body.querySelectorAll('img[src*="img.youtube.com"], img[src*="i.ytimg.com"]');
  101. for (let x = 0; x < thumbs.length; x++)
  102. {
  103. let thumb = thumbs[x];
  104. let thumbsrc = new URL(thumb.src);
  105. if (thumbsrc.hostname === "img.youtube.com" || thumbsrc.hostname === "i.ytimg.com")
  106. {
  107. if (thumb.hasAttribute('srcset') && thumb.srcset.indexOf(thumbsrc) !== -1)
  108. {
  109. thumb.setAttribute('srcset', thumb.srcset.replace(thumbsrc, thumbsrc.protocol + '//' + instance + thumbsrc.pathname + thumbsrc.search + thumbsrc.hash));
  110. }
  111. thumb.setAttribute('src', thumbsrc.protocol + '//' + instance + thumbsrc.pathname + thumbsrc.search + thumbsrc.hash);
  112. }
  113. }
  114. }
  115.  
  116. function youtubeiFrame(el) {
  117. try {
  118. let url = new URL(el.src);
  119. return (url.hostname === "youtube.com" || url.hostname.endsWith(".youtube.com")
  120. || url.hostname === "youtube-nocookie.com" || url.hostname.endsWith(".youtube-nocookie.com")
  121. || url.hostname === "youtu.be" || url.hostname.endsWith(".youtu.be"));
  122. } catch (_) {
  123. return false;
  124. }
  125. }
  126.  
  127. go(a);
  128. })();