Content Scope Runner: Difference between revisions
Much cleaner. Now with explanation! |
Clean up, provide tips |
||
Line 1: | Line 1: | ||
An extension of the [[Content Script Injection]] technique, this snippet automatically runs the ''entire'' user script in the content scope. | An extension of the [[Content Script Injection]] technique, this snippet automatically runs the ''entire'' user script in the content scope. | ||
<pre class= | <pre class="sample">if(typeof __PAGE_SCOPE_RUN__ == 'undefined') { | ||
(function page_scope_runner() { | (function page_scope_runner() { | ||
var script = document.createElement('script'); | var script = document.createElement('script'); | ||
Line 13: | Line 13: | ||
As soon as execution reaches this code, the entire script will be injected into the page and re-run. 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 [[API]]s. | As soon as execution reaches this code, the entire script will be injected into the page and re-run. 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 [[API]]s. | ||
== Tips == | |||
* This code must not be wrapped in a function, or the <code>return</code> will fail to work. It may be wrapped in a conditional. | |||
* Any code before the block will be run twice: Once in GreaseMonkey context, once in page context. | |||
* Any code after the block will only be run once, in the page context. | |||
== Explanation == | == Explanation == | ||
The code block first checks to see if it is being run inside the page by looking for a marker it knows about. If the marker is not present, the block creates and injects a script element that starts by setting the aforementioned marker variable and finishes by executing the text of the userscript. Finally, the block calls | The code block first checks to see if it is being run inside the page by looking for a marker it knows about. If the marker is not present, the block creates and injects a script element that starts by setting the aforementioned marker variable and finishes by executing the text of the userscript. Finally, the block calls <code>return</code>, ending execution. | ||
<!-- This still needs work. | <!-- This still needs work. |
Revision as of 06:43, 25 July 2010
An extension of the Content Script Injection technique, this snippet automatically runs the entire user script in the content scope.
if(typeof __PAGE_SCOPE_RUN__ == 'undefined') { (function page_scope_runner() { var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = "(function() { var __PAGE_SCOPE_RUN__ = 'yes'; (" + page_scope_runner.caller.toString() + ")(); })();"; document.documentElement.appendChild(script); document.documentElement.removeChild(script); })(); return; }
As soon as execution reaches this code, the entire script will be injected into the page and re-run. 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.
Tips
- This code must not be wrapped in a function, or the
return
will fail to work. It may be wrapped in a conditional. - Any code before the block will be run twice: Once in GreaseMonkey context, once in page context.
- Any code after the block will only be run once, in the page context.
Explanation
The code block first checks to see if it is being run inside the page by looking for a marker it knows about. If the marker is not present, the block creates and injects a script element that starts by setting the aforementioned marker variable and finishes by executing the text of the userscript. Finally, the block calls return
, ending execution.