forked from deweyapp/dewey
-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
60 lines (39 loc) · 1.52 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
var resultsList = [],
unlikely = "dewey:";
// Return the suggestions
chrome.omnibox.onInputChanged.addListener(function(text, suggest) {
chrome.bookmarks.search(text, function(searchResults) {
resultsList = [];
resultsList.push({
content: text,
description: 'Search Dewey for: ' + text
});
for (var i = 0; i < searchResults.length; i++) {
var item = searchResults[i];
if(item.url === void 0) continue;
/* jshint scripturl:true */
if(item.url.substring(0, 11) === 'javascript:') continue;
resultsList.push({
content: unlikely + item.url,
description: item.title.replace(new RegExp("(" + text + ")", "gi"), "<match>$1</match>")
});
}
var defaultSuggestion = '';
if(resultsList.length > 0){
defaultSuggestion = resultsList[0].description;
suggest(resultsList.slice(1, -1));
}
chrome.omnibox.setDefaultSuggestion({ description: defaultSuggestion });
});
});
chrome.omnibox.onInputEntered.addListener( function(text) {
// If text doesn't have unlikely prepended its the default
if(text.substring(0, unlikely.length) !== unlikely) {
text = resultsList[0].content;
chrome.tabs.update({ url: chrome.extension.getURL('app.html') + '#main?search=' + text });
}
else{
text = text.substring(unlikely.length);
chrome.tabs.update({ url: text });
}
});