/
background.js
196 lines (167 loc) · 7.06 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
(function() {
// Fuzzy search function from list.js fuzzysearch plugin
// source: https://github.com/javve/list.fuzzysearch.js/blob/master/src/fuzzy.js
String.prototype.fuzzy = function(pattern, options) {
var text = this.toLowerCase();
var pattern = pattern.toLowerCase();
// Aproximately where in the text is the pattern expected to be found?
var Match_Location = options.location || 0;
// Determines how close the match must be to the fuzzy location
// (specified above). An exact letter match which is 'distance' characters
// away from the fuzzy location would score as a complete mismatch. A
// distance of '0' requires the match be at the exact location specified,
// a threshold of '1000' would require a perfect match to be within 800
// characters of the fuzzy location to be found using a 0.8 threshold.
var Match_Distance = options.distance || 100;
// At what point does the match algorithm give up. A threshold of '0.0'
// requires a perfect match (of both letters and location), a threshold
// of '1.0' would match anything.
var Match_Threshold = options.threshold || 0.4;
if (pattern === text) return true; // Exact match
if (pattern.length > 32) return false; // This algorithm cannot be used
// Set starting location at beginning text and initialise the alphabet.
var loc = Match_Location,
s = (function() {
var q = {},
i;
for (i = 0; i < pattern.length; i++) {
q[pattern.charAt(i)] = 0;
}
for (i = 0; i < pattern.length; i++) {
q[pattern.charAt(i)] |= 1 << (pattern.length - i - 1);
}
return q;
}());
// Compute and return the score for a match with e errors and x location.
// Accesses loc and pattern through being a closure.
function match_bitapScore_(e, x) {
var accuracy = e / pattern.length,
proximity = Math.abs(loc - x);
if (!Match_Distance) {
// Dodge divide by zero error.
return proximity ? 1.0 : accuracy;
}
return accuracy + (proximity / Match_Distance);
}
// Highest score beyond which we give up.
var score_threshold = Match_Threshold;
// Is there a nearby exact match? (speedup)
var best_loc = text.indexOf(pattern, loc);
if (best_loc != -1) {
score_threshold = Math.min(match_bitapScore_(0, best_loc), score_threshold);
// What about in the other direction? (speedup)
best_loc = text.lastIndexOf(pattern, loc + pattern.length);
if (best_loc != -1) {
score_threshold = Math.min(match_bitapScore_(0, best_loc), score_threshold);
}
}
// Initialise the bit arrays.
var matchmask = 1 << (pattern.length - 1);
best_loc = -1;
var bin_min, bin_mid;
var bin_max = pattern.length + text.length;
var last_rd;
for (var d = 0; d < pattern.length; d++) {
// Scan for the best match; each iteration allows for one more error.
// Run a binary search to determine how far from 'loc' we can stray at this
// error level.
bin_min = 0;
bin_mid = bin_max;
while (bin_min < bin_mid) {
if (match_bitapScore_(d, loc + bin_mid) <= score_threshold) {
bin_min = bin_mid;
} else {
bin_max = bin_mid;
}
bin_mid = Math.floor((bin_max - bin_min) / 2 + bin_min);
}
// Use the result from this iteration as the maximum for the next.
bin_max = bin_mid;
var start = Math.max(1, loc - bin_mid + 1);
var finish = Math.min(loc + bin_mid, text.length) + pattern.length;
var rd = Array(finish + 2);
rd[finish + 1] = (1 << d) - 1;
for (var j = finish; j >= start; j--) {
// The alphabet (s) is a sparse hash, so the following line generates
// warnings.
var charMatch = s[text.charAt(j - 1)];
if (d === 0) { // First pass: exact match.
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
} else { // Subsequent passes: fuzzy match.
rd[j] = (((rd[j + 1] << 1) | 1) & charMatch) |
(((last_rd[j + 1] | last_rd[j]) << 1) | 1) |
last_rd[j + 1];
}
if (rd[j] & matchmask) {
var score = match_bitapScore_(d, j - 1);
// This match will almost certainly be better than any existing match.
// But check anyway.
if (score <= score_threshold) {
// Told you so.
score_threshold = score;
best_loc = j - 1;
if (best_loc > loc) {
// When passing loc, don't exceed our current distance from loc.
start = Math.max(1, 2 * loc - best_loc);
} else {
// Already passed loc, downhill from here on in.
break;
}
}
}
}
// No hope for a (better) match at greater error levels.
if (match_bitapScore_(d + 1, loc) > score_threshold) {
break;
}
last_rd = rd;
}
return (best_loc < 0) ? false : true;
};
///////////////////////////////////////////////////////////////////////
// Set of functions to convert some symbols to html entities
// source: http://stackoverflow.com/a/5499821/199021
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
function safe(str) {
return str.replace(/[&<>]/g, replaceTag);
}
///////////////////////////////////////////////////////////////////////
// This event is fired each time the user updates the text in the omnibox,
// as long as the extension's keyword mode is still active.
chrome.omnibox.onInputChanged.addListener(
function(text, suggest) {
// console.log('inputChanged: ' + text);
chrome.tabs.query({windowType: "normal"}, function(tabs) {
// reduce tab list to only have tabs we're interested in
var suggestions = tabs.reduce(function(result, tab) {
// remove anything up to and including the schema from the url
var url = tab.url.replace(/^.*https?:\/\//, '');
// if text fuzzy-matches title or url, add to result array
if(tab.title.fuzzy(text, {}) || url.fuzzy(text, {})) {
return result.concat({content: ""+tab.id, description: safe(tab.title)});
} else {
return result;
}
}, []);
// console.log(suggestions);
suggest(suggestions);
});
});
// This event is fired with the user accepts the input in the omnibox.
chrome.omnibox.onInputEntered.addListener(
function(text) {
// console.log('inputEntered: ' + text);
// make selected tab active
chrome.tabs.update(parseInt(text, 10), {active: true}, function(tab) {
// focus window with selected tab
chrome.windows.update(tab.windowId, {focused: true});
});
});
})();