Emulate Greasemonkey functions

  • Published: 2005-05-30 23:16
  • Modified: 2005-10-27 12:57
  • Author: TarquinWJ
  • Requires: Opera 8.0
  • Install as: Opera User JS

Learn how to install scripts

Report a bug

Opera’s User JavaScript event listeners and magicvariables/functions may offer several advantages over Greasemonkey’s more simplistic approach, but Greasemonkey offers a few features that Opera’s User JavaScript does not have. It can store persistent data, add temporary menu entries, and also do a few other things.

This User JavaScript attempts to replicate these functions in Opera. It is not perfect, but it should allow you to use a few more Greasemonkey scripts in Opera. The file name starts with ‘aa’ to make sure it loads before all other regular Opera User JavaScripts - no promises, but it should always load before Greasemonkey scripts. It provides these functions:

Stores the variable value as a cookie so it can be retrieved later. With Greasemonkey, these would be available to the script no matter what site it is being used on. With this script, it is available only for the current domain (as with normal cookies). Unlike Greasemonkey, which can store only numbers, strings and boolean values, this script can store regular expressions, dates, functions, arrays and objects as well.
Returns the previously stored variable using the given key. If none, it returns the defaultValue, or undefined if no defaultValue is provided.
Deletes the stored value - this is not a normal Greasemonkey function, but it just seemed sensible to provide a way to delete them again…
Adds a menu item with the specified title that runs the specified function when clicked. Unlike Greasemonkey (which uses the browser’s own menu), this script places a menu in the top right corner of the page when needed - it is even keyboard accessible, using spatial navigation.
Synonym for the XMLHttpRequest constructor. With Greasemonkey, this has no cross domain restrictions. I cannot replicate that behaviour here.
Synonym for window.opera.postError.

Additionally, it maps window._content to window, as this is unnecessarily used by many Greasemonkey scripts.

Note: many Greasemonkey scripts also use XPath (whether they actually need to or not is beside the point). If you want to use these scripts, you can either use Opera 9, which natively supports XPath, or use Cameron McCormack’s excellent script library, which should provide the necessary functionality in Opera 8.