-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmdn_search.js
110 lines (91 loc) · 3.4 KB
/
mdn_search.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
var API_KEY = "AIzaSyCr6xs0NlPg2hIynXQJWY3o230n6iQyDl0",
NO_OF_RESULTS = 5; // Chrome shows only upto 5 results
var currentQueryString,
latestDefault = null,
resultCache = {};
function clearDefault (queryText) {
latestDefault = null;
var suggestion = queryText ?
"Search MDN for: <match>" + queryText + "</match>":
"Start typing to search MDN";
chrome.omnibox.setDefaultSuggestion({description: suggestion});
}
function setDefaultSuggestion (result) {
latestDefault = result;
chrome.omnibox.setDefaultSuggestion({description: result.description});
}
chrome.omnibox.onInputChanged.addListener(_.debounce(function (queryText, suggestCallback) {
console.log("Changed:", queryText);
currentQueryString = queryText;
function dataHandler (data) {
if (data && !data.error) {
resultCache[queryText] = data;
}
if (currentQueryString !== queryText) { // We went past this query
return;
}
if (!data.items) {
chrome.omnibox.setDefaultSuggestion({description: "No results found for: <match>" + queryText + "</match>"});
return;
}
var results = _(data.items).
chain().
first(NO_OF_RESULTS).
map(function (item) {
var description = "<url>" + item.htmlFormattedUrl + "</url><dim> - " + item.htmlTitle + "</dim>";
description = description.replace(/<b>/gi, "<match>").replace(/<\/b>/gi, "</match>");
return {
content : item.link,
description: description
};
}).
push({
content : "[mdn]" + queryText,
description: "Search MDN for: <match>" + queryText + "</match>"
}).
value();
setDefaultSuggestion(results.shift());
suggestCallback(results);
}
if (!queryText) {
clearDefault(queryText);
return;
}
// Check if we cached results for this query
if (resultCache[queryText]) {
dataHandler(resultCache[queryText]);
return;
} else {
clearDefault(queryText);
}
$.getJSON("https://www.googleapis.com/customsearch/v1?callback=?", {
key : API_KEY,
alt : "json",
q : queryText,
num : NO_OF_RESULTS,
lr : "lang_en",
cx : "017146964052550031681:wnjobi1fzcm",
fields: "items(formattedUrl,htmlFormattedUrl,htmlTitle,link,title)"
}, dataHandler);
}, 250));
chrome.omnibox.onInputEntered.addListener(function (/*String*/queryText) {
// Navigate user to selected page or the search page
console.log("Entered:", queryText);
var url;
var isUrl = queryText.indexOf("http://") === 0 || queryText.indexOf("https://") === 0;
if (isUrl) {
url = queryText;
} else if (queryText == currentQueryString && !!latestDefault) {
url = latestDefault.content
} else {
var query = queryText.indexOf("[mdn]") == -1 ? queryText : queryText.slice("[mdn]".length);
url = "https://developer.mozilla.org/en-US/search?q=" + encodeURIComponent(query);
}
chrome.tabs.update({url: url});
});
chrome.omnibox.onInputStarted.addListener(function () {
console.log("Started");
});
chrome.omnibox.onInputCancelled.addListener(function () {
console.log("Cancelled");
});