Invidious (yewtu.be) embed

Replace YouTube embeds with yewtu.be embeds.

当前为 2022-03-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 *://*.youtube.com/*
  9. // @exclude *://*.google.com/*
  10. // @exclude *://turntable.fm/*
  11. // @exclude *://web.archive.org/web/*
  12. // @version 2.61
  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. } else if (invid.indexOf('&') !== -1) {
  48. params = new URLSearchParams(invid.split(invid.split('&')[0]).pop());
  49. invid = invid.split('&')[0];
  50. }
  51. params.delete('controls');
  52. if (!params.get('v')) {
  53. invid = invid.
  54. replace('www.', '').
  55. replace('youtube.com/', 'yewtu.be/').
  56. replace('youtube-nocookie.com/', 'yewtu.be/').
  57. replace('/v/', '/embed/')
  58. .replace('youtu.be/', 'yewtu.be/embed/');
  59. } else {
  60. invid = 'https://yewtu.be/embed/' + params.get('v');
  61. params.delete('v');
  62. }
  63.  
  64. if (auto !== -1) {
  65. params.set('autoplay', auto);
  66. } else if (!params.get('autoplay')) {
  67. params.set('autoplay', 0);
  68. }
  69.  
  70. if ((parseInt(frame.width, 10) <= 4 || parseInt(frame.style.width, 10) <= 4)
  71. && (parseInt(frame.height, 10) <= 4 || parseInt(frame.style.height, 10) <= 4)
  72. && params.get('autoplay') === '1') {
  73. params.set('listen', 1);
  74. }
  75.  
  76. invid += '?' + params;
  77. frame.setAttribute('src', invid);
  78. if (frame.tagName.toLowerCase() === 'embed'){
  79. let newframe = document.createElement('iframe');
  80. newframe.innerHTML = frame.innerHTML;
  81. frame.getAttributeNames().forEach(attrName => { newframe.setAttribute(attrName, frame.getAttribute(attrName)); });
  82. frame.parentNode.replaceChild(newframe, frame);
  83. }
  84. }
  85. }
  86.  
  87. function youtubeiFrame(el) {
  88. return (el.getAttribute('src').indexOf('youtube.com/') !== -1 || el.getAttribute('src').indexOf('youtube-nocookie.com/') !== -1 || el.getAttribute('src').indexOf('youtu.be/') !== -1);
  89. }
  90.  
  91. go(a);