YouTube URL Converter

Convert YouTube Shorts URL to regular YouTube video URL.

Versione datata 18/10/2023. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name                YouTube URL Converter
// @name:zh-TW          YouTube URL 轉換器
// @name:ja             YouTube URL コンバーター
// @name:zh-CN          YouTube URL 转换器
// @namespace           http://tampermonkey.net/
// @version             1.0
// @description         Convert YouTube Shorts URL to regular YouTube video URL.
// @description:zh-TW   將 YouTube Shorts 網址轉換為常規的 YouTube 影片網址。
// @description:ja      YouTube Shorts URLを通常のYouTubeビデオURLに変換します。
// @description:zh-CN   将 YouTube Shorts 网址转换为常规的 YouTube 视频网址。
// @author              鮪魚大師
// @match               https://www.youtube.com/*
// @grant               none
// @license             MIT
// ==/UserScript==

(function() {
    'use strict';

    let convertButton;

    function createConvertButton() {
        if (!convertButton) {
            convertButton = document.createElement('button');
            convertButton.textContent = 'Shorts網址轉換';
            convertButton.style.position = 'fixed';
            convertButton.style.top = '150px';
            convertButton.style.right = '10px';
            convertButton.style.zIndex = '9999';
            convertButton.style.backgroundColor = '#FF0000';
            convertButton.style.color = '#FFFFFF';
            convertButton.style.fontSize = '24px';
            convertButton.style.padding = '10px 20px';
            convertButton.style.border = 'none';
            convertButton.style.borderRadius = '5px';
            convertButton.title = '將Shorts網址轉換成一般影片網址';
            document.body.appendChild(convertButton);

            convertButton.addEventListener('click', convertURL);
        }
    }

    function convertURL() {
        const currentURL = window.location.href;
        if (currentURL.includes("youtube.com/shorts/")) {
            const match = currentURL.match(/https:\/\/www\.youtube\.com\/shorts\/([A-Za-z0-9_-]+)/);
            if (match) {
                const videoID = match[1];
                const videoURL = `https://www.youtube.com/watch?v=${videoID}`;
                window.location.href = videoURL;
            }
        }
    }

    function removeConvertButton() {
        if (convertButton) {
            convertButton.remove();
            convertButton = null;
        }
    }

    function checkAndCreateButton() {
        if (window.location.href.includes("youtube.com/shorts/")) {
            createConvertButton();
        } else {
            removeConvertButton();
        }
    }

    checkAndCreateButton();

    window.addEventListener('popstate', checkAndCreateButton);

    const observer = new MutationObserver(function(mutationsList, observer) {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList') {
                checkAndCreateButton();
            }
        }
    });

    observer.observe(document.documentElement, { childList: true, subtree: true });
})();