-
Notifications
You must be signed in to change notification settings - Fork 1
/
suggestions.js
80 lines (63 loc) · 1.92 KB
/
suggestions.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
/*obj.sort(function(a,b){
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
})
var list_players = []
chrome.omnibox.onInputChanged.addListener(
function(text, suggest) {
list_players = obj.filter(function(elem){
return (text === elem.substr(0,text.length));
})
list_players = list_players.slice(0.5);
console.log('inputChanged: ' + text);
suggest([{content: 'hi', description: 'you ;)'}]);
});*/
// This is copied straight from the example.
// This event is fired each time the user updates the text in the omnibox,
// as long as the extension's keyword mode is still active.
var commands = new Array("apps", "events", "friends", "gender", "groups", "home", "id", "msg", "pics", "username");
function buildResultsList(text) {
var indexOfSpace = text.indexOf(" ");
var results;
if (text == "")
return [];
if (indexOfSpace == -1) {
results = commands.filter(function(cmd) {
if (cmd.length < text)
return false;
if (cmd.substr(0,text.length) == text)
return true;
return false;
});
return results.map(function(a) {
return {content: a, description: "command: " + a};
});
}
else {
nameText = text.substr(indexOfSpace+1);
var results = JSON.parse(localStorage['userData']);
results = results.filter(function(a) {
if (a.name.length <= nameText) {
return false;
}
if (a.name.substr(0,nameText.length).toLowerCase() == nameText.toLowerCase()) {
return true;
}
return false;
});
return results.map(function (a) {
return {content: text.substr(0,indexOfSpace) + " " + a.name, description: a.name + "'s " + text.substr(0,indexOfSpace)};
});
}
}
chrome.omnibox.onInputChanged.addListener(
function(text, suggest) {
var results = buildResultsList(text);
if (results.length > 5)
suggest(results.slice(0,5));
else
suggest(results);
});