Greasy Fork is available in English.

YouTube URL Commands

Change video URL to a standard YouTube video, short, or YouTube Music websites.

  1. // ==UserScript==
  2. // @name YouTube URL Commands
  3. // @namespace https://greatest.deepsurf.us/users/1201646
  4. // @version 1.0
  5. // @description Change video URL to a standard YouTube video, short, or YouTube Music websites.
  6. // @author lemocha
  7. // @match https://www.youtube.com/*
  8. // @match https://music.youtube.com/*
  9. // @match https://www.youtube-nocookie.com/embed/*
  10. // @icon https://icons.duckduckgo.com/ip2/youtube.com.ico
  11. // @grant GM_registerMenuCommand
  12. // @run-at document-idle
  13. // @license MIT
  14. // ==/UserScript==
  15.  
  16.  
  17. (() =>
  18. {
  19. "use strict";
  20. let channel = false;
  21.  
  22. // register menu commands
  23. GM_registerMenuCommand("YouTube", () => reassignURL("youtube", "/watch?v=", true));
  24. GM_registerMenuCommand("Shorts", () => reassignURL("youtube", "/shorts/"));
  25. GM_registerMenuCommand("YouTube Music", () => reassignURL("music.youtube", "/watch?v=", true));
  26.  
  27. function reassignURL(url, path, channelCheck)
  28. {
  29. const id = getID();
  30. if (id === undefined) { return; }
  31.  
  32. // if viewing a youtube channel
  33. if (channelCheck && channel)
  34. {
  35. // format as channel URL
  36. path = "/channel/";
  37. }
  38.  
  39. // redirect to new URL
  40. window.location.assign(`https://${url}.com${path}${id}`);
  41. }
  42.  
  43. function getID()
  44. {
  45. // get subpage and URL data after it (e.g. /watch/, /shorts/, /channel/, etc.)
  46. const path = window.location.pathname;
  47. channel = path.includes("/channel/");
  48.  
  49.  
  50. // if video is embeded on a website
  51. if (window.location.hostname === "www.youtube-nocookie.com")
  52. {
  53. // trim and return video ID
  54. return path.split("/embed/")[1];
  55. }
  56. // if video is youtube short
  57. else if (path.includes("/shorts/"))
  58. {
  59. // trim and return video ID
  60. return path.split("/shorts/")[1];
  61. }
  62. // if channel is currently being viewed
  63. else if (channel)
  64. {
  65. // trim and return channel ID
  66. return path.split("/channel/")[1];
  67. }
  68. else
  69. {
  70. // remove from playlist, trim and return video ID
  71. // window.location.search is section after "watch?" in URL
  72. return window.location.search.split("?v=")[1].split("&list=")[0];
  73. }
  74. }
  75. })();