YouTube to MPC-BE (Protocol Handler)

Opens YouTube links in MPC-BE using custom protocol

As of 2024-12-21. See the latest version.

// ==UserScript==
// @name         YouTube to MPC-BE (Protocol Handler)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Opens YouTube links in MPC-BE using custom protocol
// @author       CL
// @match        https://*.youtube.com/*
// @match        https://youtube.com/*
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to check if a URL is a YouTube watch URL
    function isYouTubeWatchURL(url) {
        return url.includes('youtube.com/watch?') && url.includes('v=');
    }

    // Function to handle link clicks
    function handleLinkClick(event) {
        const link = event.target.closest('a');
        if (!link) return;

        const url = link.href;
        if (!isYouTubeWatchURL(url)) return;

        event.preventDefault();
        event.stopPropagation();

        // Open using custom protocol
        window.location.href = `mpc://${url}`;
    }

    // Add click event listener to the document
    document.addEventListener('click', handleLinkClick, true);
})();