cbc-kitten-scientists icon indicating copy to clipboard operation
cbc-kitten-scientists copied to clipboard

typeof gamePage always returns "undefined" in Firefox in userscript

Open fl0wc0ntr0l opened this issue 7 years ago • 3 comments

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.

fl0wc0ntr0l avatar Nov 14 '17 21:11 fl0wc0ntr0l

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

herrlock avatar Dec 08 '17 18:12 herrlock

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();
    }
}

fl0wc0ntr0l avatar Dec 11 '17 14:12 fl0wc0ntr0l

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.

s-bernard avatar Jan 14 '19 04:01 s-bernard