最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set waiting time, delay time, to execute script - Stack Overflow

programmeradmin2浏览0评论

this script for greasemonkey is working for me, to click a button, when a certain website was loaded.

But how can I set a waiting time? Example: The website loaded and the script waits 1 second till it is executed.

My second question is: how can I run it only once per pageload? The script starts over and over again.

    // ==UserScript==
// @name     _YOUR_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  .7.2/jquery.min.js
// @require  .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
);

Script source: How do I get Greasemonkey to click on a button that only appears after a delay?

this script for greasemonkey is working for me, to click a button, when a certain website was loaded.

But how can I set a waiting time? Example: The website loaded and the script waits 1 second till it is executed.

My second question is: how can I run it only once per pageload? The script starts over and over again.

    // ==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
);

Script source: How do I get Greasemonkey to click on a button that only appears after a delay?

Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Sep 12, 2015 at 9:38 StefanSStefanS 2292 gold badges3 silver badges12 bronze badges 2
  • 1 Try setTimeout(function() { waitForKeyElements ( ................ ); }, 1000) – woxxom Commented Sep 12, 2015 at 10:09
  • It is not working :( – StefanS Commented Sep 12, 2015 at 13:00
Add a comment  | 

2 Answers 2

Reset to default 25

This might be what you are looking for:

$(document).ready(function() { //When document has loaded

setTimeout(function() {

//Code to run After timeout elapses

}, 2000); //Two seconds will elapse and Code will execute.

}); 

Answering your second question:

how can I run it only once per pageload? The script starts over and over again.

You should simply pass a third argument true to this function (read instructions how to use it in it's source code), like this:

waitForKeyElements (
    "input[type='submit'][value*='Click Me Now']",
    clickSubmitBtnWhenItAppears,
    true
);

But there's no need to use external scripts, especially that waitForKeyElements also requires JQuery to run - which has lots of nice additional features, but if you are making very simple script, then it's an overkill. You can simply check website every x miliseconds for a match and when it's found, turn off timer. It's also a good idea, to limit number of times the check will run, in case a site doesn't load properly or layout changes, in which case the check would keep running.
This will check every 100 ms for no more than 50 times, making it check website for max 5 seconds, but if it finds a match, it will run your function and stop checking website.

let retries = 50;

const intervalID = setInterval(_ => {
    const match = document.querySelector("<CSS SELECTOR HERE>");
    if(match) doSomething(match);

    retries--;
    if(retries == 0 || match) clearInterval(intervalID);
}, 100);
发布评论

评论列表(0)

  1. 暂无评论