Greasy Fork

Greasy Fork is available in English.

JustWatch Link Cleaner

Cleans JustWatch links to get directly to the movies

当前为 2023-08-19 提交的版本,查看 最新版本

// ==UserScript==
// @name         JustWatch Link Cleaner
// @namespace    MickyFoley
// @version      1.0
// @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 decodedUrl = decodeURIComponent(url);
        const cleanUrlParam = new URL(decodedUrl).searchParams.get('r');
        if (cleanUrlParam) {
            const urlWithoutSearchReferral = cleanUrlParam.split('?searchReferral=')[0];
            return urlWithoutSearchReferral;
        }
        return null;
    }

    // Function to clean and modify the links
    function cleanLinks() {
        // Get all the anchor elements on the page
        const links = document.querySelectorAll('a[href*="click.justwatch.com/a?r="]');

        // Loop through each link and modify its href attribute
        links.forEach(link => {
            const cleanUrlParam = cleanUrl(link.href);
            if (cleanUrlParam) {
                link.href = cleanUrlParam;
            }
        });
    }

    // Call the cleanLinks function initially
    cleanLinks();

    // Set up a MutationObserver to monitor DOM changes
    const observer = new MutationObserver(mutations => {
        // Call the cleanLinks function whenever the DOM changes
        cleanLinks();
    });

    // Start observing the DOM for changes
    observer.observe(document.body, { childList: true, subtree: true });
})();