如果某个值在另一个节点中,如何每秒自动点击一个链接?

时间:2012-08-24 12:38:03

标签: javascript greasemonkey

我想制作一个Greasemonkey脚本,自动点击"清空垃圾箱"如果价值"能量离开"大于20%。

页面代码如下。谢谢,抱歉我的英语不好。

<div class="btn-group">
    <a class="btn btn-mini" href="#" data-toggle="dropdown"><i class="icon-wrench"></i> Chores</a>
    <a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
    <ul class="dropdown-menu">
        <li><a href="/pet/100000113715357/chore/1">Poke your owner (5%)</a></li>
        <li><a href="/pet/100000113715357/chore/2">Pinch your owner (10%)</a></li>
        <li><a href="/pet/100000113715357/chore/3">Serve strawberries (15%)</a></li>
        <li><a href="/pet/100000113715357/chore/4">Empty trash (20%)</a></li>
        <li><a href="/pet/100000113715357/chore/5">Iron clothes (40%)</a></li>
        <li><a href="/pet/100000113715357/chore/6">Clean the pet store (60%)</a></li>
        <li><a href="/pet/100000113715357/chore/7">Spank your owner (80%)</a></li>
        <li><a href="/pet/100000113715357/chore/8">Dance with your owner (100%)</a></li>
        <li class="divider"></li>
        <li><a href="#">Energy left: 15%</a></li>
    </ul>
</div>

1 个答案:

答案 0 :(得分:0)

使用jQuery's contains selectorsetInterval

这里应该关闭 一个完整的脚本

// ==UserScript==
// @name    _Auto-click the Empty trash link.
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==

setInterval (EmptyTrashIfWeCan, 1000);  //1000 = 1 second.

function EmptyTrashIfWeCan () {
    var energyDisp  = $("div.btn-group ul.dropdown-menu li a:contains('Energy left')");
    var energyVal   = energyDisp.text ().replace (/^\s*Energy left:\s+(\d+)%\s*$/i, "$1");
    if (parseInt (energyVal, 10)  >  20) {
        var emptyTrashLink  = $("div.btn-group ul.dropdown-menu li a:contains('Empty trash')");
        //--- Click the link in a more robust way...
        var clickEvent      = document.createEvent ('MouseEvents');
        clickEvent.initEvent ('click', true, true);
        emptyTrashLink.dispatchEvent (clickEvent);
    }
}