diff --git a/scripts/GUI/entrypoints/iconindicator.js b/scripts/GUI/entrypoints/iconindicator.js index bb9c240..1086519 100644 --- a/scripts/GUI/entrypoints/iconindicator.js +++ b/scripts/GUI/entrypoints/iconindicator.js @@ -1,8 +1,7 @@ import BrowserIcon from '/scripts/GUI/browsericon.js'; -import Image from '/scripts/mapping/image.js'; import Tabs from '/scripts/GUI/tabs.js'; import texts from "/scripts/mapping/read.js"; -import {session} from '/scripts/secretariat.js'; +import {global, observe} from "/scripts/secretariat.js"; const CONFIG = chrome.runtime.getURL("styles/colors/icon.json"); @@ -12,12 +11,60 @@ class IconIndicator { */ static enable() { BrowserIcon.enable(); - (Tabs.query(null, 0)).then(async (TAB) => { - BrowserIcon.set({ - "BadgeText": await (new texts(`extensionIcon_website_loading`)).symbol, - "BadgeBackgroundColor": await fetch(CONFIG).then((response) => response.json()).then((jsonData) => {return (jsonData[`loading`]);}) - }, {"tabId": TAB.id}); + + // Enable icon changes if enabled within the settings. + global.read([`settings`, `behavior`, `showApplicable`]).then(async (PREFERENCE) => {(PREFERENCE) + ? fetch(CONFIG).then((response) => response.json()).then((jsonData) => { + const ICON_COLORS = jsonData; + + /* + Show an iconified summary of the results. + + @param {string} location the URL of the page + @param {string} ID the tab's ID + */ + function showDetails(location, ID) { + let LOCATION = location; + // If the tab data is ready, change the icon to reflect the results. + global.read([`sites`, LOCATION, `status`]).then(async (STATUS) => { + if (STATUS) { + (STATUS[`error`]) ? BrowserIcon.set({ + "BadgeText": await (new texts(`extensionIcon_error`)).symbol, + "BadgeBackgroundColor": ICON_COLORS[`error`] + }, {"tabId": ID}) : false; + + if (STATUS[`done`]) { + global.read([`sites`, LOCATION, `analysis`, `Rating`, `Trust`]).then(async (RESULTS) => { + ((typeof RESULTS).includes(`str`)) ? RESULTS.trim() : false; + (RESULTS) ? BrowserIcon.set({ + "BadgeText": await (new texts(`extensionIcon_product_`.concat(RESULTS))).symbol, + "BadgeBackgroundColor": ICON_COLORS[`product_`.concat(RESULTS)] + }, {"tabId": ID}) : false; + }) + }; + }; + }); + } + + + (Tabs.query(null, 0)).then(async (TAB) => { + // Get the URL of the tab. + let LOCATION = TAB.url; + + BrowserIcon.set({ + "BadgeText": await (new texts(`extensionIcon_website_loading`)).symbol, + "BadgeBackgroundColor": ICON_COLORS[`loading`] + }, {"tabId": TAB.id}); + + showDetails(LOCATION, TAB.id); + observe((changes) => { + showDetails(LOCATION, TAB.id); + }) + }); + }) + : false; }) + } /* @@ -25,11 +72,15 @@ class IconIndicator { */ static disable() { BrowserIcon.disable(); - (Tabs.query(null, 0)).then(async (TAB) => { - BrowserIcon.set({ - "BadgeText": await (new texts(`extensionIcon_website_unsupported`)).symbol, - "BadgeBackgroundColor": await fetch(CONFIG).then((response) => response.json()).then((jsonData) => {return (jsonData[`N/A`]);}) - }, {"tabId": TAB.id}); + + // Enable icon changes if enabled within the settings. + global.read([`settings`, `behavior`, `showApplicable`]).then((PREFERENCE) => { + (Tabs.query(null, 0)).then(async (TAB) => { + BrowserIcon.set({ + "BadgeText": await (new texts(`extensionIcon_website_unsupported`)).symbol, + "BadgeBackgroundColor": await fetch(CONFIG).then((response) => response.json()).then((jsonData) => {return (jsonData[`N/A`]);}) + }, {"tabId": TAB.id}); + }) }) } @@ -41,6 +92,21 @@ class IconIndicator { static set(callback) { BrowserIcon.addActionListener("onClicked", callback); } + + /* + The action when the icon is clicked. + */ + static onclick() { + // Check if autorunning is not enabled. + (global.read([`settings`, `behavior`, `autoRun`])).then((result) => { + if (!result) { + (Tabs.query(null, 0)).then((TAB) => { + // Tell the content script to begin scraping the page. + chrome.tabs.sendMessage(TAB.id, {"refresh": true}); + }); + } + }); + } } export {IconIndicator as default}; \ No newline at end of file