如何让 Greasemonkey 单击仅在延迟后出现的按钮? [英] How do I get Greasemonkey to click on a button that only appears after a delay?

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

问题描述

我看到了很多类似的问题,并且我已经尝试了所有我能想到的方法来让我自己解决这个问题.

I've seen a lot of similar questions and I've tried everything I can think of to get this to work on my own.

首先是相关代码(¿来自目标页面?):

First the relevant code (¿from the target page?):

document.getElementById('btn_submit').innerHTML =
 '<input type="hidden" value="17271" name="idofclick">
  <input type="submit" value=" Click Me Now! " name="submit_com" class="padding2">';

页面上基本上有一个计时器和现在点击我!"按钮在 3 秒后出现,这就是我想要点击的部分.

Basically there is a timer on the page and the "click me now!" button appears after 3 secs, that's the part I want to click on.

这是我的代码.它不起作用:

This is my code. It's not working:

// ==UserScript==
// @name        abc
// @namespace   something
// @description abc Scripty
// @include     *
// @version     1
// ==/UserScript==
(function ClicktheButton(obj) {
   var evt = document.createEvent("MouseEvents");
   evt.initMouseEvent("click", true, true, window,
   0, 0, 0, 0, 0, false, false, false, false, 0, null);
   var canceled = !obj.dispatchEvent(evt);
   /*

  if(canceled) {
    // A handler called preventDefault
    alert("canceled");
  } else {
    // None of the handlers called preventDefault
    alert("not canceled");
  }
  */
}

var StupidButton = document.querySelector.innerHTML('input[type="submit"][value=" Click Me Now! "]');
ClicktheButton(StupidButton);

推荐答案

该代码有错误.使用 Firefox 的错误控制台 (CtrlShiftJ) 来查看它们.使用 jslint 来检查您的代码也很有帮助.

That code has errors. Use Firefox's error console (CtrlShiftJ) to see them. Using jslint to check your code, can be helpful too.

无论如何,这是一个常见的 Greasemonkey 问题.使用 waitForKeyElements() 实用程序来处理该按钮的延迟出现.使用 jQuery 来简化代码(并使其更加健壮和便携).

Anyway, this is a common Greasemonkey problem. Use the waitForKeyElements() utility to handle the delayed appearance of that button. Use jQuery to simplify the code (and make it more robust and portable).

所以你的脚本会变成:

// ==UserScript==
// @name     _YOUR_NAME
// @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 design change
    introduced in GM 1.0.   It restores the sandbox.
*/

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

//-- Value match is case-sensitive
waitForKeyElements (
    //"#btn_submit input[type='submit'][value*='Click Me Now']",
    "input[type='submit'][value*='Click Me Now']",
    clickSubmitBtnWhenItAppears
);

这篇关于如何让 Greasemonkey 单击仅在延迟后出现的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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