[Greasemonkey] Switching to Webpage Context
Nikolas Coukouma
lists at atrus.org
Mon Jan 9 11:25:55 EST 2006
Vincent Chute wrote:
> I tried using unsafeWindow but I didn't manage to get it working
> successfully and whilst I could probably get
> document.createElement("script"); to work I don't fancy the pain of
> maintaining nearly 200 lines of sc.innerHTML += "...." especially with
> the effective loss syntax highlighting.
>
> Are there any techniques that I am missing?
> Anyone else interested in this ability?
Assuming you actually need to do this ...
I think injecting as a script element is the only way to ensure proper
scope stripping. Eval-ing in a window context would also work, I
believe, but would be harder to maintain.
I think you should write the script you want to inject in a separate
file and then convert it to a data URL for easy injection and
maintenance. Ian Hixie has written an excellent tool for this:
http://software.hixie.ch/utilities/cgi/data/data
I recommend base64 encoding for binary data (e.g. images) because it is
much more efficient (33% smaller, IIRC). Scripts are mostly text, so I
think you'll probably be better off with regular URI encoding.
Cheers,
-Nikolas
More information about the Greasemonkey
mailing list