-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
116 lines (98 loc) · 3.47 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
let data = {};
// This function only runs once when the extension is installed or updated
chrome.runtime.onInstalled.addListener(function () {
chrome.storage.local.get(null, function (result) {
data = result;
data.totalTabs = data.totalTabs || 0;
// Initialize tab count to the current number of tabs in the browser
chrome.tabs.query({}, function (tabs) {
data.tabCount = tabs.length;
updateBadge();
chrome.storage.local.set(data);
});
});
});
// Listen for the message and respond to it
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "getData") {
updateTabAndWindowCounts();
let tabCount = 0;
let windowCount = 0;
chrome.windows.getAll({ populate: true }, function (windows) {
windowCount = windows.length;
windows.forEach(function (window) {
tabCount += window.tabs.length;
});
let date = new Date().toLocaleDateString();
chrome.storage.local.get(null, function (result) {
data = result;
data[date] = { tabs: tabCount, windows: windowCount };
chrome.storage.local.set(
{
totalTabs: data.totalTabs,
[date]: data[date],
windows: windowCount,
},
function () {
// Send a response to the popup with the updated data
sendResponse({
tabCount: tabCount,
windowCount: windowCount,
result: result,
});
}
);
});
});
// Return true to indicate that the sendResponse callback will be called asynchronously
return true;
}
});
function updateTabAndWindowCounts() {
chrome.windows.getAll({ populate: true }, function (windows) {
let currentDate = new Date().toLocaleDateString();
let windowCount = windows.length;
let tabCount = 0;
windows.forEach(function (window) {
tabCount += window.tabs.length;
});
// Update the stored data with the tab and window counts for the current day
chrome.storage.local.get(null, function (result) {
data = result;
data[currentDate] = { tabs: tabCount, windows: windowCount };
// Update the totalTabs count (increment only if tabCount increased)
if (tabCount > data.tabCount) {
data.totalTabs = (data.totalTabs || 0) + 1;
}
// Store the updated data in local storage
chrome.storage.local.set(data);
});
});
}
// Function to update the badge
function updateBadge() {
chrome.storage.local.get({ showTabsNumber: true }, function (result) {
const showTabsNumber = result.showTabsNumber;
if (showTabsNumber) {
chrome.tabs.query({}, function (tabs) {
const tabCount = tabs.length;
chrome.action.setBadgeText({ text: tabCount.toString() });
});
} else {
chrome.windows.getAll({ populate: true }, function (windows) {
const windowCount = windows.length;
chrome.action.setBadgeText({ text: windowCount.toString() });
});
}
});
}
// Listen for tab and window events to update the badge and trigger counting
function updateBadgeAndCount() {
updateBadge();
updateTabAndWindowCounts();
}
// Listen for tab and window events to update the badge and trigger counting
chrome.tabs.onCreated.addListener(updateBadgeAndCount);
chrome.tabs.onRemoved.addListener(updateBadgeAndCount);
chrome.windows.onCreated.addListener(updateBadgeAndCount);
chrome.windows.onRemoved.addListener(updateBadgeAndCount);