【问题标题】:How do I make Greasemonkey click a link that has specific Text?如何让 Greasemonkey 单击具有特定文本的链接?
【发布时间】:2013-09-09 07:55:49
【问题描述】:

到目前为止,我有这个,

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

但它不起作用。

有什么想法让它工作吗?

【问题讨论】:

标签: javascript hyperlink greasemonkey


【解决方案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;

【讨论】:

  • 链接到相关页面,或将其源代码粘贴到 pastebin.com,然后链接到该页面。此代码在具有此类链接的页面上完美运行。
  • pastebin.com/9tH7vSeF@Brock Adams 这里是源代码的 pastebin。
  • Xaric,我看到你更新了问题代码。好的。现在注意问题代码忽略了答案中指出的问题!!我给了你一个完整的工作脚本,没有比这更好的了。 ...在该站点上测试...按设计工作。
  • 不,您有一个格式错误的元数据块。从// ==UserScript==// ==/UserScript== 的所有内容都有非常具体的规则。
  • 我还注意到我忘了添加分号。这可能是它不起作用的原因。
【解决方案2】:

我从未听说过“包含”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;
    }
}

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多