Content Scope Runner: Difference between revisions
From GreaseSpot Wiki
Jump to navigationJump to search
Created page with 'An extension of the content script injection technique, this snippet automatically runs the ''entire'' user script in the content scope. <pre class='sample'> (function page_…' |
|||
Line 33: | Line 33: | ||
If used as the first [[Metadata Block#.40require|@require]]d script, this snippet will continue to run and work perfectly. | If used as the first [[Metadata Block#.40require|@require]]d script, this snippet will continue to run and work perfectly. | ||
Try it by adding this line as the first @require in the [[ | Try it by adding this line as the first @require in the [[Metadata Block]]: | ||
// @require http://userscripts.org/scripts/source/68059.user.js | <nowiki>// @require http://userscripts.org/scripts/source/68059.user.js</nowiki> | ||
[[Category:Coding Tips:Interacting With The Page]] | [[Category:Coding Tips:Interacting With The Page]] | ||
[[Category:@require Library]] | [[Category:@require Library]] |
Revision as of 18:43, 4 February 2010
An extension of the content script injection technique, this snippet automatically runs the entire user script in the content scope.
(function page_scope_runner() { if ('undefined' == typeof __PAGE_SCOPE_RUN__) { // If we're _not_ already running in the page, grab the full source // of this script. var my_src = page_scope_runner.caller.toString(); // Create a script node holding this script, plus a marker that lets us // know we are running in the page scope (not the Greasemonkey sandbox). var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = "const __PAGE_SCOPE_RUN__ = true;" + my_src; // Insert the script node into the page, so it will run, and immediately // remove it to clean up. document.body.appendChild(script); document.body.removeChild(script); // Stop running, because we know Greasemonkey actually runs us in // an anonymous wrapper. return; } })();
As long as this is the first piece of code in your script, it will stop execution of the remainder of the script, and re-start execution completely within the content scope. Thus you need not worry about any of the security restrictions from XPCNativeWrappers in the Greasemonkey sandbox. You also will, of course, not have access to any of the APIs.
@require
If used as the first @required script, this snippet will continue to run and work perfectly. Try it by adding this line as the first @require in the Metadata Block:
// @require http://userscripts.org/scripts/source/68059.user.js