-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
134 lines (114 loc) · 4.07 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
let tabInfoList = {}
// Synchronize tabInfoList with active tabs
function syncActiveTabs() {
chrome.tabs.query({}, function (tabs) {
let tabInfoListUpdated = false;
// Remove inactive tabs
for (let tabKey in tabInfoList) {
let tabStillExists = tabs.some(
(tab) => tabKey === generateTabKey(tab.url, tab.title)
);
if (!tabStillExists) {
delete tabInfoList[tabKey];
tabInfoListUpdated = true;
}
}
// Update tabInfoList with opened tabs
tabs.forEach(function (tab) {
let tabKey = generateTabKey(tab.url, tab.title);
let isNewTab = !tabInfoList.hasOwnProperty(tabKey);
let tabInfo = tabInfoList[tabKey] || {
id: tab.id,
windowId: tab.windowId,
title: tab.title,
url: tab.url,
firstOpened: new Date().toLocaleString(),
lastOpened: new Date().toLocaleString(),
};
if (!isNewTab) {
tabInfo.title = tab.title;
tabInfo.id = tab.id;
tabInfo.windowId = tab.windowId; // Update the windowId
// Update the lastOpened property only when the tab is active
if (tab.active) {
tabInfo.lastOpened = new Date().toLocaleString();
}
}
tabInfoList[tabKey] = tabInfo;
tabInfoListUpdated = true;
});
if (tabInfoListUpdated) {
chrome.storage.local.set({ tabInfoList: tabInfoList });
}
});
}
// Generate a unique key for the tab based on its URL and title
function generateTabKey(url, title) {
return `${url}_${title}`;
}
// Update tabInfo when the tab is updated or activated
function updateTabInfo(tabId, changeInfo, tab) {
if (changeInfo.status === "complete" || changeInfo.title || changeInfo.url || changeInfo.active === true) {
let tabKey = generateTabKey(tab.url, tab.title);
let tabInfo = tabInfoList[tabKey] || {};
// Check if the tabKey has changed (e.g., due to a title or URL update)
if (tabInfoList.hasOwnProperty(tabKey)) {
tabInfo = tabInfoList[tabKey];
} else {
// Remove the old tabInfo entry if the tabKey has changed
for (let oldTabKey in tabInfoList) {
if (tabInfoList[oldTabKey].id === tabId) {
delete tabInfoList[oldTabKey];
break;
}
}
tabInfo.firstOpened = new Date().toLocaleString();
}
tabInfo.id = tab.id;
tabInfo.windowId = tab.windowId;
tabInfo.title = tab.title;
tabInfo.url = tab.url;
tabInfo.lastOpened = new Date().toLocaleString();
tabInfoList[tabKey] = tabInfo;
chrome.storage.local.set({ tabInfoList: tabInfoList });
}
}
// Listener for onActivated event
function onTabActivated(activeInfo) {
chrome.tabs.get(activeInfo.tabId, function (tab) {
updateTabInfo(tab.id, { active: true }, tab);
});
}
// Listener for onRemoved event
function onTabRemoved(tabId, removeInfo) {
for (let tabKey in tabInfoList) {
if (tabInfoList[tabKey].id === tabId) {
delete tabInfoList[tabKey];
break;
}
}
chrome.storage.local.set({ tabInfoList: tabInfoList });
}
chrome.storage.local.get("tabInfoList", function (result) {
tabInfoList = result.tabInfoList || {};
syncActiveTabs();
});
// Listener for messages from the popup
function onMessageReceived(request, sender, sendResponse) {
if (request === "getTabInfo") {
// Retrieve tabInfoList from local storage
chrome.storage.local.get("tabInfoList", function (result) {
tabInfoList = result.tabInfoList || {};
syncActiveTabs();
// Send the retrieved tabInfoList to the popup
sendResponse(tabInfoList);
});
// Return true to indicate that you will send a response asynchronously
return true;
}
}
// Add event listeners
chrome.tabs.onUpdated.addListener(updateTabInfo);
chrome.tabs.onActivated.addListener(onTabActivated);
chrome.tabs.onRemoved.addListener(onTabRemoved);
chrome.runtime.onMessage.addListener(onMessageReceived);