//TODO uglify this (async function () { const emEcoBot = {}; //fetch config from api emEcoBot.domain = (document.currentScript.src).replace("/embed.js","") || "https://ecobot.ecomap.dev"; emEcoBot.chatWindow = document.getElementById('emt-ecobot-container'); if (!emEcoBot.chatWindow) { console.error("ERROR: Ecobot embed code is incorrect.") return; } if (!emEcoBot.chatWindow.dataset.ecobotId) { console.error("ERROR: can't use EcoBot without an id in your embed code.") return; } document.body.appendChild(emEcoBot.chatWindow); emEcoBot.id = emEcoBot.chatWindow.dataset.ecobotId; const configRes = await fetch(emEcoBot.domain + '/api/config?id=' + emEcoBot.id); emEcoBot.config = await configRes.json(); if (!emEcoBot.config || !emEcoBot.config.ecobot) { console.error("ERROR: invalid response from EcoBot server, check your embed code or try again later.") return; } emEcoBot.config = emEcoBot.config.ecobot; emEcoBot.config['allowedDomainsList'] = [...emEcoBot.config['allowedDomainsList'], "http://localhost:3000", "https://vercel.live", "https://ecobot.ecomap.dev"] let target_url = (window.location != window.parent.location) ? document.referrer : document.location.href; target_url = new URL(target_url) const requesting_domain = target_url.hostname // console.log(requesting_domain) const domains_check = emEcoBot.config['allowedDomainsList'].filter(d => new URL(d).hostname.includes(requesting_domain)) if (!domains_check.length) { document.getElementById('emt-ecobot-container').innerHTML = `
EcoBot access is not enabled for the current domain.
`; } else { //declare what vars we're replacing, and the text string of html we're adding to the DOM emEcoBot.valMap = ["name", "avatarLogo", "primaryColor", "accentColor", "id"]; emEcoBot.innerHtml = ``; //replace all the vars in the html string with real values emEcoBot.valMap.forEach(val => { emEcoBot.innerHtml = emEcoBot.innerHtml.replaceAll(`{{${val}}}`, emEcoBot.config[val]); }); emEcoBot.innerHtml = emEcoBot.innerHtml.replaceAll(`{{domain}}`, emEcoBot.domain); //add html to DOM emEcoBot.chatWindow.innerHTML = emEcoBot.innerHtml; //add even listeners to the toggle & close buttons emEcoBot.closeBtn = document.getElementById('ecobot-close-btn'); emEcoBot.toggleBtn = document.getElementById('ecobot-toggle-btn'); ['click', 'keydown'].forEach(evt => { emEcoBot.closeBtn.addEventListener(evt, function (e) { //ignore non-enter keys if (e.key && e.key !== "Enter") { return; } e.preventDefault(); emEcoBot.chatWindow.classList.remove('open'); }); emEcoBot.toggleBtn.addEventListener(evt, function (e) { //ignore non-enter keys if (e.key && e.key !== "Enter") { return; } e.preventDefault(); emEcoBot.chatWindow.classList.toggle('minimized'); emEcoBot.chatWindow.classList.toggle('open'); }); }); } })();