-
Notifications
You must be signed in to change notification settings - Fork 28
/
background.html
160 lines (140 loc) · 3.47 KB
/
background.html
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
<script>
var omnibox = chrome.omnibox;
var topMatch;
function escape(text) {
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
}
function escapeRegexp(text) {
var specialChars = [
'\\',
'[',
']',
'(',
')',
'|',
'.',
'+',
'?',
'{',
'}',
'-'
];
return text.split('').map(function(char) {
if (specialChars.indexOf(char) > -1)
return '\\' + char;
else
return char;
}).join('');
}
function parseMatches(text, search) {
var terms = escapeRegexp(search).split(/\s+/g);
var termMatchCounts = [];
terms.forEach(function() { termMatchCounts.push(0); });
var re = new RegExp("(" + terms.join(")|(") + ")", "ig");
var result = [];
var lastIndex = 0;
var match;
while (match = re.exec(text)) {
result.push({
match: false,
text: escape(text.substring(lastIndex, match.index))
});
lastIndex = match.index + match[0].length;
result.push({
match: true,
text: escape(text.substring(match.index, lastIndex))
});
for (var i = 1; i < match.length; i++) {
if (match[i])
termMatchCounts[i - 1]++;
}
}
// If any term found no matches, then we don't have a match.
if (termMatchCounts.indexOf(0) > -1) {
return [
{
match: false,
text: escape(text)
}
];
}
result.push({
match: false,
text: escape(text.substring(lastIndex))
});
return result;
}
function formatMatches(parsed) {
return parsed.reduce(function(s, piece) {
if (piece.match)
return s + "<match>" + piece.text + "</match>";
else
return s + piece.text;
}, "");
}
omnibox.onInputChanged.addListener(function(text, suggest) {
text = text.toLowerCase().replace(/\W+/g, ' ').trim();
if (!text)
return;
chrome.windows.getAll({populate: true}, function(windows) {
topMatch = -1;
var tabs = windows.reduce(function(arr, win) {
return arr.concat(win.tabs);
}, []);
var suggestions = tabs.map(function(tab) {
return {
tab: tab,
title: parseMatches(tab.title, text),
url: parseMatches(tab.url, text)
};
}).filter(function(item) {
if (item.title.length > 1 || item.url.length > 1) {
if (topMatch == -1)
topMatch = item.tab.id;
return true;
} else {
return false;
}
}).map(function(item) {
return {
content: item.tab.title + ' - ' + item.tab.url + '#' + item.tab.id,
description: formatMatches(item.title) + ' - ' +
'<url>' + formatMatches(item.url) + '</url>'
};
});
suggest(suggestions);
});
});
omnibox.onInputEntered.addListener(function(url) {
var tabId = url.match(/#(\d+)$/);
if (tabId)
tabId = parseInt(tabId[1]);
else if (topMatch != -1)
tabId = topMatch;
else
return;
chrome.tabs.getSelected(null, function(selected) {
// if the selected tab was the new tab page,
// assume that it was blank, and close it!
if (selected.url == 'chrome://newtab/')
chrome.tabs.remove(selected.id);
});
chrome.tabs.get(tabId, function(tab) {
if (tab && !tab.selected) {
chrome.tabs.update(tabId, {
selected: true
});
}
chrome.windows.get(tab.windowId, function(win) {
if (!window.focused) {
chrome.windows.update(tab.windowId, {
focused: true
});
}
});
});
});
omnibox.onInputCancelled.addListener(function() {
topMatch = -1;
});
</script>