Skip to content

Commit

Permalink
Merge pull request #1 from RetroYogi/1.0.3
Browse files Browse the repository at this point in the history
1.0.3
  • Loading branch information
RetroYogi authored Jul 8, 2024
2 parents 4ab7769 + 4723909 commit 8898e60
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
38 changes: 30 additions & 8 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ 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) => {
for (const tab of tabs) {
if (tab.pinned) {
pinnedTabs[tab.id] = { url: tab.url, index: tab.index };
}
}
});
}
// Call this function after background script loads
updatePinnedTabs();

// Listen for tab updates
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (tab.pinned) {
pinnedTabs[tabId] = { url: tab.url, index: tab.index };
} else {
delete pinnedTabs[tabId]; // Remove from pinnedTabs if unpinned
}
});

Expand Down Expand Up @@ -38,26 +52,34 @@ chrome.webNavigation.onBeforeNavigate.addListener((details) => {
if (details.frameId !== 0) return; // Only handle main frame navigation

chrome.tabs.get(details.tabId, (tab) => {
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 });
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 });
}
}
});
});


// 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 });
sendResponse({ success: true });
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
}
});
});
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 1.0.0
Initial release

# 1.0.3
## Bug fixes
- Improvement: Added functionality to identify pinned tabs upon extension activation. This ensures the extension immediately recognizes which tabs are protected. (Implemented in background.js)
- Fix: Resolved an issue where the extension blocked CMD-W (or Ctrl-W) for tabs that were previously pinned and then unpinned. Now, the extension only blocks the shortcut for currently pinned tabs. (Modified background.js)
- Fix: Addressed a bug in content.js that caused a "TypeError: Cannot read properties of undefined (reading 'query')" error. The script now checks for extension availability before using the chrome.tabs API. (Modified content.js)
- Fixed an issue where clicking a link to a different domain inside a pinned tab sometimes opened two new tabs instead of one.
16 changes: 10 additions & 6 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Intercept link clicks
function isExtensionReady() {
return !!chrome.tabs && !!chrome.tabs.query; // Check for both chrome.tabs and its query function
}

document.addEventListener('click', (event) => {
const link = event.target.closest('a');
if (link && link.href) {
chrome.runtime.sendMessage({ action: "openInNewTab", url: link.href }, (response) => {
if (response.success) {
event.preventDefault();
if (link && link.href && isExtensionReady()) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0].pinned) {
chrome.runtime.sendMessage({ action: "openInNewTab", url: link.href });
event.preventDefault(); // Prevent default behavior immediately
}
});
}
});
});
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Besser Pinned Tabs",
"version": "1.0",
"version": "1.0.3",
"description": "Protects pinned tabs by quickly reopening them if closed",
"permissions": [
"tabs",
Expand Down Expand Up @@ -29,4 +29,4 @@
"48": "icon48.png",
"128": "icon128.png"
}
}
}

0 comments on commit 8898e60

Please sign in to comment.