Regwall Element Blocker

Blocks rendering of elements with class or id containing "regwall"

目前為 2023-06-26 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Regwall Element Blocker
// @namespace    regwall-element-blocker
// @version      1.3.2
// @description  Blocks rendering of elements with class or id containing "regwall"
// @match        https://www.economist.com/*
// @match        *://*.fortune.com/*
// @run-at       document-start
// @author       TIME
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let loadCustomPage = () => {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", window.location.href, true);
    xhr.onerror = function () {
        document.documentElement.innerHTML = "Error getting Page!";
    };
    xhr.send();
    xhr.onreadystatechange = function() {
        let states = [
            "Removing the Subscription...",
            "Initiating the Request...",
            "Establishing the Server...",
            "Request Received...",
            "Processing the Request...",
            "Error Finding the Page!"
        ];
        document.documentElement.innerHTML = states[this.readyState] || "Error Finding the Page!";
        if (this.readyState == 4 && this.status == 200) {
            removeSubscription(this.responseText);
        }
    };
};

let removeSubscription = (htmlContentStr) => {
    let wrapper = document.createElement("DIV");
    wrapper.innerHTML = htmlContentStr;
    if (matchDomain('economist.com')) {
        removeElements(wrapper, ".paywall");
        removeElements(wrapper, ".subscription-benefits");
    } else if (matchDomain('fortune.com')) {
        hideElementsBySelector(wrapper, 'tp-container-inner');
        hideElementsBySelector(wrapper, '.paywall-selector paywallFade');
        hideElementsBySelector(wrapper, 'lazy-transclude');
    }
    document.documentElement.innerHTML = "Removing the Ads...";
    removeElements(wrapper, ".advert");
    putNewPage(wrapper);
};

let removeElements = (wrapper, selector) => {
    let elements = wrapper.querySelectorAll(selector);
    elements.forEach((element) => {
        element.remove();
    });
};

let hideElementsBySelector = (wrapper, selector) => {
    let elements = wrapper.querySelectorAll(selector);
    elements.forEach((element) => {
        element.style.display = 'none';
    });
};

let putNewPage = (pageHtml) => document.documentElement.innerHTML = pageHtml.innerHTML;

window.stop();
loadCustomPage();

function matchDomain(domains) {
    const hostname = window.location.hostname;
    if (typeof domains === 'string') { domains = [domains]; }
    return domains.some(domain => hostname === domain || hostname.endsWith('.' + domain));
}

})();