Suppress YouTube Playlist links

When opening a video from a playlist, strip out the parameters that make it open in a playlist UI

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Suppress YouTube Playlist links
// @namespace    http://tampermonkey.net/
// @description  When opening a video from a playlist, strip out the parameters that make it open in a playlist UI
// @license MIT
// @author       Igor Makarov
// @match        *://*.youtube.com/playlist*
// @match        *://*.youtube.com/feed/history*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @version 0.0.1.20241227113632
// ==/UserScript==

var hrefsCount = 0;
function modifyLinks() {
    let hrefs = document.querySelectorAll('a');
    if (hrefs.length == hrefsCount) {
        return;
    }
    hrefsCount = hrefs.length;
    hrefs.forEach(link => {
        let href = link.href
        if (!href) {
            return;
        }
        // console.log(`href: ${href}`);
        let url = new URL(href, document.location);
        if (url.searchParams && url.searchParams.has('list')) {
            url.searchParams.delete('list');
        }
        if (url.searchParams && url.searchParams.has('index')) {
            url.searchParams.delete('index');
        }
        if (url.searchParams && url.searchParams.has('pp')) {
            url.searchParams.delete('pp');
        }
        link.href = url
        // console.log(`url: ${url}`);
    });
}

document.addEventListener('DOMContentLoaded', modifyLinks)
let observer = new MutationObserver(e => { modifyLinks() })
observer.observe(document, {
    subtree: true,
    //attributes: true,
    //characterData: true,
    childList: true
})