Youtube to Invidious

Scan page for youtube embeds and urls and replace with Invidious.

当前为 2018-12-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube to Invidious
  3. // @namespace Krul & Brood
  4. // @description Scan page for youtube embeds and urls and replace with Invidious.
  5. // @include *
  6. // @exclude http*://youtube.*
  7. // @exclude http*://invidio.us*
  8. // @exclude http*://www.youtube.*
  9. // @exclude http*://www.invidio.us*
  10. // @version 3.0
  11. // ==/UserScript==
  12.  
  13. var a=0; //set to 1 to autoplay embedded videos present on initial page load (0 recommended)
  14. var b=0; //set to 1 to autoplay embedded videos that appear on page interaction
  15. var c=1; //set to 1 to replace all youtube hyperlinks to invidious
  16.  
  17. var observer=new MutationObserver(function(mutations){
  18. mutations.forEach(function(mutation){
  19. embed(b);
  20. });
  21. });
  22. observer.observe(document.body,{childList:true,subtree:true,});
  23. embed(a);
  24. if(c==1){link();}
  25.  
  26. function embed(auto){
  27. var filter=Array.filter||Benchmark.filter;
  28. var frames=filter(document.getElementsByTagName('iframe'),youtube);
  29. for(var i=0;i<frames.length;i++){
  30. var src=frames[i].getAttribute('src')
  31. .replace('youtube.com/','invidio.us/')
  32. .replace('youtu.be/','invidio.us/')
  33. .replace('-nocookie','')
  34. .replace('autoplay=','');
  35. if(src.indexOf('?')===-1){
  36. src+='?autoplay='+auto;
  37. }else{
  38. src+='&autoplay='+auto;
  39. }
  40. frames[i].setAttribute('src',src);
  41. }
  42. }
  43.  
  44. function link(){
  45. if(c==1){
  46. var filter=Array.filter||Benchmark.filter;
  47. var urls=filter(document.getElementsByTagName('a'),youtube);
  48. for(var i=0;i<urls.length;i++){
  49. var href=urls[i].getAttribute('href')
  50. .replace('youtube.com/','invidio.us/')
  51. .replace('youtu.be/','invidio.us/')
  52. .replace('-nocookie','');
  53. urls[i].setAttribute('href',href);
  54. }
  55. }
  56. }
  57.  
  58. function youtube(el){
  59. if(el.hasAttribute('src')){
  60. return el.getAttribute('src')
  61. .indexOf('youtu')!==-1;
  62. }else if(el.hasAttribute('href')){
  63. return el.getAttribute('href')
  64. .indexOf('youtu')!==-1;
  65. }
  66. return false;
  67. }