forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
171 lines (144 loc) · 5.11 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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var currentRequest = null;
chrome.omnibox.onInputChanged.addListener(
function(text, suggest) {
if (currentRequest != null) {
currentRequest.onreadystatechange = null;
currentRequest.abort();
currentRequest = null;
}
updateDefaultSuggestion(text);
if (text == '' || text == 'halp')
return;
currentRequest = search(text, function(xml) {
var results = [];
var entries = xml.getElementsByTagName("entry");
for (var i = 0, entry; i < 5 && (entry = entries[i]); i++) {
var path = entry.getElementsByTagName("file")[0].getAttribute("name");
var line =
entry.getElementsByTagName("match")[0].getAttribute("lineNumber");
var file = path.split("/").pop();
var description = '<url>' + file + '</url>';
if (/^file:/.test(text)) {
description += ' <dim>' + path + '</dim>';
} else {
var content = entry.getElementsByTagName("content")[0].textContent;
// There can be multiple lines. Kill all the ones except the one that
// contains the first match. We can ocassionally fail to find a single
// line that matches, so we still handle multiple lines below.
var matches = content.split(/\n/);
for (var j = 0, match; match = matches[j]; j++) {
if (match.indexOf('<b>') > -1) {
content = match;
break;
}
}
// Replace any extraneous whitespace to make it look nicer.
content = content.replace(/[\n\t]/g, ' ');
content = content.replace(/ {2,}/g, ' ');
// Codesearch wraps the result in <pre> tags. Remove those if they're
// still there.
content = content.replace(/<\/?pre>/g, '');
// Codesearch highlights the matches with 'b' tags. Replaces those
// with 'match'.
content = content.replace(/<(\/)?b>/g, '<$1match>');
description += ' ' + content;
}
results.push({
content: path + '@' + line,
description: description
});
}
suggest(results);
});
}
);
function resetDefaultSuggestion() {
chrome.omnibox.setDefaultSuggestion({
description: '<url><match>src:</match></url> Search Chromium source'
});
}
resetDefaultSuggestion();
function updateDefaultSuggestion(text) {
var isRegex = /^re:/.test(text);
var isFile = /^file:/.test(text);
var isHalp = (text == 'halp');
var isPlaintext = text.length && !isRegex && !isFile && !isHalp;
var description = '<match><url>src</url></match><dim> [</dim>';
description +=
isPlaintext ? ('<match>' + text + '</match>') : 'plaintext-search';
description += '<dim> | </dim>';
description += isRegex ? ('<match>' + text + '</match>') : 're:regex-search';
description += '<dim> | </dim>';
description += isFile ? ('<match>' + text + '</match>') : 'file:filename';
description += '<dim> | </dim>';
description += isHalp ? '<match>halp</match>' : 'halp';
description += '<dim> ]</dim>';
chrome.omnibox.setDefaultSuggestion({
description: description
});
}
chrome.omnibox.onInputStarted.addListener(function() {
updateDefaultSuggestion('');
});
chrome.omnibox.onInputCancelled.addListener(function() {
resetDefaultSuggestion();
});
function search(query, callback) {
if (query == 'halp')
return;
if (/^re:/.test(query))
query = query.substring('re:'.length);
else if (/^file:/.test(query))
query = 'file:"' + query.substring('file:'.length) + '"';
else
query = '"' + query + '"';
var url = "https://code.google.com/p/chromium/codesearch#search/&type=cs&q=" + query +
"&exact_package=chromium&type=cs";
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.setRequestHeader("GData-Version", "2");
req.onreadystatechange = function() {
if (req.readyState == 4) {
callback(req.responseXML);
}
}
req.send(null);
return req;
}
function getUrl(path, line) {
var url = "https://code.google.com/p/chromium/codesearch#" + path
"&sq=package:chromium";
if (line)
url += "&l=" + line;
return url;
}
function getEntryUrl(entry) {
return getUrl(
entry.getElementsByTagName("file")[0].getAttribute("name"),
entry.getElementsByTagName("match")[0].getAttribute("lineNumber"));
return url;
}
function navigate(url) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.update(tabs[0].id, {url: url});
});
}
chrome.omnibox.onInputEntered.addListener(function(text) {
// TODO(aa): We need a way to pass arbitrary data through. Maybe that is just
// URL?
if (/@\d+\b/.test(text)) {
var chunks = text.split('@');
var path = chunks[0];
var line = chunks[1];
navigate(getUrl(path, line));
} else if (text == 'halp') {
// TODO(aa)
} else {
navigate("https://code.google.com/p/chromium/codesearch#search/&type=cs" +
"&q=" + text +
"&exact_package=chromium&type=cs");
}
});