javascriptgreasemonkey

Intercept and alter a site's javascript using greasemonkey


Suppose there is a site that includes an external .js file in an html script tag like so:

<script src="somescript.js">

I want greasemonkey to intercept each of such scripts, and alter some of the values in them before they execute. For example, I want to change all occurrences of the value "400" to "300" within them, then continue to load the page as if the scripts used those values instead of the original ones. Currently I'm using the following code in greasemonkey:

function replaceTargetJavascript (scriptNode) {
    var scriptSrc   = scriptNode.textContent;
    scriptSrc       = scriptSrc.replace (
        /'400'/,
        "'300'"
    );

    addJS_Node (scriptSrc);
}

document.addEventListener("beforescriptexecute", function(e) {

    checkForBadJavascripts ( [
        [false, /'400'/, replaceTargetJavascript]
    ] );
}, true);

Which according to my sources is the right way to do it, but it is not working. Can anyone please help me figure this out?


Solution

  • Old question, but I needed to do this recently. Here's how I did it using GreaseMonkey.

    You add the beforescriptexecute listener, and wait for your target script to be loaded, checking the src tag to identify the correct script.

    Then you stop that script from loading and get the script source yourself using GM_xmlhttpRequest.

    Then you are free to modify the script as you please and insert it back into the DOM.

    // ==UserScript==
    // @name        Test
    // @namespace   Test
    // @description TEST
    // @include     http://the.website.com/*
    // @version     1
    // @grant       GM_xmlhttpRequest
    // @run-at      document-start
    // ==/UserScript==
    
    function addScript(text) {
        text = text.replace(/replaceThis();/g, "");
        var newScript = document.createElement('script');
        newScript.type = "text/javascript";
        newScript.textContent = text;
        var head = document.getElementsByTagName('head')[0];
        head.appendChild(newScript);
    }
    
    window.addEventListener('beforescriptexecute', function(e) {
        src = e.target.src;
        if (src.search(/script_to_modify\.js/) != -1) {
            e.preventDefault();
            e.stopPropagation();        
            GM_xmlhttpRequest({
                method: "GET",
                url: e.target.src,
                onload: function(response) {
                    addScript(response.responseText);
                }
            });
        }
    });