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 List Stripper
// @namespace    https://greasyfork.org/ja/users/1492018-sino087
// @version      1.1.0
// @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;

        const link = event.target.closest('a');
        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);

})();