I notice that you did not give a real example, and I'm not going to cover all the hypothetical possibilities; there are a variety of techniques for a variety of real-world websites.
The safe, cross-browser, works almost every time it's tried, method is Script Injection:
function GM_main () {
var newVar = WEBSITE.uniqueObject.prototype.uniquePrototype;
/* Or, explicitely...
var newVar = window.WEBSITE.uniqueObject.prototype.uniquePrototype;
*/
// DO WHATEVER WITH newVar HERE.
}
addJS_Node (null, null, GM_main);
//-- This is a standard-ish utility function
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
var D = document;
var scriptNode = D.createElement ('script');
if (runOnLoad) {
scriptNode.addEventListener ("load", runOnLoad, false);
}
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}
Two variations of this were also linked from your previous question.
However, the most common cases of people wanting to use unsafeWindow
seems to be to defeat ad-display timers or to trigger javascript that is normally triggered by a link or button.
In the common button/link scenario, don't break the sandbox with unsafeWindow
. Just programmatically click
or mousedown
the control.
In the case of cheating a website timer, since it is one line (not counting the hack), this is one case where unsafeWindow
might be a good fit. EG:
unsafeWindow.payTheBillsTimerCounter = 0;
Beware:
- Angry webmasters can theoretically exploit
unsafeWindow
.
- Please don't violate any Terms of Service (TOS).
- Please support websites whose resources you use (a lot).
- For Chrome userscripts and content-scripts, the
unsafeWindow
hack will probably be blocked circa Chrome version 28. For straight scripting on Chrome, switch to Tampermonkey. Tampermonkey is likely to keep supporting unsafeWindow
, without any hacks needed (on your part).
Tampermonkey also provides near-perfect Greasemonkey compatibility and a host of features that Chrome userscripts do not.