Invidious (yewtu.be) embed

Replace YouTube embeds with yewtu.be embeds.

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

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