|
|
(13 intermediate revisions by 6 users not shown) |
Line 1: |
Line 1: |
| An extension of the [[Content Script Injection]] technique, this snippet automatically runs the ''entire'' user script in the content scope.
| | As of Greasemonkey 1.0 specifying <code>[[@grant]] none</code> provides the behavior previously available by this technique. |
|
| |
|
| <pre class='sample'>if(typeof __PAGE_SCOPE_RUN__ == 'undefined') {
| | The previous content of this page is available [http://wiki.greasespot.net/index.php?title=Content_Scope_Runner&oldid=7215 via history] only. |
| (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;
| |
| }</pre>
| |
| | |
| 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.
| |
| | |
| == 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.
| |
| | |
| <!-- This still needs work.
| |
| == @require ==
| |
| | |
| 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 [[Metadata Block]]:
| |
| | |
| <nowiki>// @require http://userscripts.org/scripts/source/68059.user.js</nowiki>
| |
| -->
| |
| | |
| [[Category:Coding Tips:Interacting With The Page]]
| |
| [[Category:@require Library]]
| |
As of Greasemonkey 1.0 specifying @grant none
provides the behavior previously available by this technique.
The previous content of this page is available via history only.