简体   繁体   中英

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?

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;

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM