views:

30

answers:

1

I'm trying to prevent my Greasemonkey script from executing within IFRAMEs. I'm doing so by using if (window.top != window.self) return;. As soon as I insert that line right after the metadata header, the error console throws out a "Security Manager vetoed action" indicating this exact line of code. There's no additional information available.

I'm using Firefox 3.6.10 and the latest Greasemonkey extension. Oh, I'm new to user scripts but even after some time looking for an answer I didn't find anything at all.

+1  A: 

Greasemonkey has an own API, which allows persistent storage and cross-site HTTP requests. For this reason, scripts are executed in a sandbox and this API cannot abused.

To make your code work, use:

if(usnafeWindow.top != unsafeWindow.self) return;

Please note the unsafe part, you may want to review these pages:

Alternatively, wrap the code in a <script> tag:

(function(f){var d=document,s=d.createElement('script');s.setAttribute('type','application/javascript');s.textContent = '('+f.toString()+')()';(d.body||d.head||d.documentElement).appendChild(s);s.parentNode.removeChild(s)})(function(){
   /* code here */
}
Lekensteyn
Alright, that explains a few things. Generally, it looks like it's a good idea to avoid unsafeWindow but in this case there's no alternative. Works like a charm now. Thank you.
JC2k8
As an alternative, you can put your script in a script tag and append that to the document. In that way, you've no longer access to the GM API, and the script behaves as any script on the page. See example in my answer.
Lekensteyn