Skip to content

Commit

Permalink
Fixed issue with extension if tab URL is changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 10, 2019
1 parent d3f3fb4 commit 7eacd78
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions chrome-ext-files/js/background.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
(function () {
const MAX_EXT_LOAD_COUNT = 30;
var curTabId = null;
let curTab = {
id: null,
url: null,
};

const getExtensionId = () => {
const matches = chrome.runtime.getURL('x').match(/.*\/\/(.*)\/x$/);
if (matches) {
// https://stackoverflow.com/a/47060021/3929126
// Mozilla uses an internal UUID on every installation to prevent fingerprinting
return matches[1];
}
return chrome.runtime.id;
}

// Create a new tab for the extension
function createNewTab() {
chrome.tabs.create({ url: 'index.html' }, function (tab) {
curTabId = tab.id;
curTab = {
id: tab.id,
url: tab.url
}

// Handle donation logic
handleDonation();
// handleDonation();
});
}

Expand Down Expand Up @@ -61,12 +77,13 @@

// Open the extension tab when the extension icon is clicked
chrome.browserAction.onClicked.addListener(function (tab) {
if (!curTabId) {
if (!curTab || !curTab.id) {
createNewTab();
} else {
chrome.tabs.get(curTabId, function (tab) {
if (tab) {
focusTab(curTabId);
chrome.tabs.get(curTab.id, function (tab) {
console.log(chrome.runtime.id, tab.url);
if (tab && tab.url && tab.url.includes(getExtensionId())) {
focusTab(curTab.id);
} else {
createNewTab();
}
Expand All @@ -76,8 +93,8 @@

// When a tab is closed, check if it is the extension tab that was closed, and unset curTabId
chrome.tabs.onRemoved.addListener(function (tabId) {
if (tabId === curTabId) {
curTabId = null;
if (tabId === curTab.id) {
curTab = {};
}
});

Expand Down

0 comments on commit 7eacd78

Please sign in to comment.