- Mar 22, 2013
- 9,143
- 10,489
I created this Greasemonkey script to clear out an annoying field that gets autofilled on some site.
The problem is that the element appears 10-20 seconds after the page has loaded. In order to make the script work, I had to use the "setTimeout" function. But it's not efficient.
The element could appear after 10 seconds, and then I have 10 seconds of the field not being cleared. Or it could appear after 30 seconds and never get cleared, because when the script runs it won't find the selector.
I tried googling and reading stackoverflow, using some "waitforkeyelements" and other solutions, but I didn't manage to make it work.
Can anyone wrap my code in a "wait until element appears" type of code black magic?
Thanks
Code:
// ==UserScript==
// @name Whatevs
// @include https://site.com/
// @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.
*/
setTimeout(function() {
var elems = document.querySelectorAll('[name^="url"]');
for (var i = 0, len = elems.length; i < len; i++)
{ elems[i].value = ""; }
}, 20000);
The problem is that the element appears 10-20 seconds after the page has loaded. In order to make the script work, I had to use the "setTimeout" function. But it's not efficient.
The element could appear after 10 seconds, and then I have 10 seconds of the field not being cleared. Or it could appear after 30 seconds and never get cleared, because when the script runs it won't find the selector.
I tried googling and reading stackoverflow, using some "waitforkeyelements" and other solutions, but I didn't manage to make it work.
Can anyone wrap my code in a "wait until element appears" type of code black magic?
Thanks