Greasy Fork

NBA.com - Spoiler Blocker

Enhances NBA.com by hiding video lengths and adding fictitious durations to the progress bar to prevent spoilers. It also hides spoilers on the front page.

目前为 2024-05-05 提交的版本。查看 最新版本

// ==UserScript==
// @name         NBA.com - Spoiler Blocker
// @version      1.1
// @description  Enhances NBA.com by hiding video lengths and adding fictitious durations to the progress bar to prevent spoilers. It also hides spoilers on the front page.
// @author       You
// @match        *://*.nba.com/*
// @license      MIT
// @grant        none
// @run-at       document-start
// @namespace https://greasyfork.org/users/1291378
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('DOMContentLoaded', function() {
        setTimeout(showToast, 500); // Delaying the toast display by 500 milliseconds

        if (window.location.href === 'https://www.nba.com/') {
            // Only run this block if the current URL is exactly 'https://www.nba.com/'

            // Inject CSS to hide specific elements as soon as the page starts loading.
            const css = '.MaxWidthContainer_mwc__ID5AG { display: none !important; }';
            const head = document.head || document.getElementsByTagName('head')[0];
            const style = document.createElement('style');
            style.type = 'text/css';
            if (style.styleSheet){
                // IE8 and below
                style.styleSheet.cssText = css;
            } else {
                // Other browsers
                style.appendChild(document.createTextNode(css));
            }
            head.appendChild(style);
        }

        if (!window.location.search.includes('watchLive=true')) {
            function getRandomMaxLength() {
                const threeHoursInSeconds = 3 * 60 * 60;
                const oneHourInSeconds = 60 * 60;
                return threeHoursInSeconds + Math.floor(Math.random() * oneHourInSeconds);
            }

            const standardMaxLength = getRandomMaxLength();

            function randomizeProgressBar() {
                const progressBar = document.getElementById('progress-bar');
                const rangeInput = document.querySelector('.sc-beqWaB.bpZMgR input[type="range"]');
                if (progressBar && rangeInput) {
                    progressBar.max = standardMaxLength;
                    rangeInput.max = standardMaxLength;
                    rangeInput.setAttribute('aria-valuemax', standardMaxLength);
                }
            }

            function hideTimeElements() {
                const timePattern = /\b\d{2}:\d{2}:\d{2}\b/;
                const spans = document.querySelectorAll('span');
                spans.forEach(span => {
                    if (timePattern.test(span.innerText)) {
                        span.style.display = 'none';
                    }
                });
            }

            const observer = new MutationObserver((mutations) => {
                mutations.forEach(mutation => {
                    if (mutation.type === 'childList' || mutation.type === 'attributes') {
                        hideTimeElements();
                    }
                });
            });

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

            setInterval(randomizeProgressBar, 1000);
            hideTimeElements();
        }

        function showToast() {
            const toast = document.createElement('div');
            toast.textContent = 'NBA Spoiler Blocker has been activated!';
            toast.style.position = 'fixed';
            toast.style.top = '20px';
            toast.style.left = '50%';
            toast.style.transform = 'translateX(-50%)';
            toast.style.padding = '10px 20px';
            toast.style.color = '#FFF';
            toast.style.background = '#FF0000';
            toast.style.borderRadius = '5px';
            toast.style.boxShadow = '0 2px 6px rgba(0,0,0,0.3)';
            toast.style.zIndex = '99999';
            document.body.appendChild(toast);
            setTimeout(() => toast.remove(), 3000);
        }
    });
})();