chwzr
chwzr

Reputation: 151

Changing a javascript variable with greasemonkey at running time

i have a page that has a .js script inside, in the page its

    <script>
        Client.includeJS(
            'js/scratchticket.js'
        );
    </script>

in this scratchticket.js

is following code

 $(document).ready(function() {
        ...
        var xxx = 100;    
        ...
  });

so i testet various codes in greasemonkey, but i cannot edit this variable that is it e.g. 200

i testet to block the scratchticket.js with adblock an paste the whole code into greasemonke script and change the var but it is not working, i dont know why the page says loading... at the time when it should do the code.

any idea how to change the variable in a other way?

Upvotes: 0

Views: 1000

Answers (2)

Collin Chaffin
Collin Chaffin

Reputation: 942

The problem is actually that @Quasimodo's clone answer above is absolutely wrong about when it executes, and it is the opposite.

To quote the wiki he cites:

document-end is the standard behavior that Greasemonkey has always had (see DOMContentLoaded). This is the default if no value is provided.

So, your issue is probably the common one for those starting out with userscripts, in that your script is executing too late (end) and you simply need to add this directive at the top:

// @run-at        document-start

Try that and see if it helps!

Upvotes: 1

Pinke Helga
Pinke Helga

Reputation: 6682

You need to provide more information and code in order to get appropriate answers. It is important to know when the userscript runs and if there are any grants.

If you just want to change JS code setting an initial value, consider listening to the beforescriptexecute event. On firing you have first to indentify if the event target is the script you are looking for. Then you can search and replace the desired JavaScript line. You have to cancel the execution of the original script and insert the modified code within a new <script> tag into the DOM, since you cannot manipulate loaded scripts directly.

Upvotes: 0

Related Questions