As mentioned in the
previous article , userscripts are supported by
all modern browsers . And even somehow supported in
IE7 and higher .
In this article we will talk
about browsers :
- Restrictions
- Problems
- Extensions to run userscripts
- Installing user scripts
Couple of words about engines
The quality of user scripts support is on a different level in different browsers. Best of all, support for userscripts is done in
Firefox and
Chrome browsers.
These browsers provide less user-friendly interfaces for managing userscripts.
')
The most severe restrictions on user scripts are imposed by
Chrome . But almost all of these limitations cost the
packaging of the script into a simple extension . Details of this process will be discussed in the
next article .
Now let's talk more about the support of userscripts in individual browsers.
Firefox Support
Mozilla Firefox supports userscripts after installing the
GreaseMonkey extension (in Russian slang -
monkey ) or
Scriptish .
After installing the extensions, firefox gets a truly powerful support for user scripts.
The information discussed below applies primarily to GreaseMonkey (this extension was the first).
Installation: userscripts are installed by simply dragging the script file into the browser.
Extensions: GreaseMonkey ,
Scriptish .
Management: userscripts can be disabled and deleted in the menu added by extensions.
Features:- There is a powerful library GM API.
- The native global window object is replaced.
- Objects of a native window, for example window.page_defined_var , are accessible via an unsafe link unsafeWindow .
- Cross-domain requests are available through the XmlHttpRequest interface.
- Analogue of globalStorage is available.
- The ability to connect third-party libraries (for example, jQuery) is available.
- Because of the security of the GreaseMonkey plugin, some features require specific hacks .
For example, running GM_setValue (...) in an ajax request handler can cause an error accessing the GM API methods. To handle this situation, use the construction of the form setTimeout (function () {GM_setValue (...)}, 0) .
- There is no debugging of userscripts. Even Firebug will not help here.
GreaseMonkey adds the so-called
GM API - a set of javascript functions that add functionality to userscripts.
Of the most requested features that the GM API provides, it is worth mentioning:
- Object of the “native window” of the unsafeWindow page (allows replacing functions on the page, using the existing libraries on the page)
- Cross Domain HttpXmlRequest: GM_xmlhttpRequest
- Analog globalStorage (localStorage without binding to the domain): GM_setValue , GM_getValue and GM_deleteValue
Learn more about the GM API and GreaseMoneky functionality at
http://wiki.greasespot.net .
Historically, all userscripts were written under the Firefox browser precisely because of the presence of a convenient plugin.
This is due to the fact that all browsers that support user scripts additionally follow the
basic rules of the GreaseMonkey specification for parsing metadata .
This means that all scripts that were written under GreaseMonkey
will be installed and run in browsers that support user scripts (with a minimum of modifications).
Unfortunately, no browser other than Firefox provides the GM API. This sad fact forces us to use GM API emulations through extensions or additional user scripts.
In the case of developing a user script from scratch, I consider it preferable
to abandon the GM API emulation and use the “bikes” of our own production. This allows you to reduce the number of user script dependencies, which, in turn, allows you to develop within the framework of the
concept of a single file : you
will need to modify only one file; the user only needs one file to run the user script.The concept of a single file can significantly reduce the complexity of support and cross-browser development of user scripts!
Chrome support
Google Chrome supports native scripts natively, i.e. does not require installation of plug-ins / extensions. It is possible (
sometimes necessary ) to pack a user script into an extension.
Installation: userscripts are installed by simply dragging the script file into the browser.
Extensions: not needed. There is an extension
Tampermonkey , which simplifies working with scripts.
Management: userscripts, as well as extensions, can be disabled and deleted (
Settings -> Tools -> Extensions ).
Features:- Document.frames [i] .parent is not available ( allowed in extension ).
- Objects of the native window are not available, for example window.page_defined_var (it will not be possible to replace the functions of the page, JSONP in userscript is also eliminated)
- Cross domain queries are not available ( allowed in extension )
- unsafeWindow is available, but does not carry the functionality of the GM API.
- Convenient native debug userscripts and extensions.
An extension is an archive packed by the browser itself (
Settings -> Tools -> Extensions -> Package Extension ).
In addition to the user script file, the extension should contain:
- manifest.json - file description of the extension. Analog of user metadata.
- background.html - file "background page" extensions. Gives access to the extensions API via calling chrome.extension. *
Packing user scripts in an extension allows you to
bypass many of the restrictions imposed by Google Chrome on user scripts.
Important: in fact, the extension and user scripts are different concepts. And if you approach the issue strictly, it is worth talking about the development of simple extensions for Chrome.
In the case when the user script requires non-standard, "heavy" functionality, it requires packing in the extension.
To pack a user script into an extension, you need to do additional steps
once . All subsequent development will be carried out within the framework of the
concept of a single file .
Opera support
Opera supports native scripts natively, but does not provide any friendly user interface for managing scripts. Such an interface is available in the UJS Manager extension.
Extensions:
UJS Manager ,
UserJS Manager .
Installation: user scripts are installed in a user-defined folder of user scripts. Its location can be set in the browser settings:
Settings -> General Settings -> Advanced -> Content -> Customize JavaScript . The folder with userscripts
should not contain spaces .
Management: user scripts can be disabled by deleting or moving the script file from the user scripts folder.
Features:- Users scripts run "as is", without turning into a closure, thereby littering the window's global scope .
- Objects of the native window are available, for example window.page_defined_var .
- Available specific events of the Opera browser, for example BeforeScript .
- Cross-domain queries are not available (it is managed using special events )
- unsafeWindow is unavailable.
- Scripts are run in alphabetical order.
IE support
IE7, IE8, IE9 support user scripts when using the
Trixie plugin.
In addition, there is the more advanced
IE7Pro plugin. In addition to user scripts, IE7Pro has many other useful features.
Important: If you do not disable additional “pribluda” in IE7Pro, then the plugin can significantly slow down the browser, especially on heavy pages.
Extensions: Trixie ,
IE7Pro .
Installation: userscripts are installed in the Trixie user scripts folder
(C: / Program Files / Bhelpuri / Trixie / Scripts) or IE7Pro
(C: / Program Files / IEPro / userscripts) .
Management: each plug-in has a user interface for managing userscripts.
Features:- Users scripts run "as is", without turning into a closure, thereby littering the window's global scope .
- Objects of the native window are available, for example window.page_defined_var .
- Cross domain queries are not available ( you can work around in ways similar to those used in javascript development: JSONP, easyXDM xdr, etc. )
- unsafeWindow is unavailable.
- Both Trixie and IEPro have a different script loading model than the GreaseMonkey model. Scripts are not restarted when the page is updated via Ctrl + R or Ctrl + F5 . In addition, the scripts are loaded by window.onLoad .
- In order for Trixie to see the script, the script must have metadata and must have the @namespace directive (see previous article ) .
- In order for IE7Pro to see the script, the script must have the extension .ieuser.js .
- Both plug-ins have problems running in IE9 under Windows 7x64.
I have seen more frequently reported IE7Pro not running.
As you can see, with the launch of the scripts in IE, things are lousy. It remains to be glad that such an opportunity exists at all.
Important: Both plugins can exist in the system at the same time, without interfering with each other.
Important: Considering the above, I always suggest my users to use
Trixie .
Safari Support
Unfortunately, I was not able to work with this browser. I will be glad to any explanations in the comments!Rumor has it that Safari needs
SIMBL and
GreaseKit plugin.
Support in Mobile Safari and other browsers
Due to the lack of support for userscripts in less popular browsers and mobile browsers, userscripts will have to be provided in the form of a
bookmarklet .
Correspondingly, in this case, metadata is not needed, and the bookmarklet
will have to be run by hand after each refresh.
Lastly
If you stick to the
concept of a single file , then the process of creation follows this path:
- Take the template.
- Add
water code and metadata. - Create a folder and files for the Chrome extension.
- We pack the extension for Chrome, rename the file for IE7Pro.
- We distribute user scripts / extensions to users.
- ...
- We fix
hands bugs. - Goto 4
List of articles:
- Learning to write userscript
- » Userscripts. Deepen.
- Userscripts. We pack user scripts for Chrome
- Usersctripts. Cross Domain Queries