-
Notifications
You must be signed in to change notification settings - Fork 0
/
back.js
303 lines (255 loc) · 7.53 KB
/
back.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
class History {
constructor() {
this.list = []
this.map = {}
}
rebuild(tabs, options) {
this.list = []
this.map = {}
for (const tab of tabs) {
this.list.push(tab)
}
this.options = options
this.compareFunc = getCompareFunc(options)
this.list.sort(this.compareFunc)
this.map = {}
for (const i in this.list) {
this.map[this.list[i].id] = Number(i)
}
this.length = this.list.length
}
push(tab) {
let flag = false
for (let i = 0; i < this.list.length; i++) {
let c_tab = this.list[i]
if (flag) {
this.map[c_tab.id]++
c_tab.idx++
continue
}
if (!flag && this.compareFunc(tab, c_tab) > 0) {
continue
}
this.list.splice(i, 0, tab)
this.map[tab.id] = i
tab.idx = i
flag = true
}
if (!flag) {
this.map[tab.id] = this.list.push(tab) - 1
tab.idx = this.map[tab.id]
}
this.length++
}
pop() {
if (!this.length) return
let tab = this.list.shift()
delete this.map[tab.id]
this.length--
for (const c_tab of this.list) {
this.map[c_tab.id]--
}
return tab
}
remove(tab_id, err_exit = true) {
if (!this.get(tab_id, err_exit)) {
return this.length
}
let idx = this.map[tab_id]
let tab = this.list.splice(idx, 1)
delete this.map[tab_id]
this.length--
for (let i = idx; i < this.length; i++) {
let tab = this.list[i]
tab.idx--
this.map[tab.id]--
}
return tab
}
get(tab_id, err_exit = true) {
if (!(tab_id in this.map)) {
if (err_exit) {
console.log("未找到指定的 tab ", tab_id)
throw new Error("未找到指定的 tab " + tab_id)
}
return undefined
}
return this.list[this.map[tab_id]]
}
update(tab) {
this.get(tab.id)
let idx = this.map[tab.id]
this.list[idx] = tab
if (idx === this.length - 1) return
let flag = false
let skip = 0
for (let i = idx + 1; i < this.length; i++) {
let c_tab = this.list[i]
if (this.compareFunc(tab, c_tab) < 0) {
flag = true
break
} else {
skip++
}
}
// 不需要交换
if (!skip) return
for (let i = 0; i < skip; i++) {
let c_tab = this.list[idx + i + 1]
this.list[idx + i] = c_tab
this.map[c_tab.id]--
c_tab.idx--
}
this.list[idx + skip] = tab
this.map[tab.id] = idx + skip
tab.idx = idx + skip
}
info(...filed) {
let info = [
this.length,
'dat',
this.list,
]
info.push("field", ...(filed.map(k => {
return this.list.map(x => x[k])
})))
return info
}
title() {
return this.list.map(x => x.title)
}
}
class TabsManager {
constructor() {
this.options = {}
this.tab = new History()
}
set_options(options = defaultOptions) {
this.options = options
}
set_tabs(tabs = []) {
tabs = tabs.map((x) => this.parse_tab(x, new Date()))
this.tab.rebuild(tabs, this.options)
}
parse_tab(tab, date = new Date()) {
return {
id: tab.id,
title: tab.title,
window: tab.windowId,
time: date,
url: tab.url ?? tab.pendingUrl,
sort_idx: -1,
index: tab.index,
}
}
remove(tabId) {
return this.tab.remove(tabId, false)
}
add(tab) {
tab = this.parse_tab(tab)
this.tab.push(tab)
let remove_count = this.tab.length - this.options.max_count
let r_tabs = []
while (remove_count > 0 && this.tab.length > 0) {
let c_tab = this.tab.pop()
chrome.tabs.remove(c_tab.id)
r_tabs.push(c_tab)
remove_count--
}
return tab
}
update(tabId, {status = "", url = "", title = ""} = {}) {
if (status === "" && title) {
status = "update_title"
}
if (status === "" || status === "complete") {
return
}
if (status === "loading" && !url) {
return
}
let tab = this.tab.get(tabId, false)
if (!tab) {
console.warn("update tab not found", tabId, arguments, this.tab)
return
}
tab.time = Number(new Date())
title && (tab.title = title)
url && (tab.url = url)
this.tab.update(tab)
return [status, tab]
}
}
let compareFunctions = {
ef: (tab1, tab2) => {
let t1 = tab1.url.includes("://newtab/") ? tab1.time - 4503599627370496 : tab1.time
let t2 = tab2.url.includes("://newtab/") ? tab2.time - 4503599627370496 : tab2.time
return Number(t1 - t2)
},
// 最少使用
rnu: (tab1, tab2) => {
if (tab1.time === tab2.time) {
return tab1.id - tab2.id
}
return Number(tab1.time - tab2.time)
},
// 左侧优先
lf: (tab1, tab2) => {
return tab1.index - tab2.index
},
}
function getCompareFunc(options) {
// let fn1 = options.empty_first ? compareFunctions.ef : () => {
// }
let fn2 = options.mode in compareFunctions ? compareFunctions[options.mode] : () => {
}
return function (tab1, tab2) {
// let v = fn1(tab1, tab2)
// if (v) return v
return fn2(tab1, tab2)
}
}
var mgr = new TabsManager()
function refresh_options(fn = undefined) {
chrome.storage.sync.get({
mode: 'rnu', //
max_count: 10,
debug: false,
}, function (options) {
console.log("options", options)
mgr.set_options(options)
chrome.tabs.query({currentWindow: true}, function (tabs) {
mgr.set_tabs(tabs)
if (fn) fn()
})
})
}
function add(tab) {
tab = mgr.add(tab)
mgr.options.debug && console.log("add", tab, mgr.tab.title('id', 'title', 'time', "url"))
}
function remove(tab_id) {
let tab = mgr.remove(tab_id)
mgr.options.debug && tab instanceof Object && console.log("remove", tab, mgr.tab.title('id', 'title', 'time', "url"))
}
function update() {
mgr.update(...arguments)
mgr.options.debug && console.log("update", arguments, mgr.tab.title('id', 'title', 'time', "url"))
}
function active({tabId, windowId}) {
mgr.update(tabId, {status: "active"})
mgr.options.debug && console.log("active", tabId, mgr.tab.title('id', 'title', 'time', "url"))
}
(function () {
refresh_options(() => {
chrome.tabs.onCreated.addListener(add)
chrome.tabs.onRemoved.addListener(remove)
chrome.tabs.onUpdated.addListener(update)
chrome.tabs.onActivated.addListener(active)
})
})()
chrome.runtime.onMessage.addListener(({type}) => {
if (type === "reload") {
refresh_options()
}
});