Invidious (yewtu.be) embed

Replace YouTube embeds with yewtu.be embeds.

当前为 2022-04-03 提交的版本,查看 最新版本

  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. // @inject-into content
  13. // @version 2.62
  14. // @namespace https://greasyfork.org/users/751327
  15. // ==/UserScript==
  16. const a = -1; //defines autoplay value on the initial page load
  17. const b = -1; //defines autoplay for embedded videos that appear on page interaction
  18. // -1 = will only autoplay if the embed has the "autoplay=1" parameter
  19. // 0 = disables autoplay
  20. // 1 = enables autoplay
  21. const dash = false; //defines if the quality=dash parameter will be used
  22.  
  23. var observer = new MutationObserver(mutate);
  24. observer.observe(document, {
  25. childList: true,
  26. attributes: true,
  27. subtree: true
  28. });
  29.  
  30. function mutate() {
  31. go(b);
  32. }
  33.  
  34. function go(auto) {
  35. var frames = document.querySelectorAll('iframe[src], embed[src]');
  36. frames = Object.values(frames).filter(youtubeiFrame);
  37.  
  38. for (let i = 0; i < frames.length; i++) {
  39. let frame = frames[i];
  40. let invid = frame.getAttribute('src');
  41. if (location.hostname.indexOf('duckduckgo.com') !== -1 && invid.indexOf('/video_frame?url=') === 0) {
  42. invid = decodeURIComponent(src.replace('/video_frame?url=', ''));
  43. }
  44.  
  45. let params = new URLSearchParams();
  46. if (invid.indexOf('?') !== -1) {
  47. params = new URLSearchParams(invid.split('?').pop());
  48. invid = invid.split('?')[0];
  49. } else if (invid.indexOf('&') !== -1) {
  50. params = new URLSearchParams(invid.split(invid.split('&')[0]).pop());
  51. invid = invid.split('&')[0];
  52. }
  53. params.delete('controls');
  54. if (!params.get('v')) {
  55. invid = invid.
  56. replace('www.', '').
  57. replace('youtube.com/', 'yewtu.be/').
  58. replace('youtube-nocookie.com/', 'yewtu.be/').
  59. replace('/v/', '/embed/')
  60. .replace('youtu.be/', 'yewtu.be/embed/');
  61. } else {
  62. invid = 'https://yewtu.be/embed/' + params.get('v');
  63. params.delete('v');
  64. }
  65.  
  66. if (auto !== -1) {
  67. params.set('autoplay', auto);
  68. } else if (!params.get('autoplay')) {
  69. params.set('autoplay', 0);
  70. }
  71.  
  72. if ((parseInt(frame.width, 10) <= 4 || parseInt(frame.style.width, 10) <= 4)
  73. && (parseInt(frame.height, 10) <= 4 || parseInt(frame.style.height, 10) <= 4)
  74. && params.get('autoplay') === '1') {
  75. params.set('listen', 1);
  76. }
  77. if(dash){ params.set('quality', 'dash'); }
  78.  
  79. invid += '?' + params;
  80. frame.setAttribute('src', invid);
  81. if (frame.tagName.toLowerCase() === 'embed'){
  82. let newframe = document.createElement('iframe');
  83. newframe.innerHTML = frame.innerHTML;
  84. frame.getAttributeNames().forEach(attrName => { newframe.setAttribute(attrName, frame.getAttribute(attrName)); });
  85. frame.parentNode.replaceChild(newframe, frame);
  86. }
  87. }
  88. }
  89.  
  90. function youtubeiFrame(el) {
  91. return (el.getAttribute('src').indexOf('youtube.com/') !== -1 || el.getAttribute('src').indexOf('youtube-nocookie.com/') !== -1 || el.getAttribute('src').indexOf('youtu.be/') !== -1);
  92. }
  93.  
  94. go(a);