YT2Invidio

Point YouTube links to Invidio -- and Twitter links to Nitter

目前为 2019-10-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name YT2Invidio
  3. // @namespace de.izzysoft
  4. // @author Izzy
  5. // @description Point YouTube links to Invidio -- and Twitter links to Nitter
  6. // @license CC BY-NC-SA
  7. // @include *
  8. // @version 1.2.0
  9. // @run-at document-idle
  10. // @grant GM.getValue
  11. // @grant GM.setValue
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_registerMenuCommand
  15. // @grant unsafeWindow
  16. // @homepageURL https://codeberg.org/izzy/userscripts
  17. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  18. // ==/UserScript==
  19.  
  20. // Get the Invidious instance to use for rewrite
  21. GM.getValue('InvidiousHost','invidiou.sh').then(function(result) {
  22. rewriteLinks(result);
  23. });
  24.  
  25.  
  26. // Do the actual rewrite
  27. function rewriteLinks(videohost) {
  28. console.log(`Using '${videohost}' for rewrite`);
  29.  
  30. for(var i = 0; i < document.links.length; i++) {
  31. var elem = document.links[i];
  32.  
  33. // Youtube: https://www.youtube.com/watch?v=cRRA2xRRgl8 || https://www.youtube.com/channel/dfqwfhqQ34er
  34. // only rewrite if we're not on Invidious already (too keep the "watch this on YouTube" links intact)
  35. if (elem.href.match(/(www\.)?youtube.com(\/watch\?v=[a-z0-9_-]+)/i) || elem.href.match(/(www\.)?youtube.com(\/channel\/[a-z0-9_-]+)/i)) {
  36. if (location.hostname != videohost) { elem.href='https://'+videohost+RegExp.$2; }
  37. } else if (elem.href.match(/(www\.)?youtu.be\/([a-z0-9_-]+)/i)) {
  38. if (location.hostname != videohost) { elem.href='https://'+videohost+'/watch?v='+RegExp.$2; }
  39.  
  40. // Twitter
  41. } else if (elem.href.match(/(mobile\.)?twitter\.com\/([^&#]+)/i)) {
  42. elem.href='https://nitter.net/'+RegExp.$2;
  43. }
  44. }
  45. }
  46.  
  47.  
  48. // Give the user the possibility to set a different preferred instance
  49. // A list of available instances can be found at
  50. // https://github.com/omarroth/invidious/wiki/Invidious-Instances
  51. async function setInstance() {
  52. let vhost = await GM.getValue('InvidiousHost','invidiou.sh');
  53. vhost = prompt('Set Invidious instance to:', vhost);
  54. if (vhost != null) GM.setValue('InvidiousHost',vhost);
  55. }
  56.  
  57. GM_registerMenuCommand('Set Invidious instance',setInstance);