/** * @constant chromeUrl * @description Chrome Web Store link * @type {string} */ const chromeUrl = 'https://chrome.google.com/webstore/detail/djcbfpkdhdkaflcigibkbpboflaplabg'; /** * @constant dispatch * @description Shortcut to send messages to background script * @type {void} */ const dispatch = chrome.runtime.sendMessage; /** * @constant edgeUrl * @description Edge Add-ons link * @type {string} */ const edgeUrl = 'https://microsoftedge.microsoft.com/addons/detail/hbogodfciblakeneadpcolhmfckmjcii'; /** * @constant firefoxUrl * @description Firefox Add-ons link * @type {string} */ const firefoxUrl = 'https://addons.mozilla.org/es/firefox/addon/cookie-dialog-monster/'; /** * @constant isChromium * @description Is current browser an instance of Chromium? * @type {boolean} */ const isChromium = navigator.userAgent.indexOf('Chrome') !== -1; /** * @constant isEdge * @description Is current browser an instance of Edge? * @type {boolean} */ const isEdge = navigator.userAgent.indexOf('Edg') !== -1; /** * @constant isFirefox * @description Is current browser an instance of Firefox? * @type {boolean} */ const isFirefox = navigator.userAgent.indexOf('Firefox') !== -1; /** * @description Disables or enables extension on current page */ const handlePowerChange = () => { dispatch({ type: 'GET_TAB' }, null, ({ hostname, id }) => { dispatch({ hostname, type: 'GET_STORE' }, null, ({ enabled }) => { dispatch({ hostname, state: { enabled: !enabled }, type: 'UPDATE_STORE' }); chrome.tabs.reload(id, { bypassCache: true }); }); }); }; /** * @description Reload current page */ const handleReload = () => { dispatch({ type: 'GET_TAB' }, null, ({ id }) => { chrome.tabs.reload(id, { bypassCache: true }); }); }; /** * @description Shows negative or positive messages * @param {MouseEvent} event */ const handleRate = (event) => { const negative = document.getElementById('negative'); const positive = document.getElementById('positive'); switch (event.currentTarget.id) { case 'unlike': positive.setAttribute('hidden', 'true'); negative.removeAttribute('hidden'); break; case 'like': negative.setAttribute('hidden', 'true'); positive.removeAttribute('hidden'); break; default: break; } }; /** * @description Setup stars handlers and result message links */ const handleContentLoaded = () => { dispatch({ type: 'GET_TAB' }, null, ({ hostname }) => { dispatch({ hostname, type: 'GET_STORE' }, null, ({ enabled }) => { translate(); const host = document.getElementById('host'); const like = document.getElementById('like'); const power = document.getElementById('power'); const reload = document.getElementById('reload'); const store = document.getElementById('store'); const unlike = document.getElementById('unlike'); like.addEventListener('click', handleRate); power.addEventListener('change', handlePowerChange); reload.addEventListener('click', handleReload); if (isEdge) store.setAttribute('href', edgeUrl); else if (isChromium) store.setAttribute('href', chromeUrl); else if (isFirefox) store.setAttribute('href', firefoxUrl); unlike.addEventListener('click', handleRate); if (location) host.innerText = hostname.replace('www.', ''); if (!enabled) power.removeAttribute('checked'); }); }); }; /** * @description Applies translations to tags with i18n data attribute */ const translate = () => { const nodes = document.querySelectorAll('[data-i18n]'); for (let i = nodes.length; i--; ) { const node = nodes[i]; const { i18n } = node.dataset; node.innerHTML = chrome.i18n.getMessage(i18n); } }; /** * @description Listen to document ready * @listens document#ready */ document.addEventListener('DOMContentLoaded', handleContentLoaded);