Whenever you see a tip or warning throughout this book, you can click on the tip icon to jump to the list of all tips. |
-
Many user scripts are available at the Greasemonkey script repository, although there is no requirement to list your scripts there. You may host (and users may install) your script from anywhere. You don't even need a web server; you can install a user script from a local file. 1.3. Installing a user script
-
You can specify the items of your user script metadata in any order. I like
@name
,@namespace
,@description
,@include
, and finally@exclude
, but there is nothing special about this order. 2.2. Describing your user script with metadata -
When you click Edit in the “Manage User Scripts” dialog, you are “live” editing a copy of your script that is buried deep inside your Firefox profile directory. I've gotten into the habit, once I've finished a “live” editing session, of going back to my text editor one last time and selecting File → Save as..., and saving the user script in another directory. While it's not necessary (Greasemonkey only pays attention to the copy in your profile directory), I prefer to keep the “master copy” of my scripts in another directory with the rest of my work. 2.4. Editing your user script
-
In JavaScript Console, you can right-click (Mac users control-click) on any line and select Copy to copy it to the clipboard. 3.2. Logging with
GM_log
-
Once you have a reference to an element (like
thisElement
), you can usethisElement.nodeName
to determine its HTML tag name. If the page is served astext/html
, the tag name is always returned as uppercase, regardless of how it was specified in the original page. However, if the page is served asapplication/xhtml+xml
, the tag name is always lowercase. I always usethisElement.nodeName.toUpperCase()
and forget about it. 4.6. Doing something for every element with a certain attribute -
Inserting new content before
someExistingElement.nextSibling
will work even ifsomeExistingElement
is the last child of its parent (i.e. it has no next sibling). In this case,someExistingElement.nextSibling
will returnnull
, and theinsertBefore
function will simply append the new content after all other siblings. (If this doesn't make any sense to you, don't worry about it. The point is that this example will always work, even when it seems like it shouldn't.) 4.8. Inserting content after an element -
If all you want to do is remove ads, it's probably easier to install AdBlock and import an up-to-date filter list than to write your own user script. 4.9. Removing an element
-
Use the
data:
URI kitchen to construct your owndata:
URLs. 4.12. Adding images without hitting a central server -
You can use the
addGlobalStyle
function to style elements that you insert into a page, or elements that were part of the original page. However, if you are styling existing elements, you should use the! important
keyword for each rule you define to ensure your styles override the rules defined by the original page. 4.13. Adding CSS styles -
Zapping and replacing
document.body.innerHTML
does not change everything about the page. Anything defined in the<head>
of the original page will still be in effect, including the page title, CSS styles, and scripts. You can modify or remove these separately. 4.16. Post-processing a page after it renders -
A
submit
event fires when a user submits a form in the usual way, i.e. by clicking the form's Submit button or hittingENTER
within a form. However, thesubmit
event does not fire when the form is submitted via a script callingaForm.submit()
. Therefore you need to do two things to capture a form submission: add an event listener to capture tosubmit
event, and modify the prototype of theHTMLFormElement
class to redirect thesubmit()
method to your custom function. 4.22. Overriding a built-in Javascript method -
You can see all the stored configuration values by visiting
about:config
and filtering ongreasemonkey.scriptvals
. GM_getValue -
Whenever you see a list of “further reading” links throughout this book, you can click on the “Further reading” header to jump to the list of all further reading links. List of “further reading” links
-
Whenever you see a tip or warning throughout this book, you can click on the tip icon to jump to the list of all tips. List of tips
-
Whenever you see a video link throughout this book, you can click on the icon to jump to the list of all videos. List of procedures