JSFiddle - React, Tailwind, and code Playground
HTML
<p>This is an iframe write and userscript target platform.</p>
<p><button>Add trigger text to iframe.</button></p>
CSS
body {padding: 0 1em;}
JavaScript
window.newFrame = $('<iframe>', {
id: 'tstIframe',
load: finishIframeChk
} ).appendTo ('body');
$("button").click ( function () {
ifrBody.append ('<div class="comment">I\'m what you\'re waiting for.</div>');
} );
function finishIframeChk () {
//-- In Chrome, the load event fires before proper load!!
pollUntil (finishIframe);
}
function finishIframe () {
var frameReady = typeof newFrame !== "undefined";
if (frameReady) {
var ifrDoc = newFrame[0].contentWindow.document;
window.ifrBody = $('body', ifrDoc);
ifrBody.html ('<h3>Target iFrame</h3>');
}
return frameReady;
}
function pollUntil (callbackFunc, safeMaxSeconds, iDelayMs, context) {
"use strict";
var iTimer = null;
var iDelay = iDelayMs || 150;
var safeMaxSeconds = safeMaxSeconds || 2;
var fireCount = 0;
var triggerCount = 0;
var K = 0;
var maxK = safeMaxSeconds * 1000 / iDelayMs;
function _stats () {
return {
mssg: 'Triggered ' + triggerCount + ' times and fired ' + fireCount + ' times.',
fired: fireCount,
triggered: triggerCount
}
}
function _reset () {
fireCount = 0;
triggerCount = 0;
clearTimer ();
}
function clearTimer () {
if (iTimer) {
clearInterval (iTimer);
iTimer = null;
}
}
function fireCallback () {
triggerCount++;
K++
if (K >= maxK) clearTimer ();
var conditionMet = callbackFunc (context);
if (conditionMet) {
fireCount++;
clearTimer ();
}
return conditionMet;
}
function _trigger () {
if ( ! fireCallback () ) {
clearTimer ();
iTimer = setInterval (fireCallback, iDelayMs);
}
}
_trigger ();
return {
reset: ...