YouTube Short Link Copier

Copy the short YouTube link directly when pressing Shift + S.

目前為 2023-09-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Short Link Copier
// @namespace    https://greasyfork.org/en/scripts/474134-youtube-short-link-copier
// @version      3.0
// @description  Copy the short YouTube link directly when pressing Shift + S.
// @author       You
// @match        https://www.youtube.com/*
// @match        https://music.youtube.com/*
// @grant        GM_setClipboard
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('keydown', function(e) {
        // Check if the pressed key is 'S' and Shift is also pressed
        if (e.key === 'S' && e.shiftKey) {
            let shortURL = '';

            // YouTube
            if (window.location.host === 'www.youtube.com' && !window.location.pathname.startsWith('/shorts/')) {
                const videoID = window.location.search.split('v=')[1]?.split('&')[0];
                if (videoID) {
                    shortURL = `https://youtu.be/${videoID}`;
                }
            }

            // YouTube Shorts
            else if (window.location.host === 'www.youtube.com' && window.location.pathname.startsWith('/shorts/')) {
                shortURL = window.location.href;
                }

            // YouTube Music
            else if (window.location.host === 'music.youtube.com') {
                const videoID = window.location.search.split('v=')[1]?.split('&')[0];
                if (videoID) {
                    shortURL = `https://music.youtube.com/watch?v=${videoID}`;
                }
            }

            // If a valid short URL is formed, copy it to the clipboard
            if (shortURL) {
                GM_setClipboard(shortURL);
            }
        }
    });
})();