From c7538ac28e27f255bcc88e3e88ff2e719dedd2d7 Mon Sep 17 00:00:00 2001 From: Kenneth Hendricks <50819541+kenhendricks00@users.noreply.github.com> Date: Fri, 8 Nov 2024 17:23:29 -0500 Subject: [PATCH] Update background.js Updated warning toggle --- src/js/background.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/js/background.js b/src/js/background.js index cbd9777..9a818f5 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -350,12 +350,16 @@ browserAPI.runtime.onMessage.addListener((message, sender, sendResponse) => { }); async function openWarningPage(tabId, unsafeUrl) { + const normalizedUrl = normalizeUrl(unsafeUrl); const tabApprovedUrls = approvedUrls.get(tabId) || []; - if (tabApprovedUrls.includes(normalizeUrl(unsafeUrl))) { + + // Check if URL has already been approved for this tab + if (tabApprovedUrls.includes(normalizedUrl)) { console.log(`URL ${unsafeUrl} was already approved for tab ${tabId}`); return; } + // Fetch the warning page setting const { warningPage } = await browserAPI.storage.sync.get({ warningPage: true, }); @@ -365,12 +369,49 @@ async function openWarningPage(tabId, unsafeUrl) { return; } + // Redirect to the warning page if it is enabled in settings const warningPageUrl = browserAPI.runtime.getURL( `../pub/warning-page.html?url=${encodeURIComponent(unsafeUrl)}` ); browserAPI.tabs.update(tabId, { url: warningPageUrl }); } +// Add listener for approval from the warning page +browserAPI.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.action === "approveSite") { + const { tabId, url } = message; + const normalizedUrl = normalizeUrl(url); + + // Store approval for this URL in the current tab + if (tabId && normalizedUrl) { + let tabApprovedUrls = approvedUrls.get(tabId) || []; + if (!tabApprovedUrls.includes(normalizedUrl)) { + tabApprovedUrls.push(normalizedUrl); + approvedUrls.set(tabId, tabApprovedUrls); + } + console.log(`Approval stored for ${url} in tab ${tabId}`); + sendResponse({ status: "approved" }); + } + } + return true; // Indicates asynchronous response handling +}); + +// Listen for settings updates from the settings page +browserAPI.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.type === "settingsUpdated") { + console.log( + "Received settingsUpdated message in background script:", + message.settings + ); + + // Acknowledge the message by sending a response + sendResponse({ status: "Settings updated successfully" }); + + // Return true to indicate that the response will be sent asynchronously + return true; + } +}); + browserAPI.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { if (changeInfo.status === "complete" && tab.url) { checkSiteAndUpdatePageAction(tabId, tab.url);