Peach Video AdBlock

Removes Pornhub ads while preserving floating Like/Favorite controls

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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();
})();