Greasemonkey Manual:Environment: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
No edit summary
m (Update for 4.0)
 
(38 intermediate revisions by 10 users not shown)
Line 1: Line 1:
[http://library.cshl.edu/wp/vb/member.php?u=1380 verizon ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=163 cheap adipex] [http://students.hsc.unt.edu/housing/item.cfm?type=2859 meridia online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=209 samsung ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=170 free cingular ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=78 clomid online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30298 cheap lisinopril] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=74 cheap carisoprodol] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=352 paxil online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=227 ericsson ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1386 free mono ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=69 albuterol online] [http://wc1.worldcrossing.com/WebX/.1de60a12 ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=171 ambien online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=188 levitra online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=83 cheap didrex] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30358 free midi ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2879 clomid online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=186 cheap lipitor] [http://news.engin.brown.edu/forums/thread-view.asp?tid=215 sagem ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=346 cheap meridia] [http://students.hsc.unt.edu/housing/item.cfm?type=2923 free sony ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30237 valium online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=171 clomid online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=184 hoodia online] [http://wc1.worldcrossing.com/WebX/.1de60a1b music ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30264 hydrocodone] [http://wc1.worldcrossing.com/WebX/.1de609db but adipex] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30321 cheap hgh] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=90 cheap hgh] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30263 order levitra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=133 tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=226 free sony ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=117 propecia online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=165 cheap vicodin] [http://library.cshl.edu/wp/vb/member.php?u=1340 soma online] [http://students.hsc.unt.edu/housing/item.cfm?type=2909 free verizon ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30252 meridia online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=221 order vicodin] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30347 verizon ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1354 cheap alprazolam] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30330 nokia ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2903 qwest ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a26 free sharp ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30348 polyphonic ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=345 cheap viagra] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=239 sildenafil online] [http://students.hsc.unt.edu/housing/item.cfm?type=2876 didrex online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=142 wellbutrin] [http://students.hsc.unt.edu/housing/item.cfm?type=2872 cyclobenzaprine online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30333 funny ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a00 rivotril online] [http://library.cshl.edu/wp/vb/member.php?u=1356 paxil online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30357 free sharp ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=123 free sagem ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30268 xenical online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=92 cheap hydrocodone] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=100 cheap lortab] [http://students.hsc.unt.edu/housing/item.cfm?type=2850 phentermine online] [http://students.hsc.unt.edu/housing/item.cfm?type=2869 cheap lorazepam] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=116 free polyphonic ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1361 order xenical] [http://library.cshl.edu/wp/vb/member.php?u=1362 buy wellbutrin] [http://wc1.worldcrossing.com/WebX/.1de60a2b cheap ultracet] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=113 paxil online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30279 ultracet online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=229 jazz ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2863 alprazolam online] [http://wc1.worldcrossing.com/WebX/.1de609f4 free mono ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1353 vicodin] [http://students.hsc.unt.edu/housing/item.cfm?type=2880 cheap albuterol] [http://wc1.worldcrossing.com/WebX/.1de609eb free funny ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=181 free free ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2887 pharmacy online online] [http://library.cshl.edu/wp/vb/member.php?u=1364 cheap nexium] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=222 vigrx online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=166 buy ativan] [http://library.cshl.edu/wp/vb/member.php?u=1381 free polyphonic ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=180 flexeril online] [http://wc1.worldcrossing.com/WebX/.1de609fd free punk ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=102 free midi ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a2c but ultram] [http://students.hsc.unt.edu/housing/item.cfm?type=2853 fioricet online] [http://students.hsc.unt.edu/housing/item.cfm?type=2899 tracfone ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=185 hydrocodone online] [http://library.cshl.edu/wp/vb/member.php?u=1378 free sprint ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1350 order meridia] [http://news.engin.brown.edu/forums/thread-view.asp?tid=152 cheap valium] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=208 cheap phentermine] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=349 cheap vicodin] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=77 free cingular ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a0c buy wellbutrin] [http://students.hsc.unt.edu/housing/item.cfm?type=2860 adipex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=223 midi ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=126 cheap sildenafil] [http://library.cshl.edu/wp/vb/member.php?u=1383 free sagem ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=197 flexeril online] [http://students.hsc.unt.edu/housing/item.cfm?type=2898 free free ringtones] [http://wc1.worldcrossing.com/WebX/.1de609ff free real ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30336 free qwest ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=343 cialis online] [http://students.hsc.unt.edu/housing/item.cfm?type=2924 ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=183 albuterol] [http://library.cshl.edu/wp/vb/member.php?u=1342 cheap phentermine] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=220 real ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2883 lipitor online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=203 funny ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=73 cheap ativan] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=218 free qwest ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=202 free tracfone ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=96 cheap levitra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=105 mp3 ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1375 real ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30238 phentermine online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30355 free punk ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30349 free cingular ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=199 music ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2914 free sony ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e7 diazepam online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30315 buy lortab] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=143 free wwe ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2906 free samsung ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=174 cheap cyclobenzaprine] [http://wc1.worldcrossing.com/WebX/.1de60a03 sony ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2857 ativan online] [http://wc1.worldcrossing.com/WebX/.1de60a2d valium online] [http://students.hsc.unt.edu/housing/item.cfm?type=2856 cialis online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30242 fioricet online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=160 adipex online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=232 sony ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2900 free funny ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2870 cheap xenical] [http://news.engin.brown.edu/forums/thread-view.asp?tid=150 order tramadol] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30332 free tracfone ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30354 alltel ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=202 nokia ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=134 tramadol online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=167 carisoprodol online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30269 wellbutrin online] [http://students.hsc.unt.edu/housing/item.cfm?type=2893 cheap hgh] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=219 viagra online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30240 carisoprodol online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30316 cheap vigrx] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30351 sonyericsson ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30350 sagem ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=332 tramadol online] [http://students.hsc.unt.edu/housing/item.cfm?type=2925 free mtv ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2915 mono ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30359 free wwe ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2849 but valium] [http://students.hsc.unt.edu/housing/item.cfm?type=2867 cheap hydrocodone] [http://wc1.worldcrossing.com/WebX/.1de60a16 online levitra] [http://library.cshl.edu/wp/vb/member.php?u=1379 music ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2919 sharp ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30338 motorola ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=174 wellbutrin online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=84 diethylpropion online] [http://students.hsc.unt.edu/housing/item.cfm?type=2882 zoloft online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=217 punk ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=220 punk ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=86 cheap fioricet] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=186 jazz ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30253 cheap norco] [http://students.hsc.unt.edu/housing/item.cfm?type=2920 midi ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=164 buy alprazolam] [http://students.hsc.unt.edu/housing/item.cfm?type=2884 cheap celexa] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=240 soma online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=347 adipex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=180 buy prozac] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=353 online levitra] [http://wc1.worldcrossing.com/WebX/.1de60a33 cheap prozac] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=179 cheap fioricet] [http://students.hsc.unt.edu/housing/item.cfm?type=2926 free jazz ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1384 free sonyericsson ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=213 prozac online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=233 sonyericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de609de alprazolam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=183 cheap hgh] [http://news.engin.brown.edu/forums/thread-view.asp?tid=160 ativan online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=217 free mono ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=226 cheap tramadol] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=148 order zyban] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=225 free wwe ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1385 sony ericsson ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=108 nexium online] [http://wc1.worldcrossing.com/WebX/.1de60a32 zoloft] [http://students.hsc.unt.edu/housing/item.cfm?type=2854 cheap ultram] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=358 free mp3 ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1359 ambien online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=334 order valium] [http://news.engin.brown.edu/forums/thread-view.asp?tid=173 xenical online] [http://library.cshl.edu/wp/vb/member.php?u=1371 funny ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2895 hoodia online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=191 lortab online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30353 free mono ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=68 buy adipex] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=194 free midi ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a34 sony ericsson ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2847 tramadol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=335 buy phentermine] {{Greasemonkey Manual TOC}}
__NOTOC__
{{Greasemonkey Manual TOC}}


= Why a Special Environment? =
<div style="border: 3px dotted; color: red; font-size: 1.2em; padding: 0.5em; margin: 1em; text-align: center">
Warning: This page is out of date and does not accurately describe Greasemonkey 4.0.
</div>
 
== Why a Special Environment? ==


When [[Greasemonkey]] executes a [[user script]] it does so in a special [[sandbox]] environment.
When [[Greasemonkey]] executes a [[user script]] it does so in a special [[sandbox]] environment.
[[Greasemonkey]] takes advantage of a Firefox feature called [[XPCNativeWrappers]] to insulate the [[user script]] from the content web page, which it references.
[[Greasemonkey]] takes advantage of a Firefox feature called [[XPCNativeWrappers]] to insulate the [[user script]] from the content web page, which it references.


Although this makes it more difficult, or impossible, to do certain things in your script, it is a necessary evil.
Although this makes it more difficult, or impossible, to do certain things in a script, it is a necessary evil.
Earlier [[version history|versions]] of [[Greasemonkey]] had no such sandbox, and as a result, [[security]] holes were uncovered.
Earlier [[version history|versions]] of [[Greasemonkey]] had no such sandbox, and as a result, [[security]] holes were uncovered.


[[Greasemonkey]] provides certain enhanced [[Greasemonkey Manual:APIs|APIs]] to the [[user script]], so that it may accomplish things that a regular web page's javascript cannot.
[[Greasemonkey]] provides certain enhanced [[Greasemonkey Manual:APIs|APIs]] to the [[user script]], so that it may accomplish things that a regular web page's JavaScript cannot.
This useful feature has enabled some of the most popular scripts.
This useful feature has enabled some of the most popular scripts.
Unfortunately, when abused, these powerful features can create serious problems.
Unfortunately, when abused, these powerful features can create serious problems.
Line 15: Line 20:


Luckily, for almost all the difficulties that the sandbox environment provides, there are ways to still accomplish the desired goal.
Luckily, for almost all the difficulties that the sandbox environment provides, there are ways to still accomplish the desired goal.
The article [http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html Avoid Common Pitfalls in Greasemonkey] does a wonderful job explaining what the most common snags are, and for each one explains the way to work around the problem.
The article [[Avoid Common Pitfalls in Greasemonkey]] does a wonderful job explaining what the most common snags are, and for each one explains the way to work around the problem.
It is essential reading for any script author.
It is essential reading for any script author.


Line 22: Line 27:
This is the raw, un-sandboxed "window" of the content page.
This is the raw, un-sandboxed "window" of the content page.
Certain limited tasks can only be accomplished by referencing this raw window directly, but be warned!
Certain limited tasks can only be accomplished by referencing this raw window directly, but be warned!
Javascript is a complicated and intricate language, even the most basic operations can be redefined by the content page to perform other actions.
JavaScript is a complicated and intricate language, even the most basic operations can be redefined by the content page to perform other actions.
The sandbox environment is provided for your safety, and the safety of any user of your script.
The sandbox environment is provided for the safety of any user of a script.
If at all possible, use of [[unsafeWindow]] should be avoided.
If at all possible, use of [[unsafeWindow]] should be avoided.


= What's Missing? =
== What's Missing? ==
 
Depending on the usage, the special [[Greasemonkey]] environment may seem perfectly normal, or excessively limiting.


Depending on your usage, the special [[Greasemonkey]] environment may seem perfectly normal, or excessively limiting.
The Greasemonkey environment is a vanilla [[XPCNativeWrapper]] of the content window, with only certain extra bits added in to emulate a normal environment, or changed.  Specifically:
The Greasemonkey environment is a vanilla [[XPCNativeWrapper]] of the content window, with only certain extra bits added in to emulate a normal environment, or changed.  Specifically:


* <code>window</code> is an [[XPCNativeWrapper|XPCNW]] of the content window.
:* <code>window</code> is an [[XPCNativeWrapper|XPCNativeWrapper]] of the content window.
** <code>document</code> is the document object of that (XPCNW) window object.
:* <code>document</code> is the document object of the XPCNativeWrapper window object.
* [http://www.xulplanet.com/references/objref/XPathResult.html XPathResult] is added, so that <code>document.evaluate()</code> works.
:* [[XPathResult]] is added so that <code>document.evaluate()</code> works.
* The entire script is contained inside an [http://en.wikipedia.org/wiki/Anonymous_function anonymous function], to guarantee each script is isolated from any other.
:* Unless the [[Metadata Block#.40unwrap|@unwrap]] metadata imperative is present in the user script header, the entire script is wrapped inside an [http://en.wikipedia.org/wiki/Anonymous_function anonymous function], to guarantee the script's identifiers do not collide with identifiers present in the Mozilla JavaScript sandbox. This function wrapper captures any function definitions and <code>var</code> variable declarations made (e.g. <code>var i = 5;</code>) into the function's local scope. Declarations made without <code>var</code> will however end up on the script's <code>this</code> object, which in Greasemonkey is the global object, contrary to in the normal browser object model, where the <code>window</code> object fills this function. In effect, after <code>i = 5;</code>, the values of <code>window['i']</code> and <code>window.i</code> remain undefined, whereas <code>this['i']</code> and <code>this.i</code> will be 5. See also: [[Global object]]
** This means <code>window</code> is not the normal "global" scope. After <code>var i=5;</code>, <code>window['i']</code> is not 5, it is not even set!
:* In order to access variables on the page, use the [[unsafeWindow]] object. To use values defined in a script, simply reference them by their names.


Since Mozilla provides a rich environment, there are a wide variety of things that have ''not'' been imported from the general content scope into the Greasemonkey sandbox.
Since Mozilla provides a rich environment, there are a wide variety of things that have ''not'' been imported from the general content scope into the Greasemonkey sandbox.
Including, but not limited to:
Including, but not limited to:


* [http://developer.mozilla.org/en/docs/DOM:document.createTreeWalker document.createTreeWalker]
:* [https://developer.mozilla.org/en/DOM/document.createTreeWalker document.createTreeWalker]
* [http://www.xulplanet.com/references/objref/SOAPCall.html SOAPCall]
:* [http://www.xulplanet.com/references/objref/SOAPCall.html SOAPCall]


And so on.
The more esoteric the method, the ''less likely'' that it has been included in the Greasemonkey sandbox.
The more esoteric the method, the less likely that it has been included in the Greasemonkey sandbox.


= See also =
== See also ==


* Wikipedia: [http://en.wikipedia.org/wiki/Sandbox_(computer_security) Sandbox]
* Wikipedia: [http://en.wikipedia.org/wiki/Sandbox_(computer_security) Sandbox]
* [[Security]]
* [[Security]]
* [http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html Avoid Common Pitfalls in Greasemonkey]
* [http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html Avoid Common Pitfalls in Greasemonkey]

Latest revision as of 16:18, 3 November 2017


Greasemonkey Manual
Using Greasemonkey
Installing Scripts
Monkey Menu
Getting Help
User Script Authoring
Editing
Environment
API

Warning: This page is out of date and does not accurately describe Greasemonkey 4.0.

Why a Special Environment?

When Greasemonkey executes a user script it does so in a special sandbox environment. Greasemonkey takes advantage of a Firefox feature called XPCNativeWrappers to insulate the user script from the content web page, which it references.

Although this makes it more difficult, or impossible, to do certain things in a script, it is a necessary evil. Earlier versions of Greasemonkey had no such sandbox, and as a result, security holes were uncovered.

Greasemonkey provides certain enhanced APIs to the user script, so that it may accomplish things that a regular web page's JavaScript cannot. This useful feature has enabled some of the most popular scripts. Unfortunately, when abused, these powerful features can create serious problems. No exploits were ever uncovered in the wild, but the potential was too great, so the sandbox environment was created.

Luckily, for almost all the difficulties that the sandbox environment provides, there are ways to still accomplish the desired goal. The article Avoid Common Pitfalls in Greasemonkey does a wonderful job explaining what the most common snags are, and for each one explains the way to work around the problem. It is essential reading for any script author.

Finally, of note is the unsafeWindow object present in the sandbox. As the name implies, use of this object is unsafe! This is the raw, un-sandboxed "window" of the content page. Certain limited tasks can only be accomplished by referencing this raw window directly, but be warned! JavaScript is a complicated and intricate language, even the most basic operations can be redefined by the content page to perform other actions. The sandbox environment is provided for the safety of any user of a script. If at all possible, use of unsafeWindow should be avoided.

What's Missing?

Depending on the usage, the special Greasemonkey environment may seem perfectly normal, or excessively limiting.

The Greasemonkey environment is a vanilla XPCNativeWrapper of the content window, with only certain extra bits added in to emulate a normal environment, or changed. Specifically:

  • window is an XPCNativeWrapper of the content window.
  • document is the document object of the XPCNativeWrapper window object.
  • XPathResult is added so that document.evaluate() works.
  • Unless the @unwrap metadata imperative is present in the user script header, the entire script is wrapped inside an anonymous function, to guarantee the script's identifiers do not collide with identifiers present in the Mozilla JavaScript sandbox. This function wrapper captures any function definitions and var variable declarations made (e.g. var i = 5;) into the function's local scope. Declarations made without var will however end up on the script's this object, which in Greasemonkey is the global object, contrary to in the normal browser object model, where the window object fills this function. In effect, after i = 5;, the values of window['i'] and window.i remain undefined, whereas this['i'] and this.i will be 5. See also: Global object
  • In order to access variables on the page, use the unsafeWindow object. To use values defined in a script, simply reference them by their names.

Since Mozilla provides a rich environment, there are a wide variety of things that have not been imported from the general content scope into the Greasemonkey sandbox. Including, but not limited to:

The more esoteric the method, the less likely that it has been included in the Greasemonkey sandbox.

See also