use Perl Log In
CPANdeps + Greasemonkey
I've just written a quick and dirty Greasemonkey script that adds a link to David Cantrell's excellent CPAN dependencies to any CPAN distribution's page on search.cpan.org.
// ==UserScript==
// @name CPAN Search
// @namespace http://hexten.net/
// @description Add links to CPAN
// @include http://search.cpan.org/
// ==/UserScript==
var new_links = {
'CPAN Dependencies': function(url, name) {
return 'http://cpandeps.cantrell.org.uk/?module='
+ escape(make_module_name(name));
}
};
function canonical_url() {
var permalink = document.getElementById('permalink');
if (permalink) {
return permalink.firstChild.href;
}
return '';
}
function trim_url(url) {
return url.replace(/^http:\/\/[^\/]+\/[^\/]+\//, '').replace(/\/$/, '');
}
function make_module_name(dist_name) {
return dist_name.replace(/-/, '::');
}
function add_links(nd) {
var end = nd.lastChild;
nd.removeChild(end);
var dist_url = canonical_url();
var dist_name = trim_url(dist_url);
// console.log(dist_name + ' ' + dist_url);
var keys = [];
for (var k in new_links) {
keys.push(k);
}
keys = keys.sort();
for (var l = 0; l < keys.length; l++) {
nd.appendChild(document.createTextNode(" ]\n[ "));
var name = keys[l];
var link = document.createElement('A');
link.href = new_links[name](dist_url, dist_name);
link.innerHTML = name;
nd.appendChild(link);
// console.log(name + " " + link);
}
nd.appendChild(end);
}
var rows = document.getElementsByTagName('tr');
if (rows) {
for (var r = 0; r < rows.length; r++) {
var cells = rows[r].getElementsByTagName('td');
if (cells.length == 2 && cells[0].innerHTML == 'Links') {
add_links(cells[1].firstChild);
}
}
}
I'm sure the DOM walking can be improved - but it works. To add other links add them to the new_links hash. Each entry is the anchor text for the link and a function that returns the URL to link to.
Related Stories
The only problem with that is that FireFox is not my main browser. I tend to use FireFox for web development (FireBug++) and Safari for general surfing. Theoretically you can use GreaseMonkey scripts in Safari using CreamMonkey - but I couldn't get that to play nicely with Leopard.
So I've released HTTP::Proxy::GreaseMonkey which builds on BooK's excellent HTTP::Proxy to provide a local proxy that functions like GreaseMonkey.
Version 0.01 of HTTP::Proxy::GreaseMonkey has no support for the GM_* utility functions that the real GreaseMonkey provides - but it works well enough to support my CPAN dependencies user script and probably quite a few other GM scripts. Suggestions are welcome. Suggestions with patches even more so.
Escalation wars... you just have to love'em.
First, David came up with the über-cool CPAN deps page.
Then Andy comes up with a nifty Greasemonkey script to add it to the CPAN distributions main pages.
Then I add a small patch to the script to retrieve some information from the Deps page.
Then David creates an xml interface to CPAN deps, opening the door wide open for Web 2.0 goodiness.
Then (and this is where we are right now) I hack together a new CPAN_Dependencies monkeyscript to take advantage of said xml interface.
This, of course, is nowhere near the end of the story. My new script only scratches the surface of what can be done with the information contained in the xml. As soon as I have some tuits, I'll probably add a way to toggle between showing only the first-level dependencies and all dependencies, and have dependencies color-coded by degree of b0rkage, and whatever bell or whistle I can think of in the meantime.

Ahem (Score:1)
The asterisk is important
Very cool! (Score:1)
And don't forget to add it on Userscripts.org [userscripts.org].
XPath insead of manual DOM walking (Score:1)
Re: (Score:1)
Yes, it’s Firefox (and maybe Opera) only, which supports XPath.
Indeed; in general purpose code you would use jQuery (or some other DOM query library of your preference) to write the same thing with CSS3 selectors. This particular case is not as concise as the XPath version because it needs to check text content, which CSS largely has no means to do.
For comparison’s sake, if written in jQu
Re: (Score:1)
Woops. Replace the remaining
$()calls withjQuery(). I haven’t trained myself out of the habit completely yet.Re: (Score:1)
Re: (Score:1)
Here's the announcement of HTTP::Proxy::GreaseMonkey [perl.org].
Mashup escalation war (Score:1)
I've tacked the following at the end of your userscript:
Re: (Score:1)
Re: (Score:1)
Oh my. On one hand, it should not be impossible, as it's all Javascript and one only has to dig in GreaseMonkey's code to find what's needed. On the other hand, I just peeked at the said GM code, and it's a pretty substancial amount of code. Something tells me it's going to be quiiiite an interesting feature to implement. Good thing that the Christmas vacations and insane quantities of eggnog are just around the corner... :-)
Re: (Score:2)
Re: (Score:1)
Whoa. Your script-fu *is* strong. 8-o
Re: (Score:2)
Re: (Score:1)
Your wish is my command.
Oooh... Very cool... Thanks!
*hack* *hack* *tinker* *hack*
Here! What do you think of this: CPAN_Dependencies [userscripts.org]?
Re: (Score:2)
Re: (Score:1)
Re: (Score:2)
Re: (Score:1)
It doesn't want to install for me (or possibly I just don't know how to install it)
Drat! Hmm... Silly question, but, you do have Greasemonkey installed, right?
if there's anything I can do to the XML to make it work better for you, please let me know.
Thanks! I'll remember that. :-)
I'll add a link to the script from my site shortly.
Excellent. Fame, here I come!
Re: (Score:2)
Re: (Score:1)
But yes, if you want the script to work, you have to install GreaseMonkey, or use the cool proxy that Andy came up with (http://search.cpan.org/~andya/HTTP-Proxy-GreaseMonkey-0.05)
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)