-
Notifications
You must be signed in to change notification settings - Fork 0
/
background_old.js
110 lines (98 loc) · 2.87 KB
/
background_old.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
/**
* @author https://github.com/LEODPEN
* date: 2021/05/
* @description This is the old version which only provided "backward" feature.
*/
class fixedSizeStack {
constructor(sz) {
this.len = sz;
this.data = [];
this.curSize = 0;
}
empty() {
return this.curSize <= 0;
}
full() {
return this.curSize >= this.len;
}
clear() {
delete this.data;
this.data = [];
this.curSize = 0;
}
push(e) {
if (this.full()) {
this.data.shift();
this.curSize--;
}
var newCurLen = this.data.push(e);
this.curSize = newCurLen >= this.len ? this.len : newCurLen;
}
pop() {
if (this.empty()) {
return;
}
var res = this.data.pop(); // 最后一个
this.curSize = this.curSize <= 0 ? 0 : --this.curSize;
return res;
}
}
function doBackWard(stack, curWin) {
if (stack.empty()) return;
chrome.tabs.getCurrent(function(curTab) {
// check again
var back;
while((back = stack.pop()).tabId == curTab
&& back.windowId == curWIn);
if (back.windowId != curWin) return;
chrome.tabs.update(back.tabId, {active: true});
});
}
try{
var latestTabMap = new Map();
let url = chrome.runtime.getURL("srch-res.html");
var stackMap = new Map();
chrome.commands.onCommand.addListener(function (command) {
if (command === 'do-search-in-new-tab') {
chrome.tabs.create({ url });
}
if (command === 'backward') {
chrome.windows.getCurrent(function(cw) {
var stack;
var curWin = cw.id;
if (stackMap.has(curWin)) {
stack = stackMap.get(curWin);
} else {
stack = new fixedSizeStack(10);
stackMap.set(curWin, stack);
}
if (!stack.empty() && latestTabMap.get(curWin)) {
latestTabMap.set(curWin, null);
}
doBackWard(stack, curWin);
});
if (command === 'forward') {
}
}
});
chrome.tabs.onActivated.addListener(function(activeInfo) {
chrome.windows.getCurrent(function(cw){
var curWin = cw.id;
var stack;
var latestTab;
if (stackMap.has(curWin)) {
stack = stackMap.get(curWin);
} else {
stack = new fixedSizeStack(10);
stackMap.set(curWin, stack);
}
if ((latestTab = latestTabMap.get(curWin))) {
stack.push(latestTab);
}
latestTabMap.set(curWin, activeInfo);
});
});
} catch(e) {
// If tab was closed then just jump it.
console.error(e);
}