-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
92 lines (75 loc) · 2.66 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
var defaultSuggestionURL = '';
chrome.runtime.onStartup.addListener(buildAndSetRepoMap);
chrome.runtime.onInstalled.addListener(buildAndSetRepoMap);
chrome.omnibox.onInputChanged.addListener(function(input, suggest) {
input = input.trim().toLowerCase();
chrome.storage.local.get('repoMap', function(storageObj) {
// Go through repoMap, find suggestions based on keyword
var repoMap = storageObj.repoMap;
var suggestions = [];
// See if we have multiple or just a single keyword
// Single keyword
if (input.split(' ').length == 1 && input != '') {
var keyword = input.trim().toLowerCase();
_.each(repoMap, function(repo, fullName) {
var suggestion = {
content: repo.url,
description: fullName
};
// Put exact match in the front of suggestions
if (repo.repoName.toLowerCase() == keyword) {
suggestions.unshift(suggestion);
} else if (_.contains(fullName.toLowerCase(), keyword)) {
suggestions.push(suggestion);
}
});
}
// Multiple keywords
else {
var keywords = input.trim().toLowerCase().split(' ');
_.each(repoMap, function(repo, fullName) {
var suggestion = {
content: repo.url,
description: fullName
};
var inFullName = function(keyword) {
return _.contains(fullName.toLowerCase(), keyword);
};
if (_.all(keywords, inFullName)) {
suggestions.push(suggestion);
}
});
}
// Use the first suggestion as default
var defaultSuggestionDescription;
if (suggestions.length > 0) {
defaultSuggestionDescription = '<match>' + suggestions[0].description + '</match>';
defaultSuggestionURL = suggestions[0].content;
} else {
defaultSuggestionDescription = '<match>No match found. Search github with "' +
input + '"</match>';
defaultSuggestionURL = 'https://github.com/search?q=' + input.replace(' ', '+');
}
chrome.omnibox.setDefaultSuggestion({
description: defaultSuggestionDescription
});
suggest(_.rest(suggestions));
});
});
chrome.omnibox.onInputEntered.addListener(function(input) {
var url;
// If input is a valid Github URL, the user has selected something else than the default option
if (_.startsWith(input, 'https://github.com/')) {
url = input;
} else {
url = defaultSuggestionURL;
}
chrome.tabs.query({ highlighted: true }, function(tab) {
chrome.tabs.update(tab.id, { url: url });
});
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
if (changeInfo.url) {
processNewURL(changeInfo.url);
}
});