forked from philc/vimium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_mode_history.js
64 lines (56 loc) · 2.12 KB
/
find_mode_history.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
// This // implements find-mode query history as a list of raw queries, most recent first.
// This is under lib/ since it is used by both content scripts and iframes from pages/.
const FindModeHistory = {
storage: chrome.storage.session,
key: "findModeRawQueryList",
max: 50,
rawQueryList: null,
async init() {
this.isIncognitoMode = chrome.extension.inIncognitoContext;
if (!this.rawQueryList) {
if (this.isIncognitoMode) this.key = "findModeRawQueryListIncognito";
let result = await this.storage.get(this.key);
if (this.isIncognitoMode) {
// This is the first incognito tab, so we need to initialize the incognito-mode query
// history.
result = await this.storage.get("findModeRawQueryList");
this.rawQueryList = result.findModeRawQueryList || [];
this.storage.set({ findModeRawQueryListIncognito: this.rawQueryList });
} else {
this.rawQueryList = result[this.key] || [];
}
}
chrome.storage.onChanged.addListener((changes, _area) => {
if (changes[this.key]) {
this.rawQueryList = changes[this.key].newValue;
}
});
},
getQuery(index) {
if (index == null) index = 0;
return this.rawQueryList[index] || "";
},
async saveQuery(query) {
if (query.length == 0) return;
this.rawQueryList = this.refreshRawQueryList(query, this.rawQueryList);
const newSetting = {};
newSetting[this.key] = this.rawQueryList;
await this.storage.set(newSetting);
// If there are any active incognito-mode tabs, then propagate this query to those tabs too.
if (!this.isIncognitoMode) {
const result = await this.storage.get("findModeRawQueryListIncognito");
if (result.findModeRawQueryListIncognito) {
await this.storage.set({
findModeRawQueryListIncognito: this.refreshRawQueryList(
query,
result.findModeRawQueryListIncognito,
),
});
}
}
},
refreshRawQueryList(query, rawQueryList) {
return ([query].concat(rawQueryList.filter((q) => q !== query))).slice(0, this.max + 1);
},
};
window.FindModeHistory = FindModeHistory;