views:

194

answers:

2

Is it possible for the javascript you write for a XUL component to interact with the javascript defined in a webpage?

Eg, if a particular webpage has a dooSomethingNeat() function, can I have a button defined in a XUL overlay execute that function, or does it live in another namespace?

Put another way: if I'm looking to enhance the functionality of a website via my own code, does it make more sense to write a Firefox extension or use something like greasemonkey?

+2  A: 

See my answer to another question here.

  • The webpage code does live in a 'namespace' separate from the scopes the browser code executes in.
  • It doesn't mean you can't access it from an extension, though.
  • On the other hand, running a function in a content page is not very easy to do securely at this moment.
  • Greasemonkey scripts (and ubiquity scripts, which can also interact with web pages) are somewhat easier to develop than extensions, and Greasemonkey already implements the required security precautions to allow you interact with web pages safely.
  • If you want others to use your script, packaging it as a standalone extension lowers the barrier to entry (on the other case, existing GM users may prefer simpler GM scripts to a separate extension).

So if you can implement what you need to do with a GM script or an ubiquity script, I'd say go with it. At least you can start with it, then convert to an extension when you find something you can't do with GM.

If you need features not supported by Greasemonkey or if you just want to try creating an extension, it is also a viable option.

Nickolay
+1  A: 

There is a Greasemonkey-to-firefox-extension "compiler" available, but it isn't up-to-date with the latest GM changes.

However, it does have the basic GM framework for page interaction and security all wrapped up as a standalone extension, ready for you to modify and extend.

Wether to use standalone extension or GM-script depends upon who will be installing this. Will the user-base be willing to install GreaseMonkey, THEN the script? Or is the extension alone enough of an installation barrier?

The GM license does allow for repackaging it with pre-set scripts, I believe, but I can't find back citations for this, at the moment.

Michael Paulukonis
This is for internal use only at my company, so the users will install whatever the heck I tell them to ;)
gct