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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// Copyright 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.
/**
* @fileoverview Support for omnibox behavior in offline mode or when API
* features are not supported on the server.
*/
// ==========================================================
// Enums.
// ==========================================================
/**
* Possible behaviors for navigateContentWindow.
* @enum {number}
*/
var WindowOpenDisposition = {
CURRENT_TAB: 1,
NEW_BACKGROUND_TAB: 2
};
/**
* The JavaScript button event value for a middle click.
* @type {number}
* @const
*/
var MIDDLE_MOUSE_BUTTON = 1;
// =============================================================================
// Util functions
// =============================================================================
/**
* The maximum number of suggestions to show.
* @type {number}
* @const
*/
var MAX_SUGGESTIONS_TO_SHOW = 5;
/**
* The omnibox input value during the last onnativesuggestions event.
* @type {string}
*/
var lastInputValue = '';
/**
* The ordered restricted ids of the currently displayed suggestions. Since the
* suggestions contain the user's personal data (browser history) the searchBox
* API embeds the content of the suggestion in a shadow dom, and assigns a
* random restricted id to each suggestion which is accessible to the JS.
* @type {Array.<number>}
*/
var restrictedIds = [];
/**
* The index of the currently selected suggestion or -1 if none are selected.
* @type {number}
*/
var selectedIndex = -1;
/**
* Shortcut for document.getElementById.
* @param {string} id of the element.
* @return {HTMLElement} with the id.
*/
function $(id) {
return document.getElementById(id);
}
/**
* Displays a suggestion.
* @param {Object} suggestion The suggestion to render.
* @param {HTMLElement} box The html element to add the suggestion to.
* @param {boolean} select True to select the selection.
*/
function addSuggestionToBox(suggestion, box, select) {
var suggestionDiv = document.createElement('div');
suggestionDiv.classList.add('suggestion');
if (select)
suggestionDiv.classList.add('selected');
var contentsContainer = document.createElement('div');
contentsContainer.className = 'contents';
var contents = suggestion.combinedNode;
contentsContainer.appendChild(contents);
suggestionDiv.appendChild(contentsContainer);
var restrictedId = suggestion.rid;
suggestionDiv.onclick = function(event) {
handleSuggestionClick(restrictedId, event);
};
restrictedIds.push(restrictedId);
box.appendChild(suggestionDiv);
}
/**
* Renders the input suggestions.
* @param {Array} nativeSuggestions An array of native suggestions to render.
*/
function renderSuggestions(nativeSuggestions) {
var box = document.createElement('div');
box.id = 'suggestionsBox';
$('suggestions-box-container').appendChild(box);
for (var i = 0, length = nativeSuggestions.length;
i < Math.min(MAX_SUGGESTIONS_TO_SHOW, length); ++i) {
// Select the first suggestion.
addSuggestionToBox(nativeSuggestions[i], box, i == 0);
}
}
/**
* Clears the suggestions being displayed.
*/
function clearSuggestions() {
$('suggestions-box-container').innerHTML = '';
restrictedIds = [];
selectedIndex = -1;
}
/**
* @return {integer} The height of the dropdown.
*/
function getDropdownHeight() {
return $('suggestions-box-container').offsetHeight;
}
/**
* Updates selectedIndex, bounding it between -1 and the total number of
* of suggestions - 1 (looping as necessary), and selects the corresponding
* suggestion.
* @param {boolean} increment True to increment the selected suggestion, false
* to decrement.
*/
function updateSelectedSuggestion(increment) {
var numSuggestions = restrictedIds.length;
if (!numSuggestions)
return;
var oldSelection = $('suggestionsBox').querySelector('.selected');
if (oldSelection)
oldSelection.classList.remove('selected');
if (increment)
selectedIndex = ++selectedIndex > numSuggestions - 1 ? -1 : selectedIndex;
else
selectedIndex = --selectedIndex < -1 ? numSuggestions - 1 : selectedIndex;
var apiHandle = getApiObjectHandle();
if (selectedIndex == -1) {
apiHandle.setValue(lastInputValue);
} else {
var newSelection = $('suggestionsBox').querySelector(
'.suggestion:nth-of-type(' + (selectedIndex + 1) + ')');
newSelection.classList.add('selected');
apiHandle.setRestrictedValue(restrictedIds[selectedIndex]);
}
}
// =============================================================================
// Handlers / API stuff
// =============================================================================
/**
* @return {Object} the handle to the searchBox API.
*/
function getApiObjectHandle() {
if (window.cideb)
return window.cideb;
if (window.navigator && window.navigator.embeddedSearch &&
window.navigator.embeddedSearch.searchBox)
return window.navigator.embeddedSearch.searchBox;
if (window.chrome && window.chrome.embeddedSearch &&
window.chrome.embeddedSearch.searchBox)
return window.chrome.embeddedSearch.searchBox;
return null;
}
/**
* chrome.searchBox.onnativesuggestions implementation.
*/
function handleNativeSuggestions() {
var apiHandle = getApiObjectHandle();
// Used to workaround repeated undesired asynchronous onnativesuggestions
// events and the fact that when a suggestion is clicked, the omnibox unfocus
// can cause onnativesuggestions to fire, preventing the suggestion onclick
// from registering.
if (lastInputValue == apiHandle.value && $('suggestionsBox')) {
return;
}
lastInputValue = apiHandle.value;
clearSuggestions();
var nativeSuggestions = apiHandle.nativeSuggestions;
if (nativeSuggestions.length) {
nativeSuggestions.sort(function(a, b) {
return b.rankingData.relevance - a.rankingData.relevance;
});
renderSuggestions(nativeSuggestions);
selectedIndex = 0;
apiHandle.setRestrictedAutocompleteText(
nativeSuggestions[selectedIndex].rid);
}
var height = getDropdownHeight();
// TODO(jered): Remove deprecated "reason" argument.
apiHandle.showOverlay(2, height);
}
/**
* Appends a style node for suggestion properties that depend on apiHandle.
*/
function appendSuggestionStyles() {
var apiHandle = getApiObjectHandle();
var style = document.createElement('style');
style.type = 'text/css';
style.id = 'suggestionStyle';
style.textContent =
'.suggestion {' +
' -webkit-margin-start: ' + apiHandle.startMargin + 'px;' +
' -webkit-margin-end: ' + apiHandle.endMargin + 'px;' +
' font: ' + apiHandle.fontSize + 'px "' + apiHandle.font + '";' +
'}';
document.querySelector('head').appendChild(style);
}
/**
* Extract the desired navigation behavior from a click event.
* @param {Event} event The click event.
* @return {WindowOpenDisposition} The desired behavior for
* navigateContentWindow.
*/
function getDispositionFromClick(event) {
if (event.button == MIDDLE_MOUSE_BUTTON)
return WindowOpenDisposition.NEW_BACKGROUND_TAB;
return WindowOpenDisposition.CURRENT_TAB;
}
/**
* Handles suggestion clicks.
* @param {integer} restrictedId The restricted id of the suggestion being
* clicked.
* @param {Event} event The click event.
*/
function handleSuggestionClick(restrictedId, event) {
clearSuggestions();
getApiObjectHandle().navigateContentWindow(restrictedId,
getDispositionFromClick(event));
}
/**
* chrome.searchBox.onkeypress implementation.
* @param {Object} e The key being pressed.
*/
function handleKeyPress(e) {
switch (e.keyCode) {
case 38: // Up arrow
updateSelectedSuggestion(false);
break;
case 40: // Down arrow
updateSelectedSuggestion(true);
break;
}
}
/**
* chrome.searchBox.embeddedSearch.onsubmit implementation.
*/
function onSubmit() {
}
/**
* Sets up the searchBox API.
*/
function setUpApi() {
var apiHandle = getApiObjectHandle();
apiHandle.onnativesuggestions = handleNativeSuggestions;
apiHandle.onkeypress = handleKeyPress;
apiHandle.onsubmit = onSubmit;
appendSuggestionStyles();
if (apiHandle.nativeSuggestions.length)
handleNativeSuggestions();
}
document.addEventListener('DOMContentLoaded', setUpApi);
|