0

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?

Xaric
  • 9
  • 3
  • 1
    possible duplicate of [How do you make Greasemonkey Click a link that has specified text?](http://stackoverflow.com/questions/5036737/how-do-you-make-greasemonkey-click-a-link-that-has-specified-text) – Brock Adams Aug 09 '11 at 01:14

2 Answers2

1

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;
    }
}
MJ Walsh
  • 593
  • 6
  • 11
1

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;
Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Link to the page in question, or paste its source-code at pastebin.com, and link to that. This code works perfectly on a page that has such a link. – Brock Adams Aug 09 '11 at 03:43
  • http://pastebin.com/9tH7vSeF @Brock Adams here is the pastebin of the source code. – Xaric Aug 09 '11 at 04:38
  • Xaric, I see that you've updated the question code. Good. Now notice that the question code ignores the problems pointed out in the answer!! I gave you a complete working script, it doesn't get any better than that. ... Tested on that site... Works as designed. – Brock Adams Aug 09 '11 at 04:45
  • No, you had a malformed metadata block. Everything from `// ==UserScript==` to `// ==/UserScript==` has very specific rules. – Brock Adams Aug 09 '11 at 07:10
  • I also noticed that I forgot to add in semicolons. that could be possibly the reason why it didn't work. – Xaric Aug 09 '11 at 07:40