
I want to talk about the convenient and unnecessarily rarely used innovations GreaseMonkey version 0.8, which has been available for more than a year. They will help make custom scripts more modular, and most importantly, they will enable us to use jQuery without tweaks, which will allow us to “write less and do more”. We are talking about the new meta-parameters of user scripts:
@resource and
@require .
Consider a small example:
habratest.user.js . The script adds the GreaseMonkey logo to the Habrahabr menu, when clicked, the text “Hello, world!” Will be displayed, taken from an external file.
// ==Userscript==<br>// @name habratest<br>// @namespace habratest<br>// @include http://habrahabr.ru/*<br>// @include http://*.habrahabr.ru/*<br>// @resource gm_logo http://veg.slutsk.net/habr/greasemonkey.png<br>// @resource hello http://veg.slutsk.net/habr/hello.txt<br>// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js<br>// ==/Userscript== <br><br>$( document ).ready( function ()<br>{<br> $( "ul.panel-nav-top" ).append( '<li style="float: right;" id="habratest"></li>' );<br> $( "#habratest" ).append( '<img src="' +GM_getResourceURL( "gm_logo" )+ '">' );<br> $( "#habratest img" ).click( function ()<br> {<br> alert(GM_getResourceText( "hello" ));<br> });<br>});
@resource allows
you to bind external files to a user script: images, text files, etc. - all that may be needed at run time. In the example, “gm_logo” is the name of the resource. The GM_getResourceURL function is used to get a local link to a file (for example, a picture), GM_getResourceText - to get text from a file (a file can contain, for example, a large piece of HTML). Both functions take as the parameter the name of the resource. The advantage of using resources instead of direct links to the server is that the specified files are downloaded once during the installation of the script and in the future are always taken from the local machine.
Much more interesting is
@require , which allows you to connect external libraries to the script. You can of course fence any code with the dynamic creation of a script tag, rather nontrivial ways to expect it to load. But in this case, the library file will be requested from the server every time a new page is opened in the browser. These shortcomings are devoid of @require - the file will be requested only once during the installation, and most importantly - it will be guaranteed to be executed before the execution of your custom script begins.
As a useful example, I suggest looking at the script code of
yandexru.user.js , which on the search results pages adds the “Search without exception” option to the query form, the “Search this site” field, and also displays whether the set region affected the search results.
')
Unfortunately, today such things can only be done in Firefox. It makes sense to hint to the developers of GreaseMonkey analogues for other browsers, so that they think about the support of such useful functions. But if you are writing a script for personal use, it is quite possible to use these capabilities today.