Log in

View Full Version : Small Greasemonkey Script (Adding Links To Pages)



benslayton
12-21-2007, 05:55 PM
There is a website that I would like to add links to but first it has to grab a link already in the page that contains an ID varible that is usually a 4 number digit. Note I am using greasemonkey...

Here is what I have...

var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; ++i)
{
var linkNode = links[i];

if(linkNode.href.match('http://domain.com/audiosample.aspx?songid=*')){
var result = linkNode.href;

// split the url
var array = result.split("=");
var id = array[array.length-1];

if(id != ""){
// make link to project homepage
var hp = document.createElement('A');
hp.setAttribute('href', 'http://domain.com/download.aspx?songid='+ id);
hp.appendChild(document.createTextNode(' [Download Here]'));

// not sure if this is the best way to do it
linkNode.parentNode.insertBefore(hp, linkNode.nextSibling);
//linkNode.parentNode.appendChild(hp); // not good
}
}
}





BUT this doesnt seem to work...

EDIT>> NEVER MIND I GUESS JS DOESNT LIKE SPLITTING WITH "="...

Trinithis
12-21-2007, 06:55 PM
How about:

var links = document.getElementsByTagName('a');
for(var i = 0; i < links.length; ++i) {
var a = links[i];
if(a.href.indexOf("http://domain.com/audiosample.aspx?songid=*") == 0) {
var id = a.href.split('=');
id = id[id.length - 1];
if(id !== "") {
var hp = document.createElement('a');
hp.setAttribute("href", "http://domain.com/download.aspx?songid=" + id);
hp.appendChild(document.createTextNode(" [Download Here]"));
if(a.nextSibling)
a.parentNode.insertBefore(hp, a.nextSibling);
else
a.parentNode.appendChild(hp);
}
}
}