-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
316 lines (292 loc) · 8.5 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* @author https://github.com/LEODPEN
* date: 2021/05/26
* @description new version using linked list.
*/
class PNode {
windowId;tabId;next;pre;
constructor(windowId, tabId) {
this.windowId = windowId;
this.tabId = tabId;
this.next = null;
this.pre = null;
}
}
class LinkedList {
head;tail;windowId;cur;
constructor(windowId) {
this.windowId = windowId;
this.head = new PNode(-1, -1);
this.tail = new PNode(-1, -1);
this.head.next = this.tail;
this.tail.pre = this.head;
this.cur = this.head;
}
empty() {
return this.head.next == this.tail;
}
clear() {
this.head.next = this.tail;
this.tail.pre = this.head;
this.cur = this.head;
}
deleteOne(node) {
if (!node || node == this.head || node == this.tail) {
return;
}
var p = node.pre;
var n = node.next;
p.next = n;
n.pre = p;
node.pre = null;
node.next = null;
node = null;
}
/**
* @param threshold > 10
*/
deleteSomeWhenSizeLargerThan(threshold) {
var sz = 0, randomNum = Math.round(Math.random()*(threshold/2));
randomNum = randomNum >= 2 ? randomNum : 2;
var node = this.head;
var randomNode = null;
while (node.next != this.tail) {
node = node.next;
sz++;
if (sz == randomNum) {
randomNode = node;
}
}
if (sz > threshold) {
var nodeAfterHead = this.head.next;
nodeAfterHead.pre = null;
this.head.next = randomNode;
randomNode.pre = this.head;
}
}
/**
* @param e 待插入element(new)
*/
add(e) {
if (!e) {
return;
}
if (!this.empty()) {
var nodeBeforeTail = this.tail.pre;
var nodeAfterCur = this.cur.next;
nodeBeforeTail.next = null;
nodeAfterCur.pre = null;
}
this.cur.next = e;
e.pre = this.cur;
e.next = this.tail;
this.tail.pre = e;
this.cur = this.cur.next; // 即为e
// 保证检测概率更大
if (Math.random() <= 0.5) {
this.deleteSomeWhenSizeLargerThan(20);
}
}
/**
* 如果返回为null,则说明cur未变,不跳转
* @returns
*/
loadBackard(ifDelete) {
if (this.empty()) return null;
var r = this.cur;
var l = this.cur.pre;
// while (l != this.head &&
// (! (judgeExistence(l.windowId, l.tabId))
// || (l.windowId == l.pre.windowId
// && l.tabId == l.pre.tabId))) {
// l = l.pre;
// }
while (l != this.head && l.tabId == l.pre.tabId) {
l.pre = l.pre.pre;
l.pre.next = l;
l = l.pre;
}
if (l != r.pre) {
l.next = r;
r.pre = l;
}
if (ifDelete == true) {
this.deleteOne(r);
}
if (l == this.head) {
if (r.next == null || r.pre == null) {
this.cur = l;
}
return null;
} else {
this.cur = l;
return this.cur;
}
}
/**
* 如果返回为null,则说明cur未变,不跳转
* @returns
*/
loadForward(ifDelete) {
if (this.empty()) return null;
var l = this.cur;
var r = this.cur.next;
// while(r != this.tail &&
// (! (judgeExistence(r.windowId, r.tabId))
// || (r.windowId == r.next.windowId
// && r.tabId == r.next.tabId))) {
// r = r.next;
// }
while (r != this.tail && r.tabId == r.next.tabId) {
r.next = r.next.next;
r.next.pre = r;
l = l.pre;
}
if (r != l.next) {
l.next = r;
r.pre = l;
}
if(ifDelete == true) {
this.deleteOne(l);
}
if (r == this.tail) {
if (l.next == null || l.pre == null) {
this.cur = r.pre;
}
return null;
} else {
this.cur = r;
return this.cur;
}
}
}
async function judgeExistence(windowId, tabId) {
var res = false;
await chrome.tabs.get(tabId)
.then((result) => {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
res = false;
} else {
// console.log("all things ok");
res = true;
}})
.catch((error) => {
console.log(error);
res = false;
});
return res;
}
function doBackward(lk, ifDelete) {
var back;
if (!lk || !(back = lk.loadBackard(ifDelete)) || back.windowId != lk.windowId) {
return;
}
chrome.tabs.update(back.tabId, {active: true}, function(res) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
doBackward(lk, true);
}
});
}
function doForward(lk, ifDelete) {
var forw;
if (!lk || !(forw = lk.loadForward(ifDelete)) || forw.windowId != lk.windowId) {
return;
}
chrome.tabs.update(forw.tabId, {active: true}, function(res) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
doForward(lk, true);
}
});
}
try{
var map = new Map();
chrome.storage.local.get("MAP", function(data) {
if (chrome.runtime.lastError || !data) {
map = new Map();
} else {
map = new Map();
var ols = new Map(Object.entries(data.MAP));
for (var [key, value] of ols) {
var h = value.head, t = value.tail, c = value.cur;
var lk = new LinkedList(parseInt(key));
var cur;
var pnode;
while(h.next != null) {
h = h.next;
if (h.windowId != -1 && h.tabId != -1) {
pnode = new PNode(h.windowId, h.tabId);
if (h == c || (h.windowId == c.windowId && h.tabId == c.tabId)) {
console.log(pnode);
cur = pnode;
}
lk.add(pnode);
}
}
if (cur) {
lk.cur = cur;
}
map.set(parseInt(key), lk);
}
}
});
chrome.commands.onCommand.addListener(function (command) {
if (command === 'backward') {
chrome.windows.getCurrent(function(cw) {
var curWin = cw.id;
var lk;
if (!map.has(curWin)) {
// 初始时刻
return;
} else {
lk = map.get(curWin);
}
doBackward(lk, false);
chrome.storage.local.set({"MAP": Object.fromEntries(map)});
});
}
if (command === 'forward') {
chrome.windows.getCurrent(function(cw) {
var curWin = cw.id;
var lk;
if (!map.has(curWin)) {
// 初始时刻
return;
} else {
lk = map.get(curWin);
}
doForward(lk, false);
chrome.storage.local.set({"MAP": Object.fromEntries(map)});
});
}
});
chrome.tabs.onActivated.addListener(function(activeInfo) {
var windowId = activeInfo.windowId;
var tabId = activeInfo.tabId;
var lk;
if (map.has(windowId)) {
lk = map.get(windowId);
if (!lk || lk.windowId != windowId) {
lk.clear();
lk = new LinkedList(windowId);
map.set(windowId, lk);
}
} else {
lk = new LinkedList(windowId);
map.set(windowId, lk);
}
if (lk.cur.windowId == windowId && lk.cur.tabId == tabId) {
return;
} else {
var curNode = new PNode(windowId, tabId);
lk.add(curNode);
chrome.storage.local.set({"MAP": Object.fromEntries(map)});
}
});
} catch(e) {
map.clear();
chrome.storage.local.set({"MAP": null});
console.error(e);
}