From ae7f4571e16aaf50e68b0a38fb3c2d7a53c17049 Mon Sep 17 00:00:00 2001
From: Kenneth Hendricks <50819541+kenhendricks00@users.noreply.github.com>
Date: Sun, 10 Nov 2024 18:13:05 -0500
Subject: [PATCH] Add files via upload
---
src/js/background.js | 76 ++++++++++++++++++--------------------------
src/pub/index.js | 40 +++++++++++------------
2 files changed, 49 insertions(+), 67 deletions(-)
diff --git a/src/js/background.js b/src/js/background.js
index eced524..ba1b674 100644
--- a/src/js/background.js
+++ b/src/js/background.js
@@ -248,29 +248,22 @@ function checkSiteAndUpdatePageAction(tabId, url) {
return;
}
- // Check if the site is starred
- if (starredSites.includes(rootUrl) || starredSites.includes(normalizedUrl)) {
- updatePageAction("starred", tabId);
- return;
+ // Check if the full URL is starred or has a specific status
+ let status = getStatusFromLists(normalizedUrl);
+ let matchedUrl = normalizedUrl;
+
+ // If no specific match for the full URL, check the root URL
+ if (status === "no_data") {
+ status = getStatusFromLists(rootUrl);
+ matchedUrl = rootUrl;
}
- // Check if the site is unsafe and not approved
- const tabApprovedUrls = approvedUrls.get(tabId) || [];
- const isApproved = tabApprovedUrls.includes(rootUrl);
+ // Apply the correct icon status to the tab
+ updatePageAction(status, tabId);
- if (unsafeSitesRegex?.test(rootUrl) && !isApproved) {
- updatePageAction("unsafe", tabId);
+ // Handle unsafe sites that need warning page redirection if not approved
+ if (status === "unsafe" && !approvedUrls.get(tabId)?.includes(rootUrl)) {
openWarningPage(tabId, rootUrl);
- } else if (unsafeSitesRegex?.test(rootUrl) && isApproved) {
- updatePageAction("unsafe", tabId);
- } else if (potentiallyUnsafeSitesRegex?.test(rootUrl)) {
- updatePageAction("potentially_unsafe", tabId);
- } else if (fmhySitesRegex?.test(rootUrl)) {
- updatePageAction("fmhy", tabId);
- } else if (safeSites.includes(rootUrl)) {
- updatePageAction("safe", tabId);
- } else {
- updatePageAction("default", tabId);
}
}
@@ -332,39 +325,32 @@ async function setupUpdateSchedule() {
// Event Listeners
browserAPI.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "checkSiteStatus") {
- const normalizedUrl = normalizeUrl(message.url.trim());
- const rootUrl = extractRootUrl(normalizedUrl);
+ const { url, rootUrl } = message;
- let isUnsafe =
- unsafeSitesRegex?.test(rootUrl) || unsafeSitesRegex?.test(normalizedUrl);
- let isPotentiallyUnsafe =
- potentiallyUnsafeSitesRegex?.test(rootUrl) ||
- potentiallyUnsafeSitesRegex?.test(normalizedUrl);
- let isFMHY =
- fmhySitesRegex?.test(rootUrl) || fmhySitesRegex?.test(normalizedUrl);
- let isStarred =
- starredSites.includes(rootUrl) || starredSites.includes(normalizedUrl);
- let isSafe =
- safeSites.includes(rootUrl) || safeSites.includes(normalizedUrl);
+ // Attempt to match with the full URL first (for specific paths)
+ let status = getStatusFromLists(url);
+ let matchedUrl = url;
- let status = "no_data";
- if (isFMHY) {
- status = "fmhy";
- } else if (isStarred) {
- status = "starred";
- } else if (isUnsafe) {
- status = "unsafe";
- } else if (isPotentiallyUnsafe) {
- status = "potentially_unsafe";
- } else if (isSafe) {
- status = "safe";
+ // If no specific match, try the root URL
+ if (status === "no_data") {
+ status = getStatusFromLists(rootUrl);
+ matchedUrl = rootUrl;
}
- sendResponse({ status: status });
- return true; // Indicates asynchronous response handling
+ sendResponse({ status, matchedUrl });
+ return true;
}
});
+function getStatusFromLists(url) {
+ if (unsafeSitesRegex?.test(url)) return "unsafe";
+ if (potentiallyUnsafeSitesRegex?.test(url)) return "potentially_unsafe";
+ if (fmhySitesRegex?.test(url)) return "fmhy";
+ if (starredSites.includes(url)) return "starred";
+ if (safeSites.includes(url)) return "safe";
+ return "no_data";
+}
+
async function openWarningPage(tabId, unsafeUrl) {
const normalizedUrl = normalizeUrl(unsafeUrl);
const tabApprovedUrls = approvedUrls.get(tabId) || [];
diff --git a/src/pub/index.js b/src/pub/index.js
index 4dacee5..7555d2d 100644
--- a/src/pub/index.js
+++ b/src/pub/index.js
@@ -7,12 +7,10 @@ document.addEventListener("DOMContentLoaded", async () => {
const browserAPI = typeof browser !== "undefined" ? browser : chrome;
- // URLs of known extension pages
const warningPageUrl = browserAPI.runtime.getURL("pub/warning-page.html");
const settingsPageUrl = browserAPI.runtime.getURL("pub/settings-page.html");
const welcomePageUrl = browserAPI.runtime.getURL("pub/welcome-page.html");
- // Apply theme based on settings
async function applyTheme() {
try {
const { theme } = await browserAPI.storage.sync.get("theme");
@@ -40,21 +38,22 @@ document.addEventListener("DOMContentLoaded", async () => {
}
const currentUrl = activeTab.url;
+ const rootUrl = extractRootUrl(currentUrl);
- // Check if the URL is an extension page by checking if it starts with known extension page URLs
if (
currentUrl.startsWith(warningPageUrl) ||
currentUrl === settingsPageUrl ||
currentUrl === welcomePageUrl
) {
handleStatusUpdate("extension_page", currentUrl);
- return; // Skip further processing since it's an internal page
+ return;
}
- // Send a message to the background script to check the site's status
+ // Send both the full URL and root URL to the background for status checking
const response = await browserAPI.runtime.sendMessage({
action: "checkSiteStatus",
- url: currentUrl,
+ url: currentUrl, // full path URL
+ rootUrl: rootUrl, // root domain URL
});
if (!response || !response.status) {
@@ -63,18 +62,15 @@ document.addEventListener("DOMContentLoaded", async () => {
);
}
- handleStatusUpdate(response.status, currentUrl);
+ // Display the appropriate URL in the popup
+ const displayUrl = response.matchedUrl || rootUrl;
+ handleStatusUpdate(response.status, displayUrl);
} catch (error) {
console.error("Error while checking site status:", error);
errorMessage.textContent = `Error: ${error.message}`;
updateUI("error", "An error occurred while retrieving the site status.");
}
- /**
- * Updates the UI based on the site status
- * @param {string} status - The status of the site (e.g., "safe", "unsafe")
- * @param {string} displayUrl - The URL or root domain to display in the message
- */
function handleStatusUpdate(status, displayUrl) {
let message;
@@ -95,7 +91,6 @@ document.addEventListener("DOMContentLoaded", async () => {
message = `${displayUrl} is a starred site.`;
break;
case "extension_page":
- // Set specific messages for each known extension page
if (displayUrl.startsWith(warningPageUrl)) {
message =
"You are on the Warning Page. This page warns you about potentially unsafe sites.";
@@ -119,11 +114,6 @@ document.addEventListener("DOMContentLoaded", async () => {
updateUI(status, message);
}
- /**
- * Updates the UI with the appropriate icon, message, and effects.
- * @param {string} status - The status of the site (e.g., "safe", "unsafe").
- * @param {string} message - The message to display to the user.
- */
function updateUI(status, message) {
const icons = {
unsafe: "../res/icons/unsafe.png",
@@ -137,20 +127,26 @@ document.addEventListener("DOMContentLoaded", async () => {
unknown: "../res/ext_icon_144.png",
};
- // Update the icon and message
statusIcon.src = icons[status] || icons["unknown"];
statusMessage.innerHTML = message || "An unknown error occurred.";
- // Add a small animation when the status changes
statusIcon.classList.add("active");
setTimeout(() => statusIcon.classList.remove("active"), 300);
console.log(`UI updated: ${message}`);
}
- // Add settings button functionality
document.getElementById("settingsButton").addEventListener("click", () => {
- // Open the settings page in a new tab
browserAPI.runtime.openOptionsPage();
});
+
+ function extractRootUrl(url) {
+ try {
+ const urlObj = new URL(url);
+ return `${urlObj.protocol}//${urlObj.hostname}`;
+ } catch (error) {
+ console.warn(`Failed to extract root URL from: ${url}`);
+ return url;
+ }
+ }
});