如何使Greasemonkey单击具有特定文本的链接?

时间:2011-08-09 00:37:29

标签: javascript hyperlink greasemonkey

到目前为止,我有这个,

// ==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

但它不起作用。

有任何想法让它发挥作用吗?

2 个答案:

答案 0 :(得分:1)

metadata section必须精确格式化。

该部分仍然格式错误。

使用:

// ==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;

答案 1 :(得分:0)

我从来没有听说过'包含'css伪类,但你总是可以遍历链接。

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

为了获得更可靠的结果,您可以使用正则表达式:

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