diff --git a/platform/chromium/pub/index.js b/platform/chromium/pub/index.js index 813d690..9b3d09c 100644 --- a/platform/chromium/pub/index.js +++ b/platform/chromium/pub/index.js @@ -19,52 +19,78 @@ document.addEventListener("DOMContentLoaded", () => { return `${urlObj.protocol}//${urlObj.hostname}`; } - try { - // Get the active tab's URL - chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { - const activeTab = tabs[0]; - - if (!activeTab || !activeTab.url) { - throw new Error("No active tab found or URL is unavailable."); + // Function to apply theme based on settings + function applyTheme() { + chrome.storage.sync.get("theme", ({ theme }) => { + if (theme === "dark") { + document.body.setAttribute("data-theme", "dark"); + } else if (theme === "light") { + document.body.setAttribute("data-theme", "light"); + } else { + const prefersDark = window.matchMedia( + "(prefers-color-scheme: dark)" + ).matches; + document.body.setAttribute( + "data-theme", + prefersDark ? "dark" : "light" + ); } + }); + } - const currentUrl = normalizeUrl(activeTab.url); - const rootDomain = extractRootDomain(currentUrl); - console.log(`Active tab URL: ${currentUrl}, Root domain: ${rootDomain}`); - - // Send a message to the background script to check the site's status - chrome.runtime.sendMessage( - { action: "checkSiteStatus", url: currentUrl }, - (response) => { - if (!response || !response.status) { - throw new Error( - "Failed to retrieve site status from the background script." - ); - } + // Apply theme on load + applyTheme(); - // Determine if the root domain or the full URL is marked as safe/starred - chrome.runtime.sendMessage( - { action: "checkSiteStatus", url: rootDomain }, - (isRootDomainMarked) => { - // Handle different site statuses and update the UI accordingly - if ( - isRootDomainMarked.status === response.status && - response.status !== "no_data" - ) { - // If both the root domain and the current URL share the same status, show the root domain in the message - handleStatusUpdate(response.status, rootDomain); - } else { - // Otherwise, show the current URL in the message - handleStatusUpdate(response.status, currentUrl); - } - } + // Get the active tab's URL + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + if (chrome.runtime.lastError || !tabs[0] || !tabs[0].url) { + handleError("No active tab found or URL is unavailable."); + return; + } + + const currentUrl = normalizeUrl(tabs[0].url); + const rootDomain = extractRootDomain(currentUrl); + console.log(`Active tab URL: ${currentUrl}, Root domain: ${rootDomain}`); + + // Send a message to the background script to check the site's status + chrome.runtime.sendMessage( + { action: "checkSiteStatus", url: currentUrl }, + (response) => { + if (chrome.runtime.lastError || !response || !response.status) { + handleError( + "Failed to retrieve site status from the background script." ); + return; } - ); - }); - } catch (error) { - console.error("Error while checking site status:", error); - errorMessage.textContent = `Error: ${error.message}`; + + // Determine if the root domain or the full URL is marked as safe/starred + chrome.runtime.sendMessage( + { action: "checkSiteStatus", url: rootDomain }, + (isRootDomainMarked) => { + if (chrome.runtime.lastError || !isRootDomainMarked) { + handleError("Failed to check root domain status."); + return; + } + + if ( + isRootDomainMarked.status === response.status && + response.status !== "no_data" + ) { + // If both the root domain and the current URL share the same status, show the root domain in the message + handleStatusUpdate(response.status, rootDomain); + } else { + // Otherwise, show the current URL in the message + handleStatusUpdate(response.status, currentUrl); + } + } + ); + } + ); + }); + + function handleError(message) { + console.error(message); + errorMessage.textContent = `Error: ${message}`; updateUI("error", "An error occurred while retrieving the site status."); } @@ -87,6 +113,12 @@ document.addEventListener("DOMContentLoaded", () => { `${displayUrl} is potentially unsafe. Proceed with caution.` ); break; + case "fmhy": + updateUI( + "fmhy", + `${displayUrl} is an FMHY related site. Proceed confidently.` + ); + break; case "safe": updateUI("safe", `${displayUrl} is safe to browse.`); break; @@ -119,6 +151,7 @@ document.addEventListener("DOMContentLoaded", () => { const icons = { unsafe: "../res/icons/unsafe.png", potentially_unsafe: "../res/icons/potentially_unsafe.png", + fmhy: "../res/icons/fmhy.png", safe: "../res/icons/safe.png", starred: "../res/icons/starred.png", no_data: "../res/ext_icon_144.png", @@ -136,4 +169,10 @@ document.addEventListener("DOMContentLoaded", () => { console.log(`UI updated: ${message}`); } + + // Add settings button functionality + document.getElementById("settingsButton").addEventListener("click", () => { + // Open the settings page in a new tab + chrome.runtime.openOptionsPage(); + }); });