-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathcompletion_engines.js
229 lines (208 loc) · 6.93 KB
/
completion_engines.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// A completion engine provides search suggestions for a custom search engine. A custom search
// engine is identified by a "searchUrl". An "engineUrl" is used for fetching suggestions, whereas a
// "searchUrl" is used for the actual search itself.
//
// Each completion engine defines:
//
// 1. An "engineUrl". This is the URL to use for search completions and is passed as the option
// "engineUrl" to the "BaseEngine" constructor.
//
// 2. One or more regular expressions which define the custom search engine URLs for which the
// completion engine will be used. This is passed as the "regexps" option to the "BaseEngine"
// constructor.
//
// 3. A "parse" function. This takes the text body of an HTTP response and returns a list of
// suggestions (a list of strings). This method is always executed within the context of a
// try/catch block, so errors do not propagate.
//
// 4. Each completion engine *must* include an example custom search engine. The example must
// include an example "keyword" and an example "searchUrl", and may include an example
// "description" and an "explanation". This info is shown as documentation to the user.
//
// Each new completion engine must be added to the list "CompletionEngines" at the bottom of this
// file.
//
// The lookup logic which uses these completion engines is in "./completion_search.js".
//
// A base class for common regexp-based matching engines. "options" must define:
// options.engineUrl: the URL to use for the completion engine. This must be a string.
// options.regexps: Regexes matching search URLs this engine handles. List of strings or RegExps.
// options.example: an example object containing at least "keyword" and "searchUrl", and optional "description".
// TODO(philc): This base class is doing very little. We should remove it and use composition.
class BaseEngine {
constructor(options) {
Object.assign(this, options);
this.regexps = this.regexps.map((regexp) => new RegExp(regexp));
}
match(searchUrl) {
return Utils.matchesAnyRegexp(this.regexps, searchUrl);
}
getUrl(queryTerms) {
return Utils.createSearchUrl(queryTerms, this.engineUrl);
}
parse(text) {
return JSON.parse(text)[1];
}
}
class Google extends BaseEngine {
constructor() {
super({
engineUrl: "http://suggestqueries.google.com/complete/search?client=chrome&q=%s",
regexps: ["^https?://[a-z]+\\.google\\.(com|ie|co\\.(uk|jp)|ca|com\\.au)/"],
example: {
searchUrl: "https://www.google.com/search?q=%s",
keyword: "g",
},
});
}
}
const googleMapsPrefix = "map of ";
class GoogleMaps extends BaseEngine {
constructor() {
super({
engineUrl:
`http://suggestqueries.google.com/complete/search?client=chrome&ds=yt&q=${googleMapsPrefix}%s`,
regexps: ["^https?://[a-z]+\\.google\\.(com|ie|co\\.(uk|jp)|ca|com\\.au)/maps"],
example: {
searchUrl: "https://www.google.com/maps?q=%s",
keyword: "m",
explanation: `\
This uses regular Google completion, but prepends the text "<tt>map of </tt>" to the query. It works
well for places, countries, states, geographical regions and the like, but will not perform address
search.\
`,
},
});
}
parse(text) {
return JSON.parse(text)[1]
.filter((suggestion) => suggestion.startsWith(googleMapsPrefix))
.map((suggestion) => suggestion.slice(googleMapsPrefix));
}
}
class Youtube extends BaseEngine {
constructor() {
super({
engineUrl: "http://suggestqueries.google.com/complete/search?client=chrome&ds=yt&q=%s",
regexps: ["^https?://[a-z]+\\.youtube\\.com/results"],
example: {
searchUrl: "https://www.youtube.com/results?search_query=%s",
keyword: "y",
},
});
}
}
class Wikipedia extends BaseEngine {
constructor() {
super({
engineUrl: "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=%s",
regexps: ["^https?://[a-z]+\\.wikipedia\\.org/"],
example: {
searchUrl: "https://www.wikipedia.org/w/index.php?title=Special:Search&search=%s",
keyword: "w",
},
});
}
}
class Bing extends BaseEngine {
constructor() {
super({
engineUrl: "https://api.bing.com/osjson.aspx?query=%s",
regexps: ["^https?://www\\.bing\\.com/search"],
example: {
searchUrl: "https://www.bing.com/search?q=%s",
keyword: "b",
},
});
}
}
class Amazon extends BaseEngine {
constructor() {
super({
engineUrl:
"https://completion.amazon.com/api/2017/suggestions?mid=ATVPDKIKX0DER&alias=aps&prefix=%s",
regexps: ["^https?://(www|smile)\\.amazon\\.(com|co\\.uk|ca|de|com\\.au)/s/"],
example: {
searchUrl: "https://www.amazon.com/s/?field-keywords=%s",
keyword: "a",
},
});
}
parse(text) {
return JSON.parse(text).suggestions.map((suggestion) => suggestion.value);
}
}
class DuckDuckGo extends BaseEngine {
constructor() {
super({
engineUrl: "https://duckduckgo.com/ac/?q=%s",
regexps: ["^https?://([a-z]+\\.)?duckduckgo\\.com/"],
example: {
searchUrl: "https://duckduckgo.com/?q=%s",
keyword: "d",
},
});
}
parse(text) {
return JSON.parse(text).map((suggestion) => suggestion.phrase);
}
}
class Webster extends BaseEngine {
constructor() {
super({
engineUrl: "https://www.merriam-webster.com/lapi/v1/mwol-search/autocomplete?search=%s",
regexps: ["^https?://www.merriam-webster.com/dictionary/"],
example: {
searchUrl: "https://www.merriam-webster.com/dictionary/%s",
keyword: "dw",
description: "Dictionary",
},
});
}
parse(text) {
return JSON.parse(text).docs.map((suggestion) => suggestion.word);
}
}
// Qwant is a privacy-friendly alternative search engine.
class Qwant extends BaseEngine {
constructor() {
super({
engineUrl: "https://api.qwant.com/api/suggest?q=%s",
regexps: ["^https?://www\\.qwant\\.com/"],
example: {
searchUrl: "https://www.qwant.com/?q=%s",
keyword: "qw",
},
});
}
parse(text) {
return JSON.parse(text).data.items.map((suggestion) => suggestion.value);
}
}
// A dummy search engine which is guaranteed to match any search URL, but never produces
// completions. This allows the rest of the logic to be written knowing that there will always be a
// completion engine match.
class DummyCompletionEngine extends BaseEngine {
constructor() {
super({
regexps: ["."],
dummy: true,
});
}
}
// On the user-facing documentation page pages/completion_engines.html, these completion search
// engines will be shown to the user in this order.
const CompletionEngines = [
Youtube,
GoogleMaps,
Google,
DuckDuckGo,
Wikipedia,
Bing,
Amazon,
Webster,
Qwant,
DummyCompletionEngine,
];
globalThis.CompletionEngines = globalThis.BuiltinCompletionEngines = CompletionEngines;
export { Amazon, BaseEngine, DuckDuckGo, Qwant, Webster };