YouTube List Stripper

Open YouTube videos from a playlist in a new tab without the playlist attributes

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         YouTube List Stripper
// @name:ja      YouTubeでプレイリストを外して開くやつ
// @namespace    https://greasyfork.org/ja/users/1492018-sino087
// @version      1.1.1
// @description  Open YouTube videos from a playlist in a new tab without the playlist attributes
// @description:ja プレイリストから動画を開くときに、プレイリストの属性を外して新しいタブで開きます
// @author       sino
// @license      MIT
// @icon         https://www.youtube.com/s/desktop/db7db827/img/favicon.ico
// @match        *://www.youtube.com/*
// @noframes
// @grant        GM_openInTab
// @run-at       document-start
// ==/UserScript==

(function () {
    'use strict';

    function getCleanUrl(url) {
        try {
            const urlObj = new URL(url);
            if (urlObj.pathname === '/watch' && urlObj.searchParams.has('list')) {
                urlObj.searchParams.delete('list');
                urlObj.searchParams.delete('index');
                return urlObj.toString();
            }
        } catch (e) {
            console.error('YouTube List Stripper: Invalid URL', e);
        }
        return null;
    }

    function handleClick(event) {
        const pathname = window.location.pathname;
        const search = window.location.search;
        const isPlaylist = pathname === '/playlist' && search.includes('list=');
        const isWatchList = pathname === '/watch' && search.includes('list=');

        if (!isPlaylist && !isWatchList) return;

        let link = event.target.closest('a');
        if (!link || !link.href) {
            const contentDiv = event.target.closest('ytd-playlist-video-renderer > div#content');
            if (contentDiv) {
                link = contentDiv.querySelector('a#video-title');
            }
            if (!link || !link.href) return;
        }
        if (link.classList.contains('yt-spec-button-shape-next')) {
            return;
        }

        if (event.button !== 0 || event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) {
            return;
        }

        const cleanUrl = getCleanUrl(link.href);
        if (cleanUrl) {
            event.preventDefault();
            event.stopPropagation();
            event.stopImmediatePropagation();

            if (isWatchList) {
                const video = document.querySelector('video');
                if (video) {
                    video.pause();
                }
            }

            window.open(cleanUrl, '_blank');
        }
    }

    document.addEventListener('click', handleClick, true);

})();