cameroncondry / cbc-kitten-scientists

Add-on for the wonderful incremental browser game: http://kittensgame.com/web/
MIT License
113 stars 94 forks source link

typeof gamePage always returns "undefined" in Firefox in userscript #223

Open fl0wc0ntr0l opened 7 years ago

fl0wc0ntr0l commented 7 years ago

Using Firefox v57.0 and Greasemonkey 4.0, Kitten Scientists does not start successfully.

The problem lies in the loadTest function. For some reason, when the loadTest check is performed, typeof gamePage will always return undefined, despite gamePage being defined. Testing using the Firefox JS console confirms that typeof gamePage returns object correctly.

My guess here is that the function is recursive in a manner, always utilizing the undefined gamePage that "existed" when the script started, and never successfully updates. That is total speculation, though, and I have not found a way to get it to successfully recognize the actual gamePage.

herrlock commented 6 years ago

With unsafeWindow.gamePage the gamePage variable can be read. (https://wiki.greasespot.net/UnsafeWindow)

fl0wc0ntr0l commented 6 years ago

Still no joy in getting it to work by modifying loadTest like so:

var loadTest = function() {
    if (typeof unsafeWindow.gamePage === 'undefined') {
        // Test if kittens game is already loaded or wait 2s and try again
        setTimeout(function(){
            loadTest();
        }, 2000);
    } else {
        // Kittens loaded, run Kitten Scientist's Automation Engine
        game = unsafeWindow.gamePage;
        run();
    }
}
s-bernard commented 5 years ago

A solution is to wrap the script in a window.eval call.

Add var code = "(" + (function() { just after // ==/UserScript== and add

}).toString() + ")()";

window.eval(code);

at the end.