Skip to content

Instantly share code, notes, and snippets.

@phylliswong
Last active March 2, 2022 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phylliswong/fe273529a39fcb5ca1231229263110b2 to your computer and use it in GitHub Desktop.
Save phylliswong/fe273529a39fcb5ca1231229263110b2 to your computer and use it in GitHub Desktop.
/*
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)
}
@phylliswong
Copy link
Author

Screen Shot 2022-03-02 at 3 14 40 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment