From c6b4b49055faa024cba5c1e7cc64c9283ec9e53c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rard=20Kieffer?= <60404287+RetroYogi@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:07:41 +0100 Subject: [PATCH] Simplified codebase Simplified codebase by removing message passing between content and background scripts --- background.js | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/background.js b/background.js index b85b15f..5f5e1b6 100644 --- a/background.js +++ b/background.js @@ -5,6 +5,7 @@ let pinnedTabs = {}; function isDifferentDomain(url1, url2) { return new URL(url1).hostname !== new URL(url2).hostname; } + // Function to update pinned tabs upon extension activation function updatePinnedTabs() { chrome.tabs.query({}, (tabs) => { @@ -15,6 +16,7 @@ function updatePinnedTabs() { } }); } + // Call this function after background script loads updatePinnedTabs(); @@ -47,39 +49,17 @@ chrome.tabs.onCreated.addListener((tab) => { } }); -// Listen for navigation events +// Listen for navigation events - this now handles both address bar navigation +// and link clicks chrome.webNavigation.onBeforeNavigate.addListener((details) => { if (details.frameId !== 0) return; // Only handle main frame navigation chrome.tabs.get(details.tabId, (tab) => { - if (tab.pinned) { // Check if the tab is pinned before proceeding - if (isDifferentDomain(tab.url, details.url)) { - // Cancel the navigation in the pinned tab - chrome.tabs.update(details.tabId, { url: tab.url }); - // Open the new URL in a new tab - chrome.tabs.create({ url: details.url, index: tab.index + 1 }); - } + if (tab.pinned && isDifferentDomain(tab.url, details.url)) { + // Cancel the navigation in the pinned tab + chrome.tabs.update(details.tabId, { url: tab.url }); + // Open the new URL in a new tab + chrome.tabs.create({ url: details.url, index: tab.index + 1 }); } }); }); - - -// Listen for messages from content script -chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { - if (request.action === "openInNewTab") { - chrome.tabs.get(sender.tab.id, (tab) => { - if (tab.pinned && isDifferentDomain(tab.url, request.url)) { - chrome.tabs.create({ url: request.url, index: tab.index + 1 }, (newTab) => { - if (newTab) { // Check if new tab creation was successful - sendResponse({ success: true }); - } else { - sendResponse({ success: false }); // Handle potential errors - } - }); - } else { - sendResponse({ success: false }); - } - }); - return true; // Indicates we will send a response asynchronously - } -});