Peach Video AdBlock

Removes Pornhub ads while preserving floating Like/Favorite controls

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Peach Video AdBlock
// @namespace    com.local.peach
// @version      1.0.1
// @description  Removes Pornhub ads while preserving floating Like/Favorite controls
// @author       Peach
// @match        *://pornhub.com/*
// @match        *://*.pornhub.com/*
// @match        *://pornhubpremium.com/*
// @match        *://*.pornhubpremium.com/*
// @run-at       document-start
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const EMPTY_SELECTOR = 'div.clearfix.noBottom';
    const HIDE_ONLY_SELECTOR = '.abovePlayerButtonsWrapper,.ctasActionMenuWrap';
    const HIDE_ONLY_SELECTORS = [
        '.abovePlayerButtonsWrapper',
        '.ctasActionMenuWrap'
    ];
    const CLEANUP_SELECTORS = [
        '.ad-tabSplit',
        '.watchpageAd',
        '.removeCTABtn',
        'iframe[data-spot-id="981"]',
        'iframe[data-spot-id="985"]',
        '.adsRemoveButtonWrapper',
        '.adsRemoveButton',
        '.videoAdWrapper',
        '.video-ad-container',
        '.video-ad-overlay',
        '.adOverlay',
        '.bottomNav',
        '.bottomNav a[data-event="header_paid_tabs"]',
        '.bottomNav .js-liveCam',
        '.tjLinksWrapper',
        '.tj-inban-icon',
        'a[href*="trafficjunky.com"]',
        'a.eudsaLink[href*="tabinfo"]',
        '.stickyTooltipWrapper',
        '#mobileHeader',
        '#tabFilters',
        '.js-mainFooter'
    ];

    function addEarlyStyle() {
        if (document.getElementById('peach-video-adblock-style')) {
            return;
        }

        const style = document.createElement('style');
        style.id = 'peach-video-adblock-style';
        style.textContent = CLEANUP_SELECTORS.concat(HIDE_ONLY_SELECTORS).join(',') + '{display:none!important;}';
        (document.head || document.documentElement).appendChild(style);
    }

    function removeElement(element) {
        try {
            if (element && element.matches(HIDE_ONLY_SELECTOR)) {
                return;
            }
        } catch (_) {}

        if (element && element.parentNode) {
            element.parentNode.removeChild(element);
        }
    }

    function cleanEmpty(root = document) {
        try {
            root.querySelectorAll(EMPTY_SELECTOR).forEach(element => {
                if (element.matches(HIDE_ONLY_SELECTOR)) {
                    return;
                }

                if (!element.textContent.trim() && element.children.length === 0) {
                    removeElement(element);
                }
            });
        } catch (_) {}
    }

    function isAdIframe(iframe) {
        if (!iframe || iframe.tagName !== 'IFRAME') {
            return false;
        }

        const spotId = iframe.getAttribute('data-spot-id') || '';
        const src = iframe.getAttribute('src') || '';
        const srcdoc = iframe.getAttribute('srcdoc') || '';

        return (
            spotId === '981' ||
            spotId === '985' ||
            /trafficjunky/i.test(src) ||
            /trafficjunky|deep_click|ad delivery system/i.test(srcdoc)
        );
    }

    function clean(root = document) {
        if (!root || !root.querySelectorAll) {
            return;
        }

        for (const selector of CLEANUP_SELECTORS) {
            try {
                root.querySelectorAll(selector).forEach(removeElement);
            } catch (_) {}
        }

        try {
            root.querySelectorAll('iframe').forEach(iframe => {
                if (isAdIframe(iframe)) {
                    removeElement(iframe);
                }
            });
        } catch (_) {}
    }

    function cleanNode(node) {
        if (!node || node.nodeType !== Node.ELEMENT_NODE) {
            return;
        }

        try {
            if (node.matches(HIDE_ONLY_SELECTOR)) {
                return;
            }
        } catch (_) {}

        for (const selector of CLEANUP_SELECTORS) {
            try {
                if (node.matches(selector)) {
                    removeElement(node);
                    return;
                }
            } catch (_) {}
        }

        if (node.tagName === 'IFRAME' && isAdIframe(node)) {
            removeElement(node);
            return;
        }

        clean(node);
        cleanEmpty(node);

        try {
            if (
                node.matches(EMPTY_SELECTOR) &&
                !node.textContent.trim() &&
                node.children.length === 0
            ) {
                removeElement(node);
            }
        } catch (_) {}
    }

    function start() {
        if (!document.documentElement) {
            setTimeout(start, 5);
            return;
        }

        addEarlyStyle();
        clean();
        cleanEmpty();

        const observer = new MutationObserver(mutations => {
            for (const mutation of mutations) {
                for (const node of mutation.addedNodes) {
                    cleanNode(node);
                }
            }
        });

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

        setInterval(() => {
            clean();
            cleanEmpty();
        }, 1500);
    }

    start();
})();