Setting up custom JS that depends on some state of third party vendor (Live Chat services like Zoho Chat, Olark Chat, LiveChatIn, etc) or just looking for certain elements that requires some time to get loaded, sometimes waiting for certain number of seconds is not sufficient. It needs a better way to monitor these states with high probability of success and setup the custom JS once the required state is detected.
Create a custom JS template that houses the main logic and execute only when the desired state is detected.
There are 4 primary steps to achieve the desired result:
First step determines the current state of the document. When in loading state, it listens for DOMContentLoaded event. Once dispatched it then executes the Load function. When the document is already loaded, it executes the Load function right away.
The Load function listens for a CUSTOM event that when dispatched it will execute the Register function. It then delegates state listening process to the WaitThenCheck function.
WaitThenCheck function is the one that tries to detect the desired state. When state is not yet available it waits for a second then tries again. This uses the setTimeout so it’s a non-blocking operation. Once the desired state is detected, it will execute the Setup function.
The Setup function can be customized depending on the spec but the last action will always be to dispatch the CUSTOM event that the Load function listen to. This function usually has 2 other functions that it delegates certain actions to like isDefined and/or isInitialized. These two other functions mostly are modified tailored to the needs of the spec.
Once the CUSTOM event is dispatched, the Register function is executed. This Register function houses the main task needed in the integration.
This template also has an easy way to debug as it has built in debugging section and can be enabled/disabled via localStorage (see logInConsole function).
<!-- MCFX - A Custom JS Template --><script type="text/javascript"> (function () { var DEBUG = localStorage ? 'true' === localStorage.getItem('CUSTOM_DEBUG') : false; function logInConsole() { if (!DEBUG) { return; } window.console.log.apply(this, arguments); } logInConsole('CUSTOM:start'); function register() { logInConsole('CUSTOM:register'); // stuff here logInConsole('CUSTOM:registered'); } function isCustomJSDefined() { return undefined !== window.CUSTOMJS; } function isCustomJSInitialized() { return isCustomJSDefined() && true === window.CUSTOMJS.initialized; } function setupOnReady() { logInConsole('CUSTOM:setupOnReady', window.CUSTOMJS.ready); if ('function' === typeof window.CUSTOMJS.ready) { var oldReady = window.CUSTOMJS.ready; window.addEventListener('CUSTOM_ready', oldReady); } window.CUSTOMJS.ready = function () { logInConsole('CUSTOM:ready'); window.dispatchEvent(new Event('CUSTOM_ready')); }; if (isCustomJSInitialized()) { logInConsole('CUSTOM:past-ready'); window.dispatchEvent(new Event('CUSTOM_ready')); } } var timeouts = []; function waitThenCheck() { logInConsole('CUSTOM:waitThenCheck'); if (timeouts.length > 30) { logInConsole('CUSTOM:waitThenCheck:too-many'); timeouts.forEach(function (timeout) { clearTimeout(timeout); }); return; } if (isCustomJSDefined()) { logInConsole('CUSTOM:waitThenCheck:defined'); timeouts.forEach(function (timeout) { clearTimeout(timeout); }); setupOnReady(); return; } logInConsole('CUSTOM:waitThenCheck:not-defined'); timeouts.push(setTimeout(waitThenCheck, 1000)); } function load() { logInConsole('CUSTOM:load'); window.addEventListener('CUSTOM_ready', register); waitThenCheck(); } if (document.readyState === 'loading') { logInConsole('CUSTOM:loading'); document.addEventListener('DOMContentLoaded', load); } else { logInConsole('CUSTOM:readystate'); load(); } logInConsole('CUSTOM:stop'); })();</script><!--End: MCFX - A Custom JS Template -->