如何使用Greasemonkey单击此按钮? [英] How do I click on this button with Greasemonkey?

查看:235
本文介绍了如何使用Greasemonkey单击此按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JS的新手,我想点击这个按钮:

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>



此按钮的XPath为: html / body / div [5] / div / span [2] / a ,目标页面的快照可以在 this Fiddle


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.

这是我试过的,工作。 (我使用从 getElementsByClassName > http://code.google.com/p/getelementsbyclassname/ ):

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();


推荐答案

这是一个完整脚本这样做。它使用jQuery来:contains()选择器

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

更新:修改脚本以报告AJAX。

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);
}






注意:在某些情况下 contains() term可能会错误触发。例如,如果有< a class =simplebutton> unfollow< / a> 按钮。

以下是一种防止false点击。修改 clickOnFollowButton 函数如下:


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*follow\s*$/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);
}






p>


Several things:


  1. getElementsByClassName() 返回元素的列表或集合。你不能只是 .submit()它的结果。 .submit()用于单个元素。

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

由于这是一个链接 .submit()将无法工作。 .click()将常常工作,但通常不会 - 当链接被事件监听器授权时(必须是这个问题的情况)。

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).

上面提到的 clickEvent 代码几乎适用于所有情况。

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

您提供的网页代码没有任何链接, class =simplebutton和包含的文本

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

你在使用?哪个Greasemonkey版本?什么操作系统?

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

查找并使用适当的JavaScript参考和适当的DOM引用。

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).

  • The MDN JavaScript Reference is the most up-to-date and the most applicable for Greasemonkey (a Firefox extension) applications.
  • Likewise, the MDN Document Object Model (DOM) Reference.

使用CSS路径,这比XPATH容易得多。 Firebug会显示给定元素的CSS路径。

jQuery使用CSS选择器/路径, document.querySelector()(非jQuery方法)。

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).

这篇关于如何使用Greasemonkey单击此按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆