Opera User JS and GreaseMonkey interoperability
I’ve written a User JS with enhancements for AvidGamers. However, GreaseMonkey automatically wraps the entire script in an anonymous function. To create functions in your User JS, which are accessible in the page itself (by elements you inserted), you need to attach it to the window object.
All of that would be great, but it didn’t work. Digging through the GreaseMonkey website I eventually discovered that this had to do with some kind of security bug (the link to Mark Pilgrims guide as the way to learn about GreaseMonkey didn’t help much).
To make a long story short, I used the following to create Opera User JS and Greasemonkey interoperability.
// for GreaseMonkey compatibility
if (typeof unsafeWindow != 'undefined') window = unsafeWindow;
// how to add variables
window['some_variable'] = 'Some text';
// how to add functions
window.some_function = function (var1, var2) {
// do something
}
To keep your code clean it’s a good idea to add in some extra variables for reference inside your code.
var some_variable = window[some_variable];
Regardless of security issues which removed direct access to the window object, it seems a tad weird to me to always wrap the code in an anonymous function. Should that not be up to me, the script author, to decide? Opera’s implementation appears to be just perfect…
I have been using the UserJS and User Styling features of Opera a lot lately. I have not used Greasemonkey, but it sounds like I wouldn’t want to. 🙂
July 13, 2006 @ 12:32Permalink
Ethan Poole