Redirect to Invidious

Redirects YouTube videos to an Invidious instance.

当前为 2023-10-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Redirect to Invidious
  3. // @author André Kugland
  4. // @description Redirects YouTube videos to an Invidious instance.
  5. // @namespace https://github.com/kugland
  6. // @license MIT
  7. // @version 0.2.1
  8. // @match *://*.youtube.com/
  9. // @match *://*.youtube.com/*
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13. "use strict";
  14. function makeUrl(videoId) {
  15. // Here you should put the URL to your Invidious instance.
  16. return `http://127.0.0.1:3000/watch?v=${videoId}`;
  17. }
  18. function getVideoId(href) {
  19. var _a;
  20. const url = new URL(href, window.location.href);
  21. if (url.pathname === '/watch') {
  22. return url.searchParams.get('v');
  23. }
  24. else {
  25. const videoId = (_a = url.pathname.match(/^\/shorts\/([a-zA-Z0-9_-]+)$/)) === null || _a === void 0 ? void 0 : _a[1];
  26. if (videoId)
  27. return videoId;
  28. }
  29. throw new Error(`Unable to parse URL: ${href}`);
  30. }
  31. // Redirect on click.
  32. document.addEventListener('click', (event) => {
  33. var _a;
  34. if (event.target instanceof HTMLElement) {
  35. try {
  36. const href = (_a = event.target.closest('a')) === null || _a === void 0 ? void 0 : _a.getAttribute('href');
  37. if (href) {
  38. event.preventDefault();
  39. event.stopPropagation();
  40. window.location.assign(makeUrl(getVideoId(href)));
  41. }
  42. }
  43. catch (e) { }
  44. }
  45. }, true);
  46. // Redirect on url change.
  47. let currentUrl = window.location.href;
  48. setInterval(() => {
  49. if (window.location.href !== currentUrl) {
  50. currentUrl = window.location.href;
  51. try {
  52. window.location.replace(makeUrl(getVideoId(currentUrl)));
  53. }
  54. catch (e) { }
  55. }
  56. }, 150);
  57. // Redirect on page load.
  58. try {
  59. window.location.replace(makeUrl(getVideoId(currentUrl)));
  60. }
  61. catch (e) { }