Last active
March 2, 2022 23:28
-
-
Save phylliswong/fe273529a39fcb5ca1231229263110b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Running Variants locally without the editor | |
Authored by Richard Cowin | |
Disable content security policy with Resource Override | |
This will allow for local host | |
window.applyVariants(‘http://localhost:8000/t-and-c/’, [‘v1’]); | |
*/ | |
window.applyVariants = function applyVariants(folder, variants, poll) { | |
poll = poll || {duration: 30000, interval:50}; | |
function getRule() { return window.evolv && window.evolv.renderRule } | |
function loadVariants() { | |
console.info('loading context') | |
loadScript(`${folder}context.js`) | |
loadStyles(`${folder}context.css`) | |
variants.forEach(v => { | |
console.info('loading variant', v) | |
loadScript(`${folder}${v}.js`) | |
loadStyles(`${folder}${v}.css`) | |
}) | |
} | |
waitFor(getRule, loadVariants, poll) | |
} | |
function loadScript(path) { | |
var scriptNode = document.createElement('script'); | |
scriptNode.setAttribute('src', path); | |
document.head.appendChild(scriptNode); | |
} | |
function loadStyles(path){ | |
// link rel="stylesheet" href="styles.css" | |
var styleNode = document.createElement('link'); | |
styleNode.setAttribute('rel', 'stylesheet'); | |
styleNode.setAttribute('href', path); | |
document.head.appendChild(styleNode); | |
} | |
function listenToEvents(config) { | |
var poll = config.poll || {duration: 2000, interval:50 }; | |
function getRule() { return window.evolv && window.evolv.renderRule } | |
waitFor(getRule, inject, poll); | |
} | |
function waitFor(check, invoke, poll) { | |
if (check()) { | |
invoke(); | |
return; | |
} | |
var polling = setInterval(function() { | |
try { | |
if (check()) { | |
invoke(); | |
clearInterval(polling); | |
polling = null; | |
} | |
} catch(e) { console.info('listener not processed') } | |
}, poll.interval) | |
setTimeout(function() { | |
if (!polling) return | |
clearInterval(polling) | |
console.info('listener timeout') | |
}, poll.duration) | |
} |
Author
phylliswong
commented
Mar 2, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment