1

如果“剩余能量”值大于 20%,我想制作一个 Greasemonkey 脚本,该脚本每秒自动单击“清空垃圾”链接。

该页面的代码如下。感谢并为我糟糕的英语感到抱歉。

<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>
4

1 回答 1

0

使用jQuery 的 contains 选择器setInterval.

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

// ==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);
    }
}
于 2012-08-24T13:21:31.543 回答