// document.body.style.border = "15px solid red"; /* IN PROGRESS: - List of alternatives should be loaded from a user-specified external source. Can be modified via options.html/options.js TODO - Pop-up should be formatted to look better. - Pop-up should be dismissable (per URL / per domain). - Once dismissed, popup should not be shown on the same url/domain. Use cookies or localStorage for this. - Since we're replacing innerHTML, we should protect against XSS attacks. - Fix and test the options UI - Test and fix for Chrome, Brave & Firefox */ const fillTemplate = function(templateString, templateVars){ return new Function("return `"+templateString +"`;").call(templateVars); } const altTemplate = "\ \

${this.name} →

\

${this.desc}

\
" function createNonRecommendedAlts(alternatives) { let nonRecommendedAlts = document.createElement("div"); nonRecommendedAlts.setAttribute("style", "text-align: left;"); nonRecommendedAlts.innerHTML = "

MORE ALTERNATIVES

"; alternatives.map(alternative => { nonRecommendedAlts.innerHTML += fillTemplate(altTemplate, alternative); }) return nonRecommendedAlts; } function createRecommendedAlt(alternative) { let recommendedAlt = document.crateElement("div"); let betterBrandText = document.createElement("h1"); betterBrandText.innerHTML = "Better"; betterBrandText.setAttribute("style", "font-size: 32px; color: #222222; font-weight: bold; margin: 12px;"); let alternativeText = document.createElement("p"); alternativeText.innerHTML = recommendedAlternative.desc; alternativeText.setAttribute("style", "font-size: 20px; color: #222222; font-weight: bold; margin-top: 24px;"); let alternativeCTA = document.createElement("a"); alternativeCTA.innerHTML = recommendedAlternative.name + " →"; alternativeCTA.setAttribute("href", recommendedAlternative.url); alternativeCTA.setAttribute("style", "display: inline-block; padding: 12px 24px; background-color: #222222; color: #ffffff; border-radius: 4px;"); recommendedAlt.appendChild(betterBrandText); recommendedAlt.appendChild(alternativeText); recommendedAlt.appendChild(alternativeCTA); return recommendedAlt; } function showBetter(alternatives) { if(!alternatives) return; let betterdiv = document.createElement("div"); betterdiv.setAttribute("style", "background-color: #ffcc49; position: fixed; bottom: 10px; right: 10px; width: 300px; \ box-shadow: 0px 10px 30px #222222; padding: 12px; text-align: center; font-size: 20px;") betterdiv.appendChild(createRecommendedAlt(alternatives[0])); if(alternatives.length > 1) { betterdiv.appendChild(createNonRecommendedAlts(alternatives.slice(1))); } let dismissButton = document.createElement("button"); dismissButton.innerHTML = "✗ Dismiss suggestion for this URL"; dismissButton.setAttribute("style", "display: block; font-size: 14px; margin: 16px auto;"); betterdiv.appendChild(dismissButton); document.body.appendChild(betterdiv); } chrome.runtime.sendMessage({type: 'getMatch', url: document.location.href}, (response) => { if (response) { showBetter(response); } });