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 |2 Answers
Reset to default 25This 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);
setTimeout(function() { waitForKeyElements ( ................ ); }, 1000)
– woxxom Commented Sep 12, 2015 at 10:09