1

我想打开一个包含单词的链接google。它看起来像这样:

 <input class="submit" style="background: #409999; border-radius: 10px;" value="open" onclick="Open('143615', '1', 'https://www.google.de/');" type="submit">


我试过这个 Greasemonkey 代码:

var snapResults = document.evaluate("//input[contains(@onclick, 'test')]",document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = snapResults.snapshotLength - 1; i >= 0; i--) {
    var elm = snapResults.snapshotItem(i);
    // do stuff with elm

    if (elm) //open the window, which contains "test"
    {
        elm.singleNodeValue.click(); //there is no effect ...
        alert(i+". element opend");
    }           
    else
    {
        alert(i+". Not found.");
    }
}

它没有效果。我想通过 Greasemonkey 打开窗口(点击事件?)

当我使用alert(elm.href);它时说它是“未定义的”。但是当我在FirePath中尝试时,XPath 可以工作。

4

1 回答 1

1

您说 XPath 有效,但elm.href在 GM 脚本中未定义。这表明<input>是通过 AJAX 添加的。

您的脚本需要使用AJAX 感知技术。就像是:

// ==UserScript==
// @name     _Clicking "Open" buttons
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("input.submit[onclick*='open']", clickOpenBtn);

function clickOpenBtn (jNode) {
    triggerMouseEvent (jNode[0], "click");
}

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}
于 2013-09-24T15:04:57.410 回答