Cross-browser userscripting

From GreaseSpot Wiki
Jump to: navigation, search

User scripts can be used in other browsers than Firefox. However, the scripting APIs and browser JavaScript support vary. This page documents differences in other environments as compared to Greasemonkey in Firefox.

Other browsers

Google Chrome

Chromium and Google Chrome convert User scripts into Chrome extensions since version 5. A userscript's Metadata block is parsed, and a manifest file is created. A copy of the user script is used as a content script. To discern "real" Chrome extensions from converted user scripts, the "converted_from_user_script" property is added. Because of this distinction, converted user scripts have access to some GM_* methods.

User scripts in Chrome behave similar to ordinary Greasemonkey scripts, with some exceptions:

  • Prior Chrome 13, GM_xmlhttpRequest did not allow cross-origin requests. Since version 13, this is possible, provided that the requested URL matches a @include or @match pattern.
  • unsafeWindow in the converted user script is not equivalent to Greasemonkey's unsafeWindow object. Content scripts in Chrome do not have a direct access to the page's global window object (look here for more information).
  • The only fully working API methods are: GM_addStyle, GM_xmlhttpRequest, GM_log and GM_openInTab.
  • The following methods are defined, but not implemented (when they're called, a message is logged in the console): GM_getValue, GM_setValue, GM_registerMenuCommand.
  • User scripts which implement persistency functions exist, but they cannot emulate the exact behaviour: Cookies, DOM Storage and such are domain-specific. Furthermore, the data is also readable by the page itself, which is a potential security leak.

Another option is to use an external user script manager, such as Tampermonkey, which brings Greasemonkey / Gecko-specific (E4X and for each loops) features to Chrome.

Internet Explorer

See this blog post for some notes.

IEScripts

IEScripts is part of the IE7Pro add-on for Internet Explorer 6 and 7 only. A directory of scripts is available at iescripts.org. Section 5 of their tutorial is entitled "Porting from Firefox GreaseMonkey user scripts" and explains how to compensate for various differences. Even minimal scripts are not immediately compatible between Greasemonkey and IEScripts since the latter uses the .ieuser.js extension rather than .user.js for user scripts; scripts must be renamed to be installed in the other user script engine.

Trixie

Trixie Homepage

Opera User JavaScript

User JavaScript is supported natively since Opera 8 beta 3. It works in Opera Mobile and the desktop version of Opera, but not Opera Mini. User JavaScript executes in the same context as the web page (unlike Greasemonkey) and GM_ API calls are not supported, but Greasemonkey @include / @exclude metadata is honoured for scripts with .user.js extensions. Access to other Opera-specific functions is provided via a window.opera object.

The Opera User JavaScript page explains how to enable User Javascript in the desktop version, including for secure connections (user scripts are disabled by default for https:). For Opera Mobile, navigate to the User Prefs section of opera:config, then tick "Always Load User JavaScript" and "User JavaScript". Enter the path to a folder (not a file) in the "User JavaScript File" field, where Opera will search for scripts, then hit the Save button at the bottom of the page.

Violentmonkey

Violentmonkey extension for Opera supports some Greasemonkey API and Metadata Block. The source available on GitHub.

Safari

GreaseKit

GreaseKit is a User script manager for Safari 4 for Mac. Unfortunately, GreaseKit removed all GM_* functions in version 1.4 (Possible workaround). The GM_addStyle and GM_log methods were re-added in version 1.7 though [1]. The most recent version was released in 2008, so features which were introduced in Greasemonkey 0.9+ (such as @match) are not included.

NinjaKit

In Safari 5 for Mac/Windows, the NinjaKit extension is preferred over GreaseKit, since it uses Safari 5-specific APIs. The developer published NinjaKit's source code on Github.

English documentation (based on experience and the source code):

  • Download the extension, and install it (by opening NinjaKit.safariextz file with Safari 5+).
  • After installation, a new button appears at the left side of the location bar.
  • To edit/delete/disable scripts, click on the button. User scripts can be managed at the Script tab.
  • By clicking on any link which ends with .user.js, the "Install Greasemonkey" dialog is triggered. These links have to be present when the document finishes loading.[2]).

Alternatives

Without a user script approach, developers still can modify websites other ways:

  • Enter javascript: URLs in the Location Bar or using bookmarklets.
  • Bookmarklets are javascript: URLs which have been bookmarked and can be very long. They can execute arbitrary JavaScript on any page, but they require a user to click them, rather than running automatically. They cannot make cross-domain XHRs, but they can preserve state via cookies, or by JSONP.
  • Use the browser's DOM Inspector. Firebug has a good tools for disabling or altering CSS/HTML elements.
  • Use a full-fledged extension that serves the same purpose (see also docs for Firefox' Jetpack, Chrome, Safari, Opera, or cross-browser frameworks such as Kango).
  • Use a local HTTP proxy that modifies the HTML, such as Proxomitron.

Related Software

See: http://en.wikipedia.org/wiki/List_of_augmented_browsing_software

Firefox Extensions

  • iMacros for Firefox is a Firefox extension that allows the user to record and replay so called "Internet Macros" for web automation, web scraping or web testing.
  • Stylish is a Firefox that allows for client-side manipulation of webpage content through Cascading Style Sheets.
  • Firebug is a developers extension that allows arbitrary real time changes to a page's DOM
  • Mason is a simple yet powerful system which can activate and interact before resources are even loaded. Requires knowledge of RegExp.


References

  1. GreaseKit Changes
  2. "Links have to be present when the document finishes loading." NinjaKit's source code on Github, grease.js, Line 93 and line 52