Redirect YouTube Shorts to Watch

Redirect /shorts to /watch

  1. // ==UserScript==
  2. // @name Redirect YouTube Shorts to Watch
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.3
  5. // @description Redirect /shorts to /watch
  6. // @author CY Fung
  7. // @license MIT
  8. // @run-at document-start
  9. // @match https://*.youtube.com/*
  10. // @match http://*.youtube.com/*
  11. // @match https://youtube.com/*
  12. // @match http://youtube.com/*
  13. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  14. // @grant none
  15. // @unwrap
  16. // @noframes
  17. // @inject-into page
  18. // ==/UserScript==
  19.  
  20. (function () {
  21. 'use strict';
  22.  
  23. let lastPathname = '';
  24. let lastId = '';
  25.  
  26. let lastRedirection = '';
  27. let lastRedirectionCache = sessionStorage.getItem('v5Nmolo6');
  28. if (typeof lastRedirectionCache === 'string') {
  29. let s = lastRedirectionCache.split('|');
  30. if (s.length === 2) {
  31. let lastRedirectionTime = +s[1];
  32. if (Date.now() - lastRedirectionTime < 2300) {
  33. lastRedirection = s[0];
  34. }
  35. }
  36. }
  37.  
  38. /**
  39. *
  40. * @param {Event?} evt
  41. */
  42. function checkRedirect(evt) {
  43. let pathname = location.pathname;
  44. if (lastPathname !== pathname) {
  45. let id = '';
  46. if (pathname && pathname.startsWith('/shorts')) {
  47. let m = /\/shorts\/([\w\-\_\+\=]+)/.exec(pathname)
  48. if (m) {
  49. id = m[1];
  50. }
  51. }
  52. lastPathname = pathname;
  53. lastId = id;
  54. }
  55. let id = lastId;
  56. if (id) {
  57. if (evt) {
  58. evt.stopPropagation();
  59. evt.stopImmediatePropagation();
  60. }
  61. if (lastRedirection !== id) {
  62. lastRedirection = id;
  63. sessionStorage.setItem('v5Nmolo6', id + '|' + Date.now());
  64. location.replace('/watch?v=' + id);
  65. }
  66. }
  67. }
  68.  
  69. for (const s of ['yt-navigate', 'yt-navigate-start', 'yt-page-data-fetched', 'yt-page-data-updated', 'yt-navigate-finish']) {
  70. document.addEventListener(s, checkRedirect, true);
  71. }
  72. checkRedirect();
  73.  
  74.  
  75. // Your code here...
  76. })();