The question is still not clear, please rewrite it like:
- I do X.
- I expect Y.
- But I get Z.
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.