Dive Into Greasemonkey

Teaching an old web new tricks

4.19. Rewriting links

Once you find a link on a page, you can rewrite it by setting its href attribute.

Example: Add a query parameter to the end of a link

This presumes that there is a link with the id "article".

var a = document.getElementById('article');
if (a.href.match(/\?/i)) {
    // link already contains other parameters, so append "&printer=1"
    a.href += '&printer=1';
} else {
    // link does not contain any parameters, so append "?printer=1"
    a.href += '?printer=1';
}
← Getting the current domain name
Redirecting pages →