0

I am loading a webpage that is not mine, so I can't edit the source code. I want to change just one variable in one of it's script tags, that looks like this

<script>
    preview=0;
    test=0;
</script>

Now in the actual page there is more in between the tags, but that isn't what I'm asking for. I want to change just the test = 0; line into test = 1; before the rest of the page loads. How can I do this?

j08691
  • 204,283
  • 31
  • 260
  • 272

1 Answers1

0

You can't modify a web page before its document has loaded, obviously.

But there is one window event that fires after a document is loaded but before stylesheets, images, and subframes have finished loading.

It's called DOMContentLoaded:

    targetWindow.addEventListener('DOMContentLoaded', (event) => {
        // do your page edits here
    });

You will need access to this page's window object. If you're loading the web page into an iframe, see How to get a reference to an iframe's window object inside iframe's onload handler created from parent window.

terrymorse
  • 6,771
  • 1
  • 21
  • 27