Skip to content

Commit

Permalink
Simplified codebase
Browse files Browse the repository at this point in the history
Simplified codebase by removing message passing between content and background scripts
  • Loading branch information
RetroYogi authored Dec 3, 2024
1 parent e9abef1 commit c6b4b49
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -15,6 +16,7 @@ function updatePinnedTabs() {
}
});
}

// Call this function after background script loads
updatePinnedTabs();

Expand Down Expand Up @@ -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
}
});

0 comments on commit c6b4b49

Please sign in to comment.