Youtube share url si parameter remover.

Script for removing si parameter from share url.

< Feedback on Youtube share url si parameter remover.

Question/comment

§
Posted: 10 Agustus 2024

I fixed a new problem again. It now doesn't break when you spam the share button to update the URL. Here's the literal code written by ChatGPT.

// ==UserScript==
// @name         Youtube share url si parameter remover.
// @namespace    http://tampermonkey.net/
// @version      0.6.userFeedback
// @description  Script for removing si parameter from share url.
// @author       m-pasik & ChatGPT & snuuy
// @match        https://www.youtube.com/*
// @icon         https://www.youtube.com/favicon.ico
// @license      GPL-3.0
// @grant        none
// @downloadURL https://update.greasyfork.org/scripts/474050/Youtube%20share%20url%20si%20parameter%20remover.user.js
// @updateURL https://update.greasyfork.org/scripts/474050/Youtube%20share%20url%20si%20parameter%20remover.meta.js
// ==/UserScript==

(function() {
    'use strict';

    // function removes si parameter from a URL
    const cleanUrl = (urlField) => {
        try {
            const url = new URL(urlField.value, window.location.href); // process relative URL using default URL
            url.searchParams.delete('si'); // remove si parameter
            urlField.value = url.toString(); // update the URL into the organized one
        } catch (e) {
            console.error('URL cleaning error:', e);
        }
    };

    // listen DOM mutation using MutationObserver
    const observer = new MutationObserver((mutationsList) => {
        mutationsList.forEach((mutation) => {
            mutation.addedNodes.forEach((node) => {
                if (node.nodeType === 1) { // ELEMENT_NODE
                    const urlField = node.querySelector('input#share-url');
                    if (urlField) {
                        cleanUrl(urlField); // clean up newly generated URL field
                        urlField.addEventListener('input', () => cleanUrl(urlField)); // listen user input
                    }
                }
            });
        });
    });

    // start listening when DOM in the page updates
    observer.observe(document.documentElement, {
        childList: true,
        subtree: true,
    });

    // regularly check the field to process it when a new URL is generated
    const monitorShareUrl = () => {
        const urlFields = document.querySelectorAll('input#share-url');
        urlFields.forEach(urlField => cleanUrl(urlField));
        window.requestAnimationFrame(monitorShareUrl);
    };

    monitorShareUrl(); // check the URL field when the page loads

})();
§
Posted: 10 Agustus 2024

p.s. This works well with playlists too.

Post reply

Sign in to post a reply.