Greasy Fork

Greasy Fork is available in English.

JustWatch Link Cleaner - Revised

Cleans JustWatch links to get directly to the movies

当前为 2023-12-25 提交的版本,查看 最新版本

// ==UserScript==
// @name         JustWatch Link Cleaner - Revised
// @namespace    MickyFoley
// @version      1.1
// @description  Cleans JustWatch links to get directly to the movies
// @author       MickyFoley
// @match        https://www.justwatch.com/*
// @license      GPL-3.0-only
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to clean a URL
    function cleanUrl(url) {
        const urlObj = new URL(url);
        const encodedUrl = urlObj.searchParams.get('r');
        if (encodedUrl) {
            // Decoding the URL
            let decodedUrl = decodeURIComponent(encodedUrl);

            // Find the start of the actual link after '&r='
            const start = decodedUrl.indexOf('http');
            if (start === -1) return null; // If 'http' is not found, return null

            // Find the end of the actual link before '&cx='
            const end = decodedUrl.indexOf('&cx=', start);
            decodedUrl = (end === -1) ? decodedUrl.substring(start) : decodedUrl.substring(start, end);

            return decodedUrl;
        }
        return null;
    }

    // Function to clean and modify the links
    function cleanLinks() {
        const links = document.querySelectorAll('a[href*="click.justwatch.com/a?"]');
        console.log(`Found ${links.length} links to clean.`);
        links.forEach(link => {
            const cleanUrlParam = cleanUrl(link.href);
            if (cleanUrlParam) {
                link.href = cleanUrlParam;
            }
        });
    }

    cleanLinks();

    const observer = new MutationObserver(mutations => {
        cleanLinks();
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();