7 var savedUserText
= null;
8 var suggestionIndex
= -1;
9 var suggestions
= ["result 1", "result 2", "http://www.google.com"];
11 var suggestionType
= 0; // INSTANT_SUGGESTION_SEARCH
12 var behavior
= 2; // INSTANT_COMPLETE_NEVER
13 var onMostVisitedChangedCalls
= 0;
14 var mostVisitedItemsCount
= 0;
15 var firstMostVisitedItemId
= 0;
16 var onNativeSuggestionsCalls
= 0;
17 var onChangeCalls
= 0;
19 var onEscKeyPressedCalls
= 0;
20 var onFocusChangedCalls
= 0;
21 var onToggleVoiceSearchCalls
= 0;
22 var prefetchQuery
= '';
23 var isFocused
= false;
24 var onvisibilitycalls
= 0;
25 var onThemeChangedCalls
= 0;
27 function getApiHandle() {
28 if (window
.navigator
&& window
.navigator
.searchBox
)
29 return window
.navigator
.searchBox
;
30 if (window
.chrome
&& window
.chrome
.searchBox
)
31 return window
.chrome
.searchBox
;
35 function getNewTabPageHandle() {
36 if (window
.navigator
&& window
.navigator
.embeddedSearch
&&
37 window
.navigator
.embeddedSearch
.newTabPage
)
38 return window
.navigator
.embeddedSearch
.newTabPage
;
39 if (window
.chrome
&& window
.chrome
.embeddedSearch
&&
40 window
.chrome
.embeddedSearch
.newTabPage
)
41 return window
.chrome
.embeddedSearch
.newTabPage
;
45 function handleNativeSuggestions() {
46 onNativeSuggestionsCalls
++;
47 // Showing the loader at 100% height.
49 apiHandle
.showOverlay();
52 function handleSubmit() {
53 location
.hash
= 'q=' + encodeURIComponent(apiHandle
.value
);
57 function handleOnChange() {
59 savedUserText
= apiHandle
.value
;
61 apiHandle
.setAutocompleteText(suggestion
, behavior
);
64 function nextSuggestion() {
65 if (suggestionIndex
< suggestions
.length
- 1) {
67 apiHandle
.setValue(suggestions
[suggestionIndex
], suggestionType
);
71 function previousSuggestion() {
72 if (suggestionIndex
!= -1) {
74 if (suggestionIndex
== -1) {
75 apiHandle
.setValue(savedUserText
, suggestionType
);
77 apiHandle
.setValue(suggestions
[suggestionIndex
], suggestionType
);
82 function handleKeyPress(event
) {
83 var VKEY_ESCAPE
= 0x1B;
87 if (event
.keyCode
== VKEY_ESCAPE
) {
88 onEscKeyPressedCalls
++;
89 if (suggestionIndex
!= -1) {
91 apiHandle
.setAutocompleteText(suggestion
, behavior
);
93 } else if (event
.keyCode
== VKEY_DOWN
) {
95 } else if (event
.keyCode
== VKEY_UP
) {
100 function handleMostVisitedChange() {
101 onMostVisitedChangedCalls
++;
102 var items
= newTabPageHandle
.mostVisited
;
104 mostVisitedItemsCount
= items
.length
;
105 firstMostVisitedItemId
= items
[0] ? items
[0].rid
: 0;
109 document
.addEventListener("visibilitychange", function() {
113 function handleFocusChange() {
114 onFocusChangedCalls
++;
115 isFocused
= apiHandle
.isFocused
;
118 function handleToggleVoiceSearch() {
119 onToggleVoiceSearchCalls
++;
122 function handleSuggestionChange() {
123 prefetchQuery
= getApiHandle().suggestion
.text
;
126 function handleThemeChange() {
127 onThemeChangedCalls
++;
131 apiHandle
= getApiHandle();
135 newTabPageHandle
= getNewTabPageHandle();
136 apiHandle
.onnativesuggestions
= handleNativeSuggestions
;
137 apiHandle
.onsubmit
= handleSubmit
;
138 apiHandle
.onchange
= handleOnChange
;
139 apiHandle
.onkeypress
= handleKeyPress
;
140 apiHandle
.onfocuschange
= handleFocusChange
;
141 apiHandle
.ontogglevoicesearch
= handleToggleVoiceSearch
;
142 apiHandle
.onsuggestionchange
= handleSuggestionChange
;
143 newTabPageHandle
.onmostvisitedchange
= handleMostVisitedChange
;
144 newTabPageHandle
.onthemechange
= handleThemeChange
;
145 if (apiHandle
.value
) {
146 handleNativeSuggestions();
149 if (newTabPageHandle
.mostVisited
&& newTabPageHandle
.mostVisited
.length
) {
150 handleMostVisitedChange();
159 <meta name=
"referrer" content=
"origin" />