-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
47 lines (44 loc) · 1.29 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
let intercomTabId = null;
const MAX_ATTEMPTS = 3;
let attemptCount = 0;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "openPopup") {
chrome.action.openPopup();
} else if (request.action === "openIntercomTab") {
openOrUpdateIntercomTab(request.url);
}
});
function openOrUpdateIntercomTab(url) {
if (intercomTabId === null) {
chrome.tabs.create({ url: url }, (tab) => {
intercomTabId = tab.id;
attemptCount = 0;
});
} else {
chrome.tabs.get(intercomTabId, (tab) => {
if (chrome.runtime.lastError) {
// Tab doesn't exist anymore
if (attemptCount < MAX_ATTEMPTS) {
chrome.tabs.create({ url: url }, (newTab) => {
intercomTabId = newTab.id;
attemptCount++;
});
} else {
console.error('Max attempts reached. Please try again later.');
attemptCount = 0;
}
} else {
// Tab exists, update its URL and focus it
chrome.tabs.update(intercomTabId, { url: url, active: true });
attemptCount = 0;
}
});
}
}
// Listen for tab removal to reset intercomTabId
chrome.tabs.onRemoved.addListener((tabId) => {
if (tabId === intercomTabId) {
intercomTabId = null;
attemptCount = 0;
}
});