6

I'm looking for modify a javascript game with an userscript.

The problem is that all the game code is wrapped with a anonymous function like this

(function () { "use strict";
    var js = 'test';
})();

Can I have access to a JS variable with my userscript?

Edit :

See also : How to alter this javascript with Greasemonkey?

This question is not the same as Is it possible to gain access to the closure of a function? !

Community
  • 1
  • 1
A. Joahny
  • 301
  • 2
  • 14
  • 1
    My question is about userscripts, i can't modify the original script unlike the other question. increasingly scripts use anonymous function wrapping... Is this the end of userscripts ? oO – A. Joahny Nov 14 '13 at 20:16
  • 1
    This is not a duplicate of that question. Userscripts have additional options not available to page-scope javascript. – Brock Adams Nov 14 '13 at 22:51

2 Answers2

2

Yes! You can if you use the right browser (Firefox).

In a userscript (on Firefox) you can rewrite the page's JS to give yourself access. See, also, "How to alter this javascript with Greasemonkey?".

Your function call would be like (for an inline script):

checkForBadJavascripts ( [
    [false, /js = 'test'/, replaceTargetJavascript]
] );


function replaceTargetJavascript (scriptNode) {
    var scriptSrc   = scriptNode.textContent;
    scriptSrc       = scriptSrc.replace (
        /js = 'test';/,
        "js = 'test'; window.myJS = js;"
    );

    addJS_Node (scriptSrc);
}

You would then access the variable like:

console.log ("The var is: ", unsafeWindow.myJS);

Alas, there is still no good way to do this kind of thing in a Chrome userscript or a Tampermonkey script.

IF the script, with the JS in question, is external, then in Chrome, you can use beforeload to block it. Then inject your own code instead -- modified to expose that private variable.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks for your response :) Can you have an exemple with external script ? I removed the original external script with the beforescriptexecute event and add dynamically my external script (which is the original without the anonymous function). But the page is loaded before my script is loaded so there are calls to undefined functions... – A. Joahny Nov 15 '13 at 08:07
  • Are you using Firefox plus Greasemonkey? Did you make your [metadata section](http://wiki.greasespot.net/Metadata_Block) like as shown in [this linked answer](http://stackoverflow.com/a/11201555/331508)? Each line is important and you should change only the `@include`, to start. – Brock Adams Nov 15 '13 at 08:58
  • Oops i didn't used exactly the same code. But finally, the original developper left the most important variable in global scope. I just had to copy/paste "utilities functions" of the game in my script. However, thanks for your complete response :) – A. Joahny Nov 18 '13 at 18:33
1

No you cannot access variables that are in a private scope.

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 1
    Yes you can, depending on the code and the browser. Userscripts can sometimes rewrite the JS code to give themselves access. Or use other tools to block the download of the script (if it's not inline). Then the userscript can inject a carefully crafted replacement instead. – Brock Adams Nov 14 '13 at 22:54