Greasy Fork

PayWall Buster El País

Quita el paywall de El País, si usas un bloqueador de anuncios: Elimina el mensaje "Para poder seguir navegando, permite que se te muestren anuncios o, si lo prefieres, suscríbete."

目前为 2020-06-07 提交的版本。查看 最新版本

// ==UserScript==
// @name         PayWall Buster El País
// @version      0.3
// @description  Quita el paywall de El País, si usas un bloqueador de anuncios: Elimina el mensaje "Para poder seguir navegando, permite que se te muestren anuncios o, si lo prefieres, suscríbete."
// @match        https://elpais.com/*
// @include      https://elpais.com/*
// @grant        none
// @run-at       document-start
// jshint esversion: 6
// @namespace https://greasyfork.org/users/161767
// ==/UserScript==

// Blocks paywall scripts
function needsToBeBlacklisted(value, type){
    let response = false;
    if(value !== undefined && value !== null && value.search(/p\.js/) > -1){
      response = true;
    }

    return response;
}

const createElementBackup = document.createElement
document.createElement = function(...args) {
    // If this is not a script tag, bypass
    if(args[0].toLowerCase() !== 'script')
        // Binding to document is essential
        return createElementBackup.bind(document)(...args)

    const scriptElt = createElementBackup.bind(document)(...args)
   
    // Backup the original setAttribute function
    const originalSetAttribute = scriptElt.setAttribute.bind(scriptElt)

    // Define getters / setters to ensure that the script type is properly set
    Object.defineProperties(scriptElt, {
        'src': {
            get() {
                return scriptElt.getAttribute('src')
            },
            set(value) {
                if(scriptElt.type !== undefined && scriptElt.type !== null && needsToBeBlacklisted(value, scriptElt.type)) {
                    originalSetAttribute('type', 'javascript/blocked')
                }
              
                if(scriptElt.src !== null && scriptElt.src !== undefined) {
                    originalSetAttribute('src', value)
                }
                return true
            }
        },
        'type': {
            set(value) {
                const src = scriptElt.src !== undefined ? scriptElt.src : '';
                const type = scriptElt.type !== undefined ? scriptElt.type : '';
                const typeValue = needsToBeBlacklisted(src, type) ? 'javascript/blocked' : value
                if(scriptElt.type !== undefined){
                    originalSetAttribute('type', typeValue)
                }
                return true
            }
        }
    })

    // Monkey patch the setAttribute function so that the setter is called instead.
    // Otherwise, setAttribute('type', 'whatever') will bypass our custom descriptors!
    scriptElt.setAttribute = function(name, value) {
        if(name === 'type' || name === 'src')
            scriptElt[name] = value
        else
            HTMLScriptElement.prototype.setAttribute.call(scriptElt, name, value)
    }

    return scriptElt
}