按钮单击帮助(Greasemonkey)

时间:2011-06-20 21:22:02

标签: javascript button click greasemonkey

请帮助我,我需要点击这两个按钮链接:

<li class="booster" id="b_10">
    <img alt="" src="http://www.erepublik.com/images/parts/pixel.gif" class="overlay" />
    <div class="top">
        <strong><a href="javascript:;">Army boots</a></strong>
        <big>0</big>
    </div>
    <img src="http://static.erepublik.com/uploads/boosters/10_77x77.png" alt="" />
    <em class="give like10"></em>
</li>

<span class="tip" id="tip_534689">
    <a class="workTrigger" companyId="534689" href="javascript:;">
        <img src="http://www.erepublik.com/images/modules/myland/tip_icons/work.png" alt="" />
    </a>
</span>

1 个答案:

答案 0 :(得分:1)

这样的东西应该可行,但// @include指令应该更具体,如果可能的话,jQuery选择器可能需要问题中没有给出的信息。

艺术在于选择选择器来获取您所追求的链接/按钮/节点。为了获得最佳效果,通常需要查看整个页面的代码。

// ==UserScript==
// @name            _Generic Link clicker
// @include         http://www.erepublik.com/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==

var topBoosterLink  = $("li.booster > div.top > strong > a:first");
var workTipLink     = $("span.tip > a.workTrigger:first");

clickJ_Node (topBoosterLink);
clickJ_Node (workTipLink);


function clickJ_Node (jNode)
{
    if (jNode && jNode.length)
    {
        var clickEvent  = document.createEvent ("HTMLEvents");
        clickEvent.initEvent ("click", true, true);
        jNode[0].dispatchEvent (clickEvent);
    }
    else
        GM_log ('No node found to click!');
}