YouTube URL Commands

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         YouTube URL Commands
// @namespace    https://greasyfork.org/users/1201646
// @version      1.0
// @description  Change video URL to a standard YouTube video, short, or YouTube Music websites.
// @author       lemocha
// @match        https://www.youtube.com/*
// @match        https://music.youtube.com/*
// @match        https://www.youtube-nocookie.com/embed/*
// @icon         https://icons.duckduckgo.com/ip2/youtube.com.ico
// @grant        GM_registerMenuCommand
// @run-at       document-idle
// @license      MIT
// ==/UserScript==


(() =>
{
	"use strict";
	let channel = false;

	// register menu commands
	GM_registerMenuCommand("YouTube", () => reassignURL("youtube", "/watch?v=", true));
	GM_registerMenuCommand("Shorts", () => reassignURL("youtube", "/shorts/"));
	GM_registerMenuCommand("YouTube Music", () => reassignURL("music.youtube", "/watch?v=", true));

	function reassignURL(url, path, channelCheck)
	{
		const id = getID();
		if (id === undefined) { return; }

		// if viewing a youtube channel
		if (channelCheck && channel)
		{
			// format as channel URL
			path = "/channel/";
		}

		// redirect to new URL
		window.location.assign(`https://${url}.com${path}${id}`);
	}

	function getID()
	{
		// get subpage and URL data after it (e.g. /watch/, /shorts/, /channel/, etc.)
		const path = window.location.pathname;
		channel = path.includes("/channel/");


		// if video is embeded on a website
		if (window.location.hostname === "www.youtube-nocookie.com")
		{
			// trim and return video ID
			return path.split("/embed/")[1];
		}
		// if video is youtube short
		else if (path.includes("/shorts/"))
		{
			// trim and return video ID
			return path.split("/shorts/")[1];
		}
		// if channel is currently being viewed
		else if (channel)
		{
			// trim and return channel ID
			return path.split("/channel/")[1];
		}
		else
		{
			// remove from playlist, trim and return video ID
			// window.location.search is section after "watch?" in URL
			return window.location.search.split("?v=")[1].split("&list=")[0];
		}
	}
})();