-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
212 lines (187 loc) · 6.75 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
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
// Copyright (c) 2012 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.
var qbm = function() {
var bookmarks = "";
var waiting = false;
var currentText = "";
var suggestFunctionDelayed;
var defaultSuggestion = "";
var suggestion = function(content, description) {
if (content === "") {
return {
description: description
}
} else {
return {
content: content,
description: description
};
}
}
var setDefault = function(suggestion) {
defaultSuggestion = suggestion;
chrome.omnibox.setDefaultSuggestion(suggestion);
}
var treeLoader = function(bookmarkTree) {
bookmarks = bookmarkTree[0];
if (waiting) {
waiting = false;
setSuggestions();
}
}
var reset = function() {
setDefault(suggestion("", "Save Current In Bookmarks Bar"));
}
// When the user accepts an option.
var acceptHandler = function(text) {
console.log("Accepted [" + text + "]");
// TODO Handle command
}
var commandType = function(command, startsWith) {
return command.toLowerCase().indexOf(startsWith.toLowerCase()) === 0;
}
var getAction = function(command) {
if (commandType(command, "A")) {
return {con: "A ", desc: "<match>Add</match> <dim>to</dim>"};
} else if (commandType(command, "L")) {
return {con: "L ", desc: "<match>Load</match> <dim>to</dim>"};
}
return {con: "A ", desc: "Add?"};
}
var getPath = function(command) {
if (command.length <= 2) {
return "";
} else {
return command.substring(2);
}
}
/**
* Returns suggestion corresponding to a folder.
*/
var addFolder = function(folder) {
return {
con: espaceTitle(folder.title),
desc: folder.title
};
}
var escapeTitle = function(title) {
return (title.indexOf("/" >= 0)) ? "\"" + title + "\"" : title;
}
var getP = function(parentPath, thisPath) {
return (thisPath === "") ? "" : parentPath + thisPath + "/";
}
var getSuggestedPathsNew = function(pathTokens, bookmarkObject, parentPath) {
console.log("Calling: [" + pathTokens + "] obj: " + bookmarkObject.title);
if ((pathTokens.length === 1) && (pathTokens[0] === "")) {
pathTokens = [];
}
// If no tokens, return this object and all children.
// If one token:
// If perfect match with a kid, return call the kid after consuming token.
// Else all kids with partial prefix match.
// If more than one tokens:
// Consume first token: if there is a perfect match kid, call self on kid and restTokens.
var retPaths = [];
var thisFullPath = getP(parentPath, bookmarkObject.title);
if (pathTokens.length === 0) {
// If not root, add self.
if (bookmarkObject.id !== "0") {
retPaths.push({con: thisFullPath,
desc: thisFullPath});
}
// Add all children.
for (var i = 0; i < bookmarkObject.children.length; i++) {
if (!bookmarkObject.children[i].hasOwnProperty("url")) {
retPaths.push({con: getP(thisFullPath, bookmarkObject.children[i].title),
desc: getP(thisFullPath, bookmarkObject.children[i].title)});
}
}
} else if (pathTokens.length === 1) {
for (var i = 0; i < bookmarkObject.children.length; i++) {
if ((pathTokens[0] === bookmarkObject.children[i].title) && (!bookmarkObject.children[i].hasOwnProperty("url"))) {
return getSuggestedPathsNew([], bookmarkObject.children[i], thisFullPath);
}
}
// Else suggest prefix matches.
for (var i = 0; i < bookmarkObject.children.length; i++) {
if ((bookmarkObject.children[i].title.indexOf(pathTokens[0]) === 0) && (!bookmarkObject.children[i].hasOwnProperty("url"))) {
retPaths.push({con:getP(thisFullPath, bookmarkObject.children[i].title),
desc: getP(thisFullPath, bookmarkObject.children[i].title)});
}
}
} else {
for (var i = 0; i < bookmarkObject.children.length; i++) {
if ((bookmarkObject.children[i].title === pathTokens[0]) && (!bookmarkObject.children[i].hasOwnProperty("url"))) {
pathTokens.splice(0, 1);
return getSuggestedPathsNew(pathTokens, bookmarkObject.children[i], thisFullPath);
}
}
}
return retPaths;
}
var getSuggestedPaths = function(path) {
var retPaths = getSuggestedPathsNew(path.split("/"), bookmarks, "");
var removeThings = [];
for (var i = 0; i < retPaths.length; i++) {
if ((retPaths[i].con === "") || (retPaths[i].desc === "")){
removeThings.push(i);
}
}
for (var i = removeThings.length - 1; i >= 0; i--) {
retPaths.splice(removeThings[i], 1);
}
return retPaths;
}
var getChildrenPaths = function(folder, parentPath) {
var retPaths = [];
for (var i = 0; i < folder.children.length; i++) {
if (!folder.children[i].hasOwnProperty("url")) {
retPaths.push({con: parentPath + escapeTitle(folder.children[i].title),
desc: parentPath + folder.children[i].title});
}
}
return retPaths;
}
var encode = function(text) {
return String(text)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
var setSuggestions = function() {
if (bookmarks === "") {
waiting = true;
return;
}
var suggestions = [];
var action = getAction(currentText);
var path = getPath(currentText);
var suggestedPaths = getSuggestedPaths(path);
for (var i = 0; i < suggestedPaths.length; i++) {
suggestions.push(suggestion(action.con + suggestedPaths[i].con,
action.desc + " " + encode(suggestedPaths[i].desc)));
}
// TODO Populate Suggestions.
suggestFunctionDelayed(suggestions);
}
return {
init: function() {
reset();
chrome.omnibox.onInputStarted.addListener(function() {
chrome.bookmarks.getTree(treeLoader);
});
chrome.omnibox.onInputEntered.addListener(function(text) {
acceptHandler(text);
});
chrome.omnibox.onInputChanged.addListener(function(text, suggestFn) {
suggestFunctionDelayed = suggestFn;
currentText = text;
setSuggestions();
});
}
}
} ();
qbm.init();