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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
<!DOCTYPE html>
<!--
* Copyright (c) 2010 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.
-->
<html>
<head>
</head>
<body>
<script>
/**
* Allows for binding callbacks to a specific scope.
* @param {Object} scope Scope to bind to.
* @returns {Function} A wrapped call to this function.
*/
Function.prototype.bind = function(scope) {
var func = this;
return function() {
return func.apply(scope, arguments);
};
};
//////////////////////////////////////////////////////////////////////////
/**
* Holds the search index and exposes operations to search the API docs.
* @constructor
*/
function APISearchCorpus() {
this.corpus_ = [];
};
/**
* Adds an entry to the index.
* @param {String} name Name of the function (e.g. chrome.tabs.get).
* @param {String} url Url to the documentation.
* @param {String} desc Description (optional).
* @param {String} type The type of entry (e.g. method, event).
*/
APISearchCorpus.prototype.addEntry = function(name, url, desc, type) {
this.corpus_.push({
'name' : name,
'url' : url,
'ranges' : [],
'description' : desc,
'type' : type
});
};
/**
* Locates a match from the supplied keywords against text.
*
* Keywords are matched in the order supplied, and a non-overlapping
* search is used. The ranges are returned in a way that is easily
* converted to the style array required by the omnibox API.
*
* @param {Array.<String>} keywords A list of keywords to check.
* @param {String} name The name to search against.
* @returns {Array.<Array.<number>>|null} A list of indexes corresponding
* to matches, or null if no match was found.
*/
APISearchCorpus.prototype.findMatch_ = function(keywords, name) {
var ranges = [];
var indexFrom = 0;
for (var i = 0; i < keywords.length; i++) {
var keyword = keywords[i].toLowerCase();
var start = name.indexOf(keyword, indexFrom);
if (start == -1) {
return null;
}
var end = start + keyword.length;
ranges.push([start, end]);
indexFrom = end + 1;
}
return ranges;
};
/**
* Searches this corpus for the supplied text.
* @param {String} text Query text.
* @param {Number} limit Max results to return.
* @returns {Array.<Object>} A list of entries corresponding with
* matches (@see APISearchCorpus.findMatch_ for keyword search
* algorithm. Results are returned in a sorted order, first by
* length, then alphabetically by name. An exact match will be
* returned first.
*/
APISearchCorpus.prototype.search = function(text, limit) {
var results = [];
var match = null;
if (!text || text.length == 0) {
return this.corpus_.slice(0, limit); // No text, start listing APIs.
}
var searchText = text.toLowerCase();
var keywords = searchText.split(' ');
for (var i = 0; i < this.corpus_.length; i++) {
var name = this.corpus_[i]['name'].toLowerCase();
if (results.length < limit) {
var result = this.findMatch_(keywords, name);
if (result) {
this.corpus_[i]['ranges'] = result;
results.push(this.corpus_[i]);
}
}
if (!match && searchText == name) {
match = this.corpus_[i]; // An exact match.
}
if (match && results.length >= limit) {
break; // Have an exact match and have reached the search limit.
}
}
if (match) {
results.unshift(match); // Add any exact match to the front.
}
return results;
};
/**
* Sorts the corpus according to name length, then name alphabetically.
*/
APISearchCorpus.prototype.sort = function() {
function compareLength(a, b) {
return a['name'].length - b['name'].length;
};
function compareAlpha(a, b) {
if (a['name'] < b['name']) return -1;
if (a['name'] > b['name']) return 1;
return 0;
};
function compare(a, b) {
var result = compareLength(a, b);
if (result == 0) result = compareAlpha(a, b);
return result;
};
this.corpus_.sort(compare);
};
//////////////////////////////////////////////////////////////////////////
/**
* Provides an interface to the Chrome Extensions documentation site.
* @param {APISearchCorpus} corpus The search corpus to populate.
* @constructor
*/
function DocsManager(corpus) {
this.CODE_URL_PREFIX = 'http://code.google.com/chrome/extensions/';
this.API_MANIFEST_URL = [
'http://src.chromium.org/viewvc/chrome/trunk/src/',
'chrome/common/extensions/api/extension_api.json'
].join('');
this.corpus_ = corpus;
};
/**
* Initiates a fetch of the docs and populates the corpus.
*/
DocsManager.prototype.fetch = function() {
this.fetchApi_(this.onApi_.bind(this));
};
/**
* Retrieves the API manifest from cache or fetches a new one if none.
* @param {Function} callback The function to pass the parsed manifest
* data to.
*/
DocsManager.prototype.fetchApi_ = function(callback) {
var currentCacheTime = this.getCacheTime_();
if (localStorage['cache-time'] && localStorage['cache']) {
var cacheTime = JSON.parse(localStorage['cache-time']);
if (cacheTime < currentCacheTime) {
callback(JSON.parse(localStorage['cache']));
return;
}
}
var xhr = new XMLHttpRequest();
xhr.addEventListener('readystatechange', function(evt) {
if (xhr.readyState == 4 && xhr.responseText) {
localStorage['cache-time'] = JSON.stringify(currentCacheTime);
localStorage['cache'] = xhr.responseText;
var json = JSON.parse(xhr.responseText);
callback(json);
}
});
xhr.open('GET', this.API_MANIFEST_URL, true);
xhr.send();
};
/**
* Returns a time which can be used to cache a manifest response.
* @returns {Number} A timestamp divided by the number of ms in a day,
* rounded to the nearest integer. This means the number should
* change only once per day, invalidating the cache that often.
*/
DocsManager.prototype.getCacheTime_ = function() {
var time = new Date().getTime();
time = Math.round(time / (1000 * 60 * 60 * 24));
return time;
};
/**
* Returns an URL for the documentation given an API element.
* @param {String} namespace The namespace (e.g. tabs, windows).
* @param {String} type The type of element (e.g. event, method, type).
* @param {String} name The name of the element (e.g. onRemoved).
* @returns {String} An URL corresponding with the documentation for the
* given element.
*/
DocsManager.prototype.getDocLink_ = function(namespace, type, name) {
var linkparts = [ this.CODE_URL_PREFIX, namespace, '.html' ];
if (type && name) {
linkparts.push('#', type, '-', name);
}
return linkparts.join('');
};
/**
* Returns a qualified name for an API element.
* @param {String} namespace The namespace (e.g. tabs, windows).
* @param {String} name The name of the element (e.g. onRemoved).
* @returns {String} A qualified API name (e.g. chrome.tabs.onRemoved).
*/
DocsManager.prototype.getName_ = function(namespace, name) {
var nameparts = [ 'chrome', namespace ];
if (name) {
nameparts.push(name);
}
return nameparts.join('.');
};
/**
* Parses an API manifest data structure and populates the search index.
* @param {Object} api The api manifest, as a JSON-parsed object.
*/
DocsManager.prototype.onApi_ = function(api) {
for (var i = 0; i < api.length; i++) {
var module = api[i];
if (module.nodoc) {
continue;
}
var ns = module.namespace;
var nsName = this.getName_(ns);
var nsUrl = this.getDocLink_(ns);
this.corpus_.addEntry(nsName, nsUrl, null, 'namespace');
this.parseAPIArray_('method', ns, module.functions);
this.parseAPIArray_('event', ns, module.events);
this.parseAPIArray_('type', ns, module.types);
this.parseAPIObject_('property', ns, module.properties);
this.corpus_.sort();
}
};
/**
* Parses an API manifest subsection which is formatted as an Array.
* @param {String} type The type of data (e.g. method, event, type).
* @param {String} ns The namespace (e.g. tabs, windows).
* @param {Array} list The list of API elements.
*/
DocsManager.prototype.parseAPIArray_ = function(type, ns, list) {
if (!list) return;
for (var j = 0; j < list.length; j++) {
var item = list[j];
if (item.nodoc) continue;
var name = item.name || item.id;
var fullname = this.getName_(ns, name);
var url = this.getDocLink_(ns, type, name);
var description = item.description;
this.corpus_.addEntry(fullname, url, description, type);
}
};
/**
* Parses an API manifest subsection which is formatted as an Object.
* @param {String} type The type of data (e.g. property).
* @param {String} ns The namespace (e.g. tabs, windows).
* @param {Object} list The object containing API elements.
*/
DocsManager.prototype.parseAPIObject_ = function(type, ns, list) {
for (var prop in list) {
if (list.hasOwnProperty(prop)) {
var name = this.getName_(ns, prop);
var url = this.getDocLink_(ns, type, prop);
var description = list[prop].description;
this.corpus_.addEntry(name, url, description, type);
}
}
};
//////////////////////////////////////////////////////////////////////////
/**
* Manages text input into the omnibox and returns search results.
* @param {APISearchCorpus} Populated search corpus.
* @param {TabManager} Manager to use to open tabs.
* @constructor
*/
function OmniboxManager(corpus, tabManager) {
this.SEPARATOR = ' - ';
this.corpus_ = corpus;
this.tabManager_ = tabManager;
chrome.omnibox.onInputChanged.addListener(
this.onChanged_.bind(this));
chrome.omnibox.onInputEntered.addListener(
this.onEntered_.bind(this));
};
/**
* Converts a corpus match to an object suitable for the omnibox API.
* @param {Object} match The match to convert.
* @returns {Object} A suggestion object formatted for the omnibox API.
*/
OmniboxManager.prototype.matchToSuggestion_ = function(match) {
var styles = [ ];
var ranges = match['ranges'];
var desc = match['name'];
var name = match['name'];
var lastIndex = 0;
for (var i = 0; i < ranges.length; i++) {
styles.push(chrome.omnibox.styleMatch(ranges[i][0]));
styles.push(chrome.omnibox.styleNone(ranges[i][1]));
lastIndex = ranges[i][1];
}
if (match['type']) {
// Abusing the URL style a little, but want this to stand out.
desc = this.pushStyle_(styles, 'Url', desc, match['type']);
lastIndex = desc.length;
}
if (match['description']) {
desc = this.pushStyle_(styles, 'Dim', desc, match['description']);
lastIndex = desc.length;
}
if (lastIndex == desc.length) styles.pop();
return {
'content' : name,
'description' : desc,
'descriptionStyles' : styles
};
};
/**
* Suggests a list of possible matches when omnibox text changes.
* @param {String} text Text input from the omnibox.
* @param {Function} suggest Callback to execute with a list of
* suggestion objects, if any matches were found.
*/
OmniboxManager.prototype.onChanged_ = function(text, suggest) {
var matches = this.corpus_.search(text, 10);
var suggestions = [];
for (var i = 0; i < matches.length; i++) {
var suggestion = this.matchToSuggestion_(matches[i]);
suggestions.push(suggestion);
}
suggest(suggestions);
};
/**
* Opens the most appropriate URL when enter is pressed in the omnibox.
*
* Note that the entered text does not have to be exact - the first
* search result is automatically opened when enter is pressed.
*
* @param {String} text The text entered.
*/
OmniboxManager.prototype.onEntered_ = function(text) {
var matches = this.corpus_.search(text, 1);
if (matches.length > 0) {
this.tabManager_.open(matches[0]['url']);
}
};
/**
* Helper function for constructing a suggestion style list.
*
* Adds a separator and text to a description, and modifies an array
* of styles so that the separator is not styled and the additional text
* obtains the requested style type.
*
* This method expects the list of styles to end with a styleNone style.
* It will modify the list so that the last element is a styleNone style.
* The last element will always be at the end of the returned string,
* which will throw an error unless it is removed before being passed
* to the API (this method is intended to be called a few times in a row).
* @see OmniboxManager.matchToSuggestion_ for code that removes this last
* entry.
*
* @param {Array.<Object>} styles An array of styles, in the format
* expected by the omnibox API.
* @param {String} type The style type to apply - must be one of
* "Dim", "Match", "None", and "Url".
* @param {String} desc The description text to append to and style.
* @param {String} text The text to append to the description.
* @returns {String} The description plus a separator and the supplied
* text, intended to overwrite the variable which was passed into
* this function as "desc".
*/
OmniboxManager.prototype.pushStyle_ = function(styles, type, desc, text) {
desc += this.SEPARATOR;
styles.push(chrome.omnibox['style' + type](desc.length));
desc += text;
styles.push(chrome.omnibox.styleNone(desc.length));
return desc;
};
//////////////////////////////////////////////////////////////////////////
/**
* Manages opening urls in tabs.
* @constructor
*/
function TabManager() {
this.tab_ = null;
chrome.tabs.onRemoved.addListener(this.onRemoved_.bind(this));
};
/**
* When a tab is removed, see if it was opened by us and null out if yes.
* @param {Number} tabid ID of the removed tab.
*/
TabManager.prototype.onRemoved_ = function(tabid) {
if (this.tab_ && tabid == this.tab_.id) this.tab_ = null;
};
/**
* When a tab opened by us is created, store it for future updates.
* @param {Tab} tab The tab which was just opened.
*/
TabManager.prototype.onTab_ = function(tab) {
this.tab_ = tab;
};
/**
* Opens the supplied URL.
*
* The first time this method is called a new tab is created. Subsequent
* times this is called, the opened tab will be updated. If that tab
* is ever closed, then a new tab will be created for the next call.
*
* @param {String} url The URL to open.
*/
TabManager.prototype.open = function(url) {
if (url) {
var args = { 'url' : url, 'selected' : true };
if (this.tab_) {
chrome.tabs.update(this.tab_.id, args);
} else {
chrome.tabs.create(args, this.onTab_.bind(this));
}
}
};
//////////////////////////////////////////////////////////////////////////
var corpus = new APISearchCorpus();
var docsManager = new DocsManager(corpus);
docsManager.fetch();
var tabManager = new TabManager();
var omnibox = new OmniboxManager(corpus, tabManager);
</script>
</body>
</html>
|