Advanced Search

Results 1 to 2 of 2

Thread: Small Greasemonkey Script (Adding Links To Pages)

  1. #1
    Join Date
    Mar 2006
    Location
    Nashville, TN
    Posts
    600
    Thanks
    5
    Thanked 4 Times in 4 Posts

    Default Small Greasemonkey Script (Adding Links To Pages)

    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...
    Code:
    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 "="...
    Last edited by benslayton; 12-21-2007 at 05:45 PM.

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    How about:
    Code:
    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);
        }
      }
    }
    Trinithis

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •