Skip to main content
2 of 2
added 951 characters in body
Brock Adams
  • 13k
  • 5
  • 39
  • 64

The question is still not clear, please rewrite it like:

  1. I do X.
  2. I expect Y.
  3. But I get Z.

Update:

From the comments: Apparently the OP's problem really was that accessing StackExchange.options.routeName in the script, outside the injected code, caused a ReferenceError, when using Scriptish, not Tampermonkey and Greasemonkey as stated in the question.

This is because Scriptish does not yet break the sandbox the same way that Greasemonkey and Tampermonkey do in @grant none mode.

Solutions in decreasing order of preference/robustness:

  • Don't use @grant none mode if you can help it. Keep script code sandboxed or inject it.
  • Inject all code that depends on the target page's javascript.
  • Don't use Scriptish for code needing @grant none until such time as Scriptish more fully emulates the abysmal design decisions that Greasemonkey made with @grant.

Old answer, may be useful to any others with similar problems:

  • The question's current code seems to work as expected on both browsers (Using Tampermonkey on Chrome).

  • A setTimeout or AJAX tricks don't seem to be needed to wait for, or access, that object.

  • If you mean that you can't use start_up from the console, that's the nature of the particular with_jquery() function being used. It wraps your function in parentheses () which effectively makes it anonymous.

I recommend never doing jQuery that way from a userscript. 99% of the time, use @require and a @grant setting other than none.

On friendly, jQuery powered pages, where heavy interaction with page objects is desired (like Stack Exchange sites), use the page's jQuery like this:

// ==UserScript==
// @name        (SE) Inspect global object
// @match       *://*.stackoverflow.com/*
// @match       *://*.stackapps.com/*
// @grant none
// ==/UserScript==

//var start_up = function( $ ) {    // BAD
function start_up ( $ ) {           // GOOD
    console.log (
        "===> ",
        typeof StackExchange.options.routeName,
        StackExchange.options.routeName
    );
};

withPages_jQuery (start_up);

function withPages_jQuery (NAMED_FunctionToRun) {
    //--- Use named functions for clarity and debugging...
    var funcText        = NAMED_FunctionToRun.toString ();
    var funcName        = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1");
    var script          = document.createElement ("script");
    script.textContent  = funcText + "\n\n";
    script.textContent += 'jQuery(document).ready( function () {' + funcName + '(jQuery);} );';
    document.body.appendChild (script);
};

This allows you to see your function (start_up) and use or debug it from the console.

Brock Adams
  • 13k
  • 5
  • 39
  • 64