-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
26 lines (25 loc) · 1.01 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
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
let tabId = sender.tab.id;
chrome.storage.local.get(['countArray'], function (countTracker) {
let count = updateCount(countTracker, tabId, request.add);
let countText = count > 0 ? count : '';
chrome.browserAction.setBadgeText({text: '' + countText, tabId: tabId});
});
});
chrome.runtime.onInstalled.addListener(function (event ) {
chrome.storage.local.set({countArray: []});
});
chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
chrome.storage.local.get(['countArray'], function (countTracker) {
delete countTracker.countArray[tabId];
chrome.storage.local.set(countTracker);
});
});
function updateCount(countTracker, tabId, add) {
let count = countTracker.countArray[tabId];
count = count === undefined ? 0 : count;
count += add;
countTracker.countArray[tabId] = count;
chrome.storage.local.set(countTracker);
return count;
}