Tue, 14 Apr 09

Generating and Inserting a Rel=Canonical Link Into Pages With Firefox and Greasemonkey

I’ve been thinking about this for over a year but have only recently got around to doing something about it. I’ve also chosen to use rel=canonical rather than rel=bookmark.

The magic is all in this greasemonkey script. It uses little rule objects to generate the canonical urls for, at the moment, ebay, google and theyworkforyou and insert those urls into the page as a link element with rel=canonical. Combining my script with the show canonical greasemonkey script (found on the tripwolf blog) allows me to easily visit and use the canonical url in emails and the like.

You should just about be able to see the relatively short canonical url compared to the large url generated by my searching from the firefox search box.

I hope to expand the set of rules (if you wanted to create one right now I’d suggest looking at the source of the script, the rules are defined towards the end of the script, and the rule tests) and possible have some central repository of rules that the script could use. If you know of some sites that you’d like to see canonical urls for, but don’t want to create the rule, then let me know and I’ll see if I can create it for you.