const LISTEN_TYPES = {
one: {
select: (selector, root) => root.querySelector(selector),
validate: (node) => !!node
},
all: {
select: (selector, root) => root.querySelectorAll(selector),
validate: (nodes) => nodes?.length > 0
}
};
function listenStep(args) {
args.node = args.select(args.selector, args.root);
if (!args.validate(args.node)) {
args.step++;
if (args.step < args.limit) {
setTimeout(() => {
listenStep(args);
}, args.delay);
} else {
args.reject();
}
} else {
args.resolve(args.node);
}
}
async function asyncListenFor(selector, type = 'one', customArgs = {}) {
const args = {
root: document,
node: undefined,
selector,
delay: 100,
limit: 50,
step: 0,
select: LISTEN_TYPES[type].select,
validate: LISTEN_TYPES[type].validate,
...customArgs
};
if (type === 'one' || type === 'all') {
return new Promise((resolve, reject) => {
listenStep({ ...args, resolve, reject });
});
}
}
asyncListenFor('body').then((body) => {
const script = document.createElement('script');
script.src = 'https://static.elfsight.com/platform/platform.js';
script.setAttribute('data-use-service-core', '');
script.defer = true;
const widgetDiv = document.createElement('div');
if (window.innerWidth < 640) {
widgetDiv.className = 'elfsight-app-5a76be04-b5f4-4fc2-ac9e-b2eb79ad1ca6';
} else {
widgetDiv.className = 'elfsight-app-9836aa54-abd3-42da-ba5c-e3d99b3d2509';
}
widgetDiv.setAttribute('data-elfsight-app-lazy', '');
body.appendChild(script);
body.appendChild(widgetDiv);
});