-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathbackground.js
More file actions
210 lines (147 loc) · 7.26 KB
/
background.js
File metadata and controls
210 lines (147 loc) · 7.26 KB
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
(function(){
function navigateTo( url ) {
chrome.tabs.getSelected( null, function( tab ) {
chrome.tabs.update( tab.id, { url: url } );
} );
};
function setDefaultSuggestion( text ) {
if ( text ) {
chrome.omnibox.setDefaultSuggestion( { "description":"<url><match>Java API Search</match></url> " + text } );
} else {
chrome.omnibox.setDefaultSuggestion( { "description":"<url><match>Java API Search</match></url>" } );
}
};
chrome.omnibox.onInputCancelled.addListener(function() {
setDefaultSuggestion( '' );
});
setDefaultSuggestion( '' );
chrome.omnibox.onInputChanged.addListener( function( text, suggest_callback ) {
setDefaultSuggestion( text );
if( !text ) {
return;
}
var kMaxSuggestions = 10;
var suggestions = [];
var stripped_text = text.strip();
if (!stripped_text) {
return;
}
var qlower = stripped_text.toLowerCase();
var second = [];
var third = [];
var fourth = [];
if ( index ) {
for ( var i = 0; i < index.length; ++i) {
var item = index[ i ];
var fqn = item[ "fqn" ];
var url = "http://docs.oracle.com/javase/7/docs/api/" + item[ "url" ];
var name = item[ "name" ];
var type = item[ "type" ];
var namelower = name.toLowerCase();
var nameidx = namelower.indexOf( qlower );
var fqnlower = fqn.toLowerCase();
if ( ( nameidx != -1 ) || fqnlower.startsWith( qlower ) ) {
var entry = {
"content" : url,
"description" : [ "<match>", name, "</match> <dim>(", type, " <match>", fqn, "</match>)</dim> - <url>", url, "</url>" ].join( '' )
};
if ( fqnlower.startsWith( "org." ) ) {
fourth.push( entry );
} else if ( nameidx == 0 ) {
suggestions.push( entry );
} else if ( nameidx != -1 ) {
second.push( entry );
} else {
third.push( entry );
}
if ( suggestions.length > kMaxSuggestions ) {
break;
}
}
}
}
if( suggestions.length < kMaxSuggestions ) {
for( var i = 0; i < second.length; ++i ) {
suggestions.push( second[ i ] );
}
}
if( suggestions.length < kMaxSuggestions ) {
for ( var i = 0; i < third.length; ++i ) {
suggestions.push( third[ i ] );
}
}
if( suggestions.length < kMaxSuggestions ) {
for ( var i = 0; i < fourth.length; ++i ) {
suggestions.push( fourth[ i ] );
}
}
if( stripped_text.length >= 2 ) {
suggestions.push( { "content" : stripped_text + " [Google Code Search]",
"description" : [ "Search for \"<match>", stripped_text, "</match> <dim>lang:java</dim>\" using <match><url>Google Code Search</url></match> - <url>http://code.google.com/codesearch#search/&q=", encodeURIComponent( stripped_text + " lang:java" ), "</url>" ].join( '' ) } );
suggestions.push( { "content" : stripped_text + " [Development and Coding Search]",
"description" : [ "Search for \"<match>", stripped_text, "</match>\" using <match><url>Develoment and Coding Search</url></match> - <url>http://www.google.com/cse?cx=005154715738920500810:fmizctlroiw&q=", encodeURIComponent( stripped_text ), "</url>" ].join( '' ) } );
}
suggest_callback( suggestions );
});
chrome.omnibox.onInputEntered.addListener( function( text ) {
if( !text ) {
navigateTo( "http://docs.oracle.com/javase/7/docs/api/" );
return;
}
var stripped_text = text.strip();
if( !stripped_text || stripped_text == ' ' ) {
navigateTo( "http://docs.oracle.com/javase/7/docs/api/" );
return;
}
if( stripped_text.startsWith( "http://" ) || stripped_text.startsWith( "https://" ) ) {
navigateTo( stripped_text );
return;
}
if( stripped_text.startsWith( "www." ) || stripped_text.endsWith( ".com" ) || stripped_text.endsWith( ".net" ) || stripped_text.endsWith( ".org" ) || stripped_text.endsWith( ".edu" ) ) {
navigateTo( "http://" + stripped_text );
return;
}
var google_codesearch_suffix = " [Google Code Search]";
if( stripped_text.endsWith(google_codesearch_suffix ) ) {
var newquery = stripped_text.substring( 0, stripped_text.length - google_codesearch_suffix.length ).strip();
navigateTo( "http://code.google.com/codesearch#search/&q=" + encodeURIComponent( newquery + " lang:java" ) );
return;
}
var devsearch_suffix = " [Development and Coding Search]";
if( stripped_text.endsWith( devsearch_suffix ) ) {
var newquery = stripped_text.substring(0, stripped_text.length - devsearch_suffix.length).strip();
navigateTo("http://www.google.com/cse?cx=005154715738920500810:fmizctlroiw&q=" + encodeURIComponent(newquery));
return;
}
var qlower = stripped_text.toLowerCase();
var backup_url = null;
if( index ) {
for ( var i = 0; i < index.length; ++i ) {
var item = index[ i ];
var fqn = item[ "fqn" ];
var url = "http://docs.oracle.com/javase/7/docs/api/" + item[ "url" ];
var name = item[ "name" ];
var fqnlower = fqn.toLowerCase();
if ( fqnlower == qlower ) {
navigateTo( url );
return;
}
if ( name.toLowerCase() == qlower ) {
if ( fqnlower.startsWith( "org." ) ) {
if ( !backup_url ) {
backup_url = url;
}
} else {
navigateTo( url );
return;
}
}
}
}
if ( backup_url ) {
navigateTo( backup_url );
return;
}
navigateTo( "http://www.google.com/search?q=" + encodeURIComponent( "Java "+stripped_text ) );
});
})();