From 2eeeb48041033840f065c50ce2998045ca69ab98 Mon Sep 17 00:00:00 2001 From: Adam Coyne Date: Sun, 2 Mar 2025 10:38:04 -0600 Subject: [PATCH] Update tab instead of create; close with timeout --- browser-extensions/chromium/background.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/browser-extensions/chromium/background.js b/browser-extensions/chromium/background.js index 359f429..f85476b 100644 --- a/browser-extensions/chromium/background.js +++ b/browser-extensions/chromium/background.js @@ -8,23 +8,26 @@ const BtProtoPrefix = "x-bt://"; -async function openInBT(url) { +async function openInBT(tabId, url) { const destUrl = BtProtoPrefix + url; - await chrome.tabs.create({ url: destUrl }); + await chrome.tabs.update(tabId, { url: destUrl }); } chrome.action.onClicked.addListener((activeTab) => { const url = activeTab.url; // as we are sending the url to BT, we can close the current tab - chrome.tabs.remove(activeTab.id); - - openInBT(url); + openInBT(activeTab.id, url).then(() => { + // using a timeout because I can't find a reliable way to wait for the custom protocol to be handled + setTimeout(() => { + chrome.tabs.remove(activeTab.id); + }, 250); + }); }); // context menu item click handler chrome.contextMenus.onClicked.addListener((info, tab) => { const url = info.linkUrl; - openInBT(url); + openInBT(tab.id, url); }); // add context menu item for a hyperlink (contexts: link)