2022-10-17 14:48:27 +02:00
|
|
|
let currentTab = {
|
|
|
|
id: null,
|
|
|
|
url: null,
|
|
|
|
};
|
2022-10-17 00:27:25 +02:00
|
|
|
|
2022-10-17 14:48:27 +02:00
|
|
|
const getExtensionId = () => {
|
|
|
|
const matches = chrome.runtime.getURL('x').match(/.*\/\/(.*)\/x$/);
|
|
|
|
if (matches) {
|
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return chrome.runtime.id;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a new tab for the extension
|
|
|
|
function createNewTab() {
|
|
|
|
chrome.tabs.create({ url: 'index.html' }, function (tab) {
|
|
|
|
currentTab = {
|
|
|
|
id: tab.id,
|
|
|
|
url: tab.url
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2022-10-17 00:27:25 +02:00
|
|
|
|
2022-10-17 14:48:27 +02:00
|
|
|
// Focus on the open extension tab
|
|
|
|
function focusTab(tabId) {
|
|
|
|
var updateProperties = { "active": true };
|
|
|
|
chrome.tabs.update(tabId, updateProperties, function (tab) { });
|
|
|
|
}
|
2022-10-17 00:27:25 +02:00
|
|
|
|
2022-10-17 14:48:27 +02:00
|
|
|
// Open the extension tab when the extension icon is clicked
|
|
|
|
chrome.action.onClicked.addListener(function (tab) {
|
|
|
|
if (!currentTab || !currentTab.id) {
|
|
|
|
createNewTab();
|
|
|
|
} else {
|
|
|
|
chrome.tabs.get(currentTab.id, function (tab) {
|
|
|
|
console.log(chrome.runtime.id, tab.url);
|
|
|
|
if (tab && tab.url && tab.url.includes(getExtensionId())) {
|
|
|
|
focusTab(currentTab.id);
|
|
|
|
} else {
|
|
|
|
createNewTab();
|
|
|
|
}
|
2022-10-17 00:27:25 +02:00
|
|
|
});
|
|
|
|
}
|
2022-10-17 14:48:27 +02:00
|
|
|
});
|
2022-10-17 00:27:25 +02:00
|
|
|
|
2022-10-17 14:48:27 +02:00
|
|
|
// When a tab is closed, check if it is the extension tab that was closed, and unset currentTabId
|
|
|
|
chrome.tabs.onRemoved.addListener(function (tabId) {
|
|
|
|
if (tabId === currentTab.id) {
|
|
|
|
currentTab = {};
|
2022-10-17 00:27:25 +02:00
|
|
|
}
|
2022-10-17 14:48:27 +02:00
|
|
|
});
|