Xaric
Xaric

Reputation: 9

How do I make Greasemonkey click a link that has specific Text?

So far I have this,

// ==UserScript==
// @name           Random Creature LevelUP for Avadopts
// @namespace      Xaric
// @description    Clicks randomcreature for leveling up on Avadopts
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// @include *


//--- Note that the contains() text is case-sensitive.
var TargetLink          = $("a:contains('Give a random creature a level!')")

if (TargetLink  &&  TargetLink.length) 
     window.location.href    = TargetLink[0].href

But it doesn't work.

Any thoughts to get it working?

Upvotes: 0

Views: 2477

Answers (2)

MJ Walsh
MJ Walsh

Reputation: 633

I've never heard of a 'contains' css pseudo class but you can always just loop through the links.

var l = document.getElementsByTagName("a");
var i = l.length; 
while (i--) {
    if (l[i].innerHTML == "Give a random creature a level!") {
        window.location.href = l[i].href;
        break;
    }
}

For more reliable results you could use a regular expression:

var l = document.getElementsByTagName("a");
var i = l.length; 
while (i--) {
    if (l[i].innerHTML.match(/random creature/)) {
        window.location.href = l[i].href;
        break;
    }
}

Upvotes: 1

Brock Adams
Brock Adams

Reputation: 93473

The metadata section must be formatted precisely.

That section is still malformed.

Use:

// ==UserScript==
// @name            _Random Creature LevelUP for Avadopts
// @description     Clicks randomcreature for leveling up on Avadopts
// @include         http://avadopts.com/*
// @include         http://www.avadopts.com/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==

//--- Note that the contains() text is case-sensitive.
var TargetLink              = $("a:contains('Give a random creature a level!')");

if (TargetLink  &&  TargetLink.length)
    window.location.href    = TargetLink[0].href;

Upvotes: 1

Related Questions