Intercept and alter a site's javascript using greasemonkey

Joey

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?

Jack Culhane

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);
            }
        });
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

redirect using javascript or jquery in greasemonkey

From Dev

Greasemonkey script to intercept key presses

From Dev

Greasemonkey script to intercept key presses

From Dev

Replacing a javascript function inside an object using GreaseMonkey

From Dev

Using javascript to alter content of a <div>

From Dev

Alter data attribute using javascript

From Dev

Get contents of another site's element using only JavaScript

From Dev

Using .NET (website project), how can I intercept all incoming image requests and alter their request path?

From Dev

Intercept outgoing URL's using python

From Dev

Using JavaScript to alter links to current host

From Dev

Using JavaScript to alter attributes in a MVC web application

From Dev

Save information from a website using Javascript/Greasemonkey Addon

From Dev

Using JavaScript and Greasemonkey to reload just one tab in Firefox?

From Dev

Move a button over any images on the website Using Javascript for GreaseMonkey

From Dev

using innerHTML with Greasemonkey

From Dev

Change the URL using GreaseMonkey

From Dev

Login with Scrapy with site using Javascript

From Dev

Use Greasemonkey to change Text to Images (on a static site)?

From Dev

intercept click only on "clickable" elements using jquery (or javascript)

From Dev

Is it possible to alter an email using PHP's IMAP functions?

From Dev

Using a regular expression in a Greasemonkey @include?

From Dev

Opening Multiple Hyperlinks Using Greasemonkey

From Dev

Change Class value using Greasemonkey?

From Dev

How to hide an element using Greasemonkey

From Dev

JavaScript intercept module import

From Dev

Intercept Paste data in JavaScript

From Dev

Get body element of site using only javascript

From Dev

Logging Into Site Using Javascript HTTP Request?

From Dev

RegEx works in tester not on site using JavaScript

Related Related

  1. 1

    redirect using javascript or jquery in greasemonkey

  2. 2

    Greasemonkey script to intercept key presses

  3. 3

    Greasemonkey script to intercept key presses

  4. 4

    Replacing a javascript function inside an object using GreaseMonkey

  5. 5

    Using javascript to alter content of a <div>

  6. 6

    Alter data attribute using javascript

  7. 7

    Get contents of another site's element using only JavaScript

  8. 8

    Using .NET (website project), how can I intercept all incoming image requests and alter their request path?

  9. 9

    Intercept outgoing URL's using python

  10. 10

    Using JavaScript to alter links to current host

  11. 11

    Using JavaScript to alter attributes in a MVC web application

  12. 12

    Save information from a website using Javascript/Greasemonkey Addon

  13. 13

    Using JavaScript and Greasemonkey to reload just one tab in Firefox?

  14. 14

    Move a button over any images on the website Using Javascript for GreaseMonkey

  15. 15

    using innerHTML with Greasemonkey

  16. 16

    Change the URL using GreaseMonkey

  17. 17

    Login with Scrapy with site using Javascript

  18. 18

    Use Greasemonkey to change Text to Images (on a static site)?

  19. 19

    intercept click only on "clickable" elements using jquery (or javascript)

  20. 20

    Is it possible to alter an email using PHP's IMAP functions?

  21. 21

    Using a regular expression in a Greasemonkey @include?

  22. 22

    Opening Multiple Hyperlinks Using Greasemonkey

  23. 23

    Change Class value using Greasemonkey?

  24. 24

    How to hide an element using Greasemonkey

  25. 25

    JavaScript intercept module import

  26. 26

    Intercept Paste data in JavaScript

  27. 27

    Get body element of site using only javascript

  28. 28

    Logging Into Site Using Javascript HTTP Request?

  29. 29

    RegEx works in tester not on site using JavaScript

HotTag

Archive