Discussions » Development

How to modify a json value inside an embedded script?

§
Posted: 2020-02-19

How to modify a json value inside an embedded script?

Hi, there's a site with an embedded JS script featuring a configuration JSON that is then supplied to an external JS script, I'd like to make a script to live update the configuration JSON before it is supplied to the external JS script.

Basically, the HTML page looks like this:

html
    head
        script
            //someJS
        /script
        script
            let J=some init;
            $(document).ready(function () {
                $("#container").extScript({
                    //configuration JSON
                    btnTest: { enabled: false }, <<<<<<< THERE
                    //configuration JSON
                });
            })
        /script
    /head
    body
    /body
/html

The whole idea is to switch 'btnTest.enabled' from 'false' to 'true'.

I started like this:

// ==UserScript==
// @name     www.test.com
// @version  1
// @include  https://www.test.com*
// @run-at   document-start
// @grant    none
// ==/UserScript==

 (function () {
  var scripts = document.getElementsByTagName('script');
  for (var i = 0; i < scripts.length; i++)
  {
    console.log('script detected:', scripts[i].src);
    var Matches = scripts[i].innerHTML.match(/btnTest/);
//    if (Matches && Matches[1]) {
//       console.log(Matches[1]);
//      location.href = Matches[1];
     }
   }
 }) ();

But nothing seems to work at all, even in this partial state. Any advice?

wOxxOmMod
§
Posted: 2020-02-19

Script tags run just once when they are first added/parsed so changing their tags afterwards does nothing, it won't even matter if the script elements are removed. You'll need to change the code of the page before it comes into the tab. There are two methods: 1) Firefox extensions that use browser.webRequest.filterResponseData and 2) programs like Fiddler or Charles proxy that can be configured to modify the HTTP response.

Post reply

Sign in to post a reply.