-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
335 lines (289 loc) Β· 9.27 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
var FF_MAX_SUGGESTIONS = 5;
var FF_MAX_MATCHLENGTH = 1000;
var FF_DEBUGGING = false;
var FF_INCLUDE_HISTORY = true;
var FF_MOVE_TAB_TO_FIRST = false;
var FF_MOVE_TAB_TO_FIRST_TO_CURRENT_WINDOW = false;
var ffHistory = [];
var ffCurrentWindowId;
var ffTabsOnStart = [];
function ffEscapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
function ffActivateTag(tab) {
if (tab.url) {
chrome.tabs.create({ url: tab.url });
}
if (tab.tabId) {
chrome.tabs.update(tab.tabId, { active: true });
if (FF_MOVE_TAB_TO_FIRST) {
chrome.tabs.move(tab.tabId, { index: 0 });
}
}
if (tab.windowId) {
chrome.windows.update(tab.windowId, { focused: true });
}
}
function ffEscapeHtml(unsafe) {
return unsafe.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function ffGetHostname(url) {
var a = document.createElement('a');
a.href = url;
return a.hostname;
}
function ffHighlightText(text, words) {
words
.map(function (word) { return ffRegexpExactHl(word); })
.forEach(function (word) {
if (text.match(word)) {
text = text.replace(word, '\0$1\1');
}
});
words
.map(function (word) { return ffRegexpFuzzy(word); })
.forEach(function (word) {
if (text.match(word)) {
text = text.replace(word, function (m) {
return '\0' + m + '\1';
});
}
});
return ffEscapeHtml(text).replace(new RegExp('\0', 'g'), '<match>')
.replace(new RegExp('\1', 'g'), '</match>');
}
function ffCalculateScoreWords(tab, words) {
var score = 0;
var hostname = ffGetHostname(tab.url);
var found_words = words.map(function () { return false; });
words
.map(function (word) { return ffRegexpExact(word); })
.forEach(function (word, i) {
if (hostname.match(word)) {
score += 100;
found_words[i] = true;
}
if (tab.title.match(word)) {
score += 90;
found_words[i] = true;
}
if (tab.url.match(word)) {
score += 100;
found_words[i] = true;
}
});
words
.map(function (word) { return ffRegexpFuzzy(word); })
.forEach(function (word, i) {
if (found_words[i]) { return; }
if (hostname.match(word)) {
score += 20;
found_words[i] = true;
}
if (tab.title.match(word)) {
score += 20;
found_words[i] = true;
}
if (tab.url.match(word)) {
score += 30;
found_words[i] = true;
}
});
if (tab.visitCount) {
score += tab.visitCount * 10;
}
if (tab.lastVisitTime) {
score += tab.lastVisitTime / (new Date().getTime());
}
if (found_words.filter(function (x) { return x; }).length !== words.length) { score = 0; }
if (FF_DEBUGGING && score > 0) { console.debug('matching tab', tab.lastVisitTime && 'History' || 'Opened', 'id:' + tab.id, 'score:' + score, hostname, tab); }
return score;
}
function ffRegexpExact(word) {
return new RegExp(ffEscapeRegExp(word), 'i');
}
function ffRegexpExactHl(word) {
return new RegExp('(' + ffEscapeRegExp(word) + ')', 'ig');
}
function ffRegexpFuzzy(word) {
return new RegExp(word.split('').map(function (ch) { return ffEscapeRegExp(ch); }).join('.{0,10}?'), 'i');
}
function ffPrepareTab(tab, words) {
var content = tab.url + '#' + tab.windowId + '.' + tab.id;
var desc = ' π<url>' + ffHighlightText(tab.url, words) + '</url> π' + ffHighlightText(tab.title, words);
if (tab.score || FF_DEBUGGING) {
desc = '<url>[' + (tab.score * 0.01).toFixed(1) + ']</url>' + desc;
}
if (tab.status && tab.status !== 'complete') {
desc = '[' + tab.status + '] ' + desc;
}
if (tab.incognito) {
desc = '<url>[π]</url>' + desc;
}
if (tab.pinned) {
desc = '<url>[π]</url>' + desc;
}
if (tab.audible) {
desc = '<url>[π]</url>' + desc;
}
if (tab.lastVisitTime) {
content = tab.url + '#url';
desc = '<url>[π]</url>' + desc;
}
return { content: content, description: desc };
}
function ffFilter(tabs, words) {
return tabs.map(function (tab) {
tab.score = ffCalculateScoreWords(tab, words);
return tab;
})
.filter(function (tab) { return tab.score >= 10 && tab.title.length > 0; })
.sort(function (tab1, tab2) {
if (tab1.score < tab2.score) return 1;
if (tab1.score > tab2.score) return -1;
return 0;
})
;
}
function ffConcat(tabs1, tabs2) {
return tabs1.concat(ffTabsWithout(tabs2, tabs1));
}
function ffTabsWithout(whiteTabs, blackTabs) {
return whiteTabs.filter(function (whiteTab) {
return 0 === blackTabs.filter(function (blackTab) { return blackTab.url === whiteTab.url; }).length;
});
}
function ffReorderTabs(tabs) {
var windows = {};
/*
* move ones for all (doesn't work correctly):
*
* chrome.tabs.move(tabs.map(function(tab) { return tab.id; }), {index: 0});
*/
/*
* move once for all tabs in each window (doesn't work correctly):
*
* tabs.forEach(function(tab) {
* if(!windows[tab.windowId]) {
* windows[tab.windowId] = [];
* }
* windows[tab.windowId].push(tab);
* });
* for(var windowId in windows) {
* console.log("WINDOW", windowId, windows[windowId]);
* chrome.tabs.move(windows[windowId].map(function(tab) { return tab.id; }), {index: 0, windowId: parseInt(windowId)});
* }
*/
if (ffTabsOnStart.length === 0) {
ffTabsOnStart = tabs.map(function (tab) { return { id: tab.id, index: tab.index }; });
}
tabs.forEach(function (tab, i) {
if (FF_MOVE_TAB_TO_FIRST_TO_CURRENT_WINDOW && ffCurrentWindowId) {
// move one by one to current window
chrome.tabs.move(tab.id, { index: i, windowId: ffCurrentWindowId });
} else {
// move one by one
if (!windows[tab.windowId]) { windows[tab.windowId] = []; }
windows[tab.windowId].push(true);
chrome.tabs.move(tab.id, { index: windows[tab.windowId].length - 1 });
}
});
}
function ffParseSelected(text) {
var matchTab = text.match(/#(\d+)\.(\d+)$/);
if (matchTab) {
return { windowId: +matchTab[1], tabId: +matchTab[2] };
} else {
var matchUrl = text.match(/(.*)#url$/);
if (matchUrl) { return { url: matchUrl[1] }; }
}
return null;
}
function ffSearchFor(text) {
text = text.trim();
var words = text.split(/\s+/);
var words_exact_hl = text.split(/\s+/).map(function (word) {
return ffRegexpExactHl(word);
});
return new Promise(function (resolve, reject) {
chrome.tabs.query({}, function (array_of_tabs) {
var matching_tabs = ffFilter(array_of_tabs, words);
if (FF_MOVE_TAB_TO_FIRST) {
ffReorderTabs(matching_tabs.slice(0, 200));
}
matching_tabs = matching_tabs.slice(0, FF_MAX_SUGGESTIONS);
if (FF_INCLUDE_HISTORY && matching_tabs.length < FF_MAX_SUGGESTIONS) {
chrome.history.search({
text: '',
maxResults: 5000,
startTime: new Date().getTime() - 30 * 24 * 3600 * 1000
}, function (array_of_history_items) {
var matching_histories = ffFilter(array_of_history_items, words);
resolve(ffConcat(matching_tabs.slice(0, FF_MAX_SUGGESTIONS), matching_histories).map(function (tab) { return ffPrepareTab(tab, words); }));
});
return;
}
resolve(matching_tabs.map(function (tab) { return ffPrepareTab(tab, words); }));
});
});
}
chrome.windows.onFocusChanged.addListener(
function (windowId) {
ffCurrentWindowId = windowId;
}
);
chrome.omnibox.onInputChanged.addListener(
function (text, suggest) {
if (FF_DEBUGGING) { console.debug('input changed:', text); }
ffSearchFor(text).then(function (suggestions) { suggest(suggestions); });
}
);
chrome.omnibox.onInputCancelled.addListener(
// TODO this is called even when user presses UP/Down arrows.
function () {
// revert tab order (one by one)
ffTabsOnStart.sort(function (tab1, tab2) {
if (tab1.index < tab2.index) return 1;
if (tab1.index > tab2.index) return -1;
return 0;
}).forEach(function (tab, i) {
chrome.tabs.move(tab.id, { index: tab.index });
});
ffTabsOnStart = [];
}
);
chrome.omnibox.onInputEntered.addListener(
function (text) {
if (FF_DEBUGGING) {
console.debug('entered:', text);
console.debug('history:', ffHistory);
}
var selected = {};
if (text.length === 0) {
if (ffHistory.length >= 2) {
selected = ffHistory[ffHistory.length - 2];
} else {
return;
}
} else {
selected = ffParseSelected(text);
if (!selected) {
// User probably typed something but selected the first default option,
// i.e., "Run ff command: query"
ffSearchFor(text).then(function (suggestions) {
if (suggestions.length === 0) { return; }
var selected = ffParseSelected(suggestions[0].content);
ffHistory.push(selected);
ffActivateTag(selected);
});
return;
}
}
ffHistory.push(selected);
ffActivateTag(selected);
}
);