Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

javascript - How do I click on this button with Greasemonkey?

I'm a total newbie with JS, and I'm trying to click on this button:

<a class="simplebutton" href="javascript:void(0);">find</a>


The XPath of this button is: /html/body/div[5]/div/span[2]/a, and a snapshot of the target page can be seen at this Fiddle.

This is what I've tried, but it doesn't work. (I'm using the function getElementsByClassName which I got from http://code.google.com/p/getelementsbyclassname/):

document.getElementsByClassName('simplebutton').submit();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a complete script that does that. It uses jQuery for the :contains() selector.

Update: Modified script to account for reported AJAX.

// ==UserScript==
// @name     _Click on a specific link
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/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 major design change
    introduced in GM 1.0.
    It restores the sandbox.
*/

//--- Note that contains() is CASE-SENSITIVE.
waitForKeyElements ("a.simplebutton:contains('follow')", clickOnFollowButton);

function clickOnFollowButton (jNode) {
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

Note: in some cases the contains() term can trigger falsely. For example, if there is an <a class="simplebutton">unfollow</a> button.
Here's one way to prevent false clicks. Modify the clickOnFollowButton function like so:

function clickOnFollowButton (jNode) {
    if ( ! /^s*follows*$/i.test (jNode.text() ) ) {
        /*--- If the node contains anything but "follow" (surrounded by 
            optional whitespace), don't click it.
        */
        return false;
    }
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

Several things:

  1. getElementsByClassName() returns a list or "collection" of elements. You can't just .submit() its result like that. .submit() is for single elements.

  2. Since this is a link .submit() won't work. .click() will often work, but often not -- when a link is empowered by an event listener (which must be the case for this question).

    The clickEvent code, given above, works in almost all cases.

  3. The page code you gave does not have any link, with class="simplebutton" and text containing find!

  4. What Browser are you using? Which Greasemonkey version? And what OS?

  5. Find and use an appropriate javascript reference and an appropriate DOM reference. The reference listed in the question is for a library that is not standard and not included in your script (most likely).

  6. Use the CSS path, its much easier than XPATH, for this kind of thing. Firebug will show you the CSS path for a given element.
    jQuery uses CSS selectors/paths, as does document.querySelector() (a non jQuery approach).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...