-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
239 lines (211 loc) · 7.51 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Open options page when clicking extension icon
chrome.action.onClicked.addListener(() => {
chrome.runtime.openOptionsPage();
});
// Handle browser startup
chrome.runtime.onStartup.addListener(() => {
restorePinnedTabs();
});
// Handle extension installation/update
chrome.runtime.onInstalled.addListener(() => {
restorePinnedTabs();
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.hasOwnProperty('pinned')) {
handlePinStatusChange(tab);
}
});
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
// We'll check if this was a pinned tab that was removed
chrome.storage.local.get('pinnedTabs', (data) => {
const tabs = data.pinnedTabs || [];
chrome.tabs.get(tabId, (tab) => {
if (chrome.runtime.lastError) {
// Tab doesn't exist anymore, we need to check our stored tabs
const removedTab = tabs.find(t => t.id === tabId);
if (removedTab) {
removeFromStorage(removedTab.url);
}
}
});
});
});
function handlePinStatusChange(tab) {
chrome.storage.local.get('pinnedTabs', (data) => {
const tabs = data.pinnedTabs || [];
const tabIndex = tabs.findIndex(t => t.url === tab.url);
if (!tab.pinned && tabIndex !== -1) {
// Tab was unpinned, remove it from storage
const newTabs = tabs.filter(t => t.url !== tab.url);
chrome.storage.local.set({ pinnedTabs: newTabs }, () => {
// Notify options page to update
chrome.runtime.sendMessage({
action: 'tabsUpdated',
removed: tab.url
});
});
} else if (tab.pinned && tabIndex === -1) {
// Tab was pinned, add it to storage if not already there
addToStorage(tab);
}
});
}
function handleTabError(error) {
console.error('Tab operation error:', error);
if (error.message.includes('No tab with id')) {
return;
}
}
function removeFromStorage(url) {
chrome.storage.local.get('pinnedTabs', (data) => {
const tabs = data.pinnedTabs || [];
const newTabs = tabs.filter(tab => tab.url !== url);
chrome.storage.local.set({ pinnedTabs: newTabs }, () => {
// Try to unpin any existing tabs with this URL
chrome.tabs.query({ url: url }, (existingTabs) => {
existingTabs.forEach(tab => {
chrome.tabs.update(tab.id, { pinned: false })
.catch(handleTabError);
});
});
// Notify options page to update
chrome.runtime.sendMessage({
action: 'tabsUpdated',
removed: url
});
});
});
}
function addToStorage(tab) {
chrome.storage.local.get('pinnedTabs', (data) => {
const tabs = data.pinnedTabs || [];
if (!tabs.some(t => t.url === tab.url)) {
tabs.push({
url: tab.url,
addedAt: new Date().toISOString(),
tags: [],
timeSpent: 0,
accessCount: 0,
lastAccessed: null
});
chrome.storage.local.set({ pinnedTabs: tabs }, () => {
// Notify options page to update if open
chrome.runtime.sendMessage({ action: 'tabsUpdated' });
});
}
});
}
// In background.js, update the restorePinnedTabs function:
function restorePinnedTabs() {
chrome.storage.local.get('pinnedTabs', (data) => {
const tabs = data.pinnedTabs || [];
// Create pinned tabs in order (left to right)
tabs.forEach((tab, index) => {
chrome.tabs.query({ url: tab.url }, (existingTabs) => {
if (existingTabs.length === 0) {
chrome.tabs.create({
url: tab.url,
pinned: true,
index: index // This is fine in create
});
} else if (!existingTabs.some(t => t.pinned)) {
chrome.tabs.update(existingTabs[0].id, {
pinned: true // Remove index from here
});
}
});
});
});
}
// Add to background.js
let sessionBackup = [];
// Save session periodically
setInterval(() => {
chrome.tabs.query({}, (tabs) => {
sessionBackup = tabs.map(tab => ({
url: tab.url,
pinned: tab.pinned,
title: tab.title,
timestamp: Date.now()
}));
chrome.storage.local.set({ 'sessionBackup': sessionBackup });
});
}, 60000); // Every minute
// Add recovery function
function recoverLastSession() {
chrome.storage.local.get('sessionBackup', (data) => {
if (data.sessionBackup) {
data.sessionBackup.forEach(tab => {
chrome.tabs.create({
url: tab.url,
pinned: tab.pinned
});
});
}
});
}
let activeTabStartTime = null;
let activeTabId = null;
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const tab = await chrome.tabs.get(activeInfo.tabId);
if (activeTabStartTime && activeTabId) {
updateTabTime(activeTabId);
}
if (tab.pinned) {
activeTabStartTime = Date.now();
activeTabId = tab.id;
updateTabAccess(tab.url);
}
});
chrome.windows.onFocusChanged.addListener((windowId) => {
if (windowId === chrome.windows.WINDOW_ID_NONE) {
if (activeTabStartTime && activeTabId) {
updateTabTime(activeTabId);
activeTabStartTime = null;
activeTabId = null;
}
}
});
async function updateTabTime(tabId) {
try {
const tab = await chrome.tabs.get(tabId);
const timeSpent = Date.now() - activeTabStartTime;
// Ignore unreasonable time differences (e.g., more than 12 hours)
if (timeSpent > 43200000) { // 12 hours in milliseconds
activeTabStartTime = null;
activeTabId = null;
return;
}
chrome.storage.local.get('pinnedTabs', (data) => {
const tabs = data.pinnedTabs || [];
const tabIndex = tabs.findIndex(t => t.url === tab.url);
if (tabIndex !== -1) {
// Reset counter if it gets too large
const currentTime = tabs[tabIndex].timeSpent || 0;
if (currentTime > 2147483647) { // Max reasonable value
tabs[tabIndex].timeSpent = timeSpent;
} else {
tabs[tabIndex].timeSpent = currentTime + timeSpent;
}
tabs[tabIndex].lastAccessed = new Date().toISOString();
chrome.storage.local.set({ pinnedTabs: tabs });
}
});
} catch (error) {
activeTabStartTime = null;
activeTabId = null;
console.log('Tab no longer exists:', error);
}
}
function updateTabAccess(url) {
chrome.storage.local.get('pinnedTabs', (data) => {
const tabs = data.pinnedTabs || [];
const tabIndex = tabs.findIndex(t => t.url === url);
if (tabIndex !== -1) {
tabs[tabIndex].accessCount = (tabs[tabIndex].accessCount || 0) + 1;
chrome.storage.local.set({ pinnedTabs: tabs });
}
});
}
// Listen for crashes
chrome.runtime.onStartup.addListener(recoverLastSession);