How to compile greasemonkey user script

Source: Internet
Author: User

1. The suffix of each user script must be. User. JS, for example, helloworld. User. js. Its content is as follows:

// ==UserScript==// @name          Hello World// @namespace     http://diveintogreasemonkey.org/download/// @description   example script to alert "Hello world!" on every page// @include       *// @exclude       http://diveintogreasemonkey.org/*// @exclude       http://www.diveintogreasemonkey.org/*// ==/UserScript==alert('Hello world!');

 

2. Analyze the user script metadata

Use // = userscript = and // =/userscript = to identify the start of the script, which contains six lines of metadata description. This annotation can be placed anywhere in the script, but it is generally placed at the top of the script.

// @ Name Hello World

This is the name of your script. It will be displayed in the installation dialog box and in the manage user scripts dialog box. This name should be short and can express the meaning of the script. This metadata can be omitted. If omitted, the default value is the script file name minus the. User. js extension.

 

// @ Namespace http://diveintogreasemonkey.org/download/

This metadata is generally a URL used to differentiate scripts with the same name. If you have a domain name, you can use the domain name. Otherwise, you can use the tag: URI. This metadata can also be omitted. If omitted, the domain name of the downloaded script is used by default.

 

// @ Description Example script to alert "Hello world! "On every page

This metadata is used to define readable descriptions. It is recommended that there be no more than two sentences. This information is displayed in the installation dialog box and the manage user scripts dialog box. Of course, this metadata can be omitted. The default value is a null string.

 

// @include       *// @exclude       http://diveintogreasemonkey.org/*// @exclude       http://www.diveintogreasemonkey.org/*

@ Include and @ exclude are used to define the websites that scripts can use. * is a wildcard. The preceding example shows that the script can be used on all sites except http://dive?greasemonkey.org/#and http://www.dive=greasemonkey.org /*. Both metadata can be omitted. If the two metadata are not defined, the script will be used for all sites.

 

3. Write user scripts

alert('Hello world!');

Of course, the above is the true logic of the script. In fact, in order to avoid conflicts between your script program and the original script on the page to be injected, greasemonkey has done a lot of things behind the scenes for you. It wraps your code into an anonymous function, although you can ignore this, there is a potential problem here. The variables or functions you define cannot be called by other scripts. For example:

function helloworld() {    alert('Hello world!');}window.setTimeout("helloworld()", 60);

The above script will certainly report an error ("helloworld is not defined"), because after a user script is executed once, the variable or function will become invalid, this means that when you look forward to using window. when the setTimeout function calls your own function, an error is reported. The solution is to define your function as the properties of the window object, for example:

 

window.helloworld = function() {    alert('Hello world!');}window.setTimeout("helloworld()", 60);

 

Then, this is not the best solution, because it is very likely that you have defined window object attributes with the same name on the page of the script to be injected, which will cause a conflict, the best solution is to define an anonymous function and pass it as a parameter to window. setTimeout. For example:

window.setTimeout(function() { alert('Hello world!') }, 60);

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.