-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathsearch_engines.js
558 lines (532 loc) · 17 KB
/
search_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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* This file contains search engine specific logic via search engine objects.
*
* A search engine object must provide the following:
* - {regex} urlPattern
* - {CSS selector} searchBoxSelector
* - {SearchResult[]} getSearchResults()
* - {None} changeTools(period)
*
* Optional functions/properties:
* - {CSS class name} highlightClass:
* Default: "default-focused-search-result"
* - {Array} tabs
* Default: []
* - {int} marginTop: used if top results are not entirely visible
* Default: 0
* - {int} marginBottom: used if bottom results are not entirely visible
* Default: 0
* - {CSS selector} endlessScrollingContainer: CSS selector of the container
* containing search results that need tracking for endless scrolling
* support.
* Default: null
*/
/* eslint-disable max-len */
class SearchResult {
/**
* @param {Element} element
* @param {function|null} anchorSelector
* @param {string} highlightClass
* @param {function|null} highlightedElementSelector
* @param {function|null} containerSelector
*/
constructor(element, anchorSelector, highlightClass,
highlightedElementSelector, containerSelector) {
this.element_ = element;
this.anchorSelector_ = anchorSelector;
this.highlightClass = highlightClass;
this.highlightedElementSelector_ = highlightedElementSelector;
this.containerSelector_ = containerSelector;
}
get anchor() {
if (!this.anchorSelector_) {
return this.element_;
}
return this.anchorSelector_(this.element_);
}
get container() {
if (!this.containerSelector_) {
return this.element_;
}
return this.containerSelector_(this.element_);
}
get highlightedElement() {
if (!this.highlightedElementSelector_) {
return this.element_;
}
return this.highlightedElementSelector_(this.element_);
}
}
// eslint-disable-next-line
/**
* @param {...[Element[], function|null]} includedSearchResults An array of
* tuples. Each tuple contains collection of the search results optionally
* accompanied with their container selector.
* @constructor
*/
const getSortedSearchResults = (
includedSearchResults, excludedNodeList = []) => {
const excludedResultsSet = new Set();
for (const node of excludedNodeList) {
excludedResultsSet.add(node);
}
const searchResults = [];
for (const results of includedSearchResults) {
for (const node of results.nodes) {
if (!excludedResultsSet.has(node)) {
// Prevent adding the same node multiple times.
excludedResultsSet.add(node);
searchResults.push(new SearchResult(
node,
results.anchorSelector,
results.highlightClass,
results.highlightedElementSelector,
results.containerSelector,
));
}
}
}
// Sort searchResults by their document position.
searchResults.sort((a, b) => {
const position = a.anchor.compareDocumentPosition(b.anchor);
if (position & Node.DOCUMENT_POSITION_FOLLOWING) {
return -1;
} else if (position & Node.DOCUMENT_POSITION_PRECEDING) {
return 1;
} else {
return 0;
}
});
return searchResults;
};
class GoogleSearch {
constructor(options) {
this.options = options;
}
get urlPattern() {
return /(www|encrypted)\.google\./;
}
get searchBoxSelector() {
// Must match search engine search box
// NOTE: we used '#searchform input[name=q]' before 2020-06-05 but that
// doesn't work in the images search tab. Another option is to use
// 'input[role="combobox"]' but this doesn't work when there's also a
// dictionary search box.
// return '#searchform input[name=q]',
return 'form[role=search] input[name=q]';
}
get marginTop() {
// When scrolling down, the search box can hide some of the search results,
// so we try to compensate for it.
const searchBoxContainer = document.querySelector('#searchform.minidiv');
if (!searchBoxContainer) {
return 0;
}
return searchBoxContainer.getBoundingClientRect().height;
}
isImagesTab() {
return /[?&]tbm=isch(&|$)/.test(location.search);
}
getSearchResults() {
// Don't initialize results navigation on image search, since it doesn't
// work there.
if (this.isImagesTab()) {
return [];
}
const includedElements = [
{
nodes: document.querySelectorAll('#search .r > a:first-of-type'),
highlightClass: 'default-focused-search-result',
containerSelector: (n) => n.parentElement.parentElement,
},
{
nodes: document.querySelectorAll('#search .r g-link > a:first-of-type'),
highlightClass: 'default-focused-search-result',
containerSelector: (n) => n.parentElement.parentElement,
},
{
nodes: document.querySelectorAll('div.zjbNbe > a'),
highlightClass: 'default-focused-search-result',
},
// Shopping results
{
nodes: document.querySelectorAll('div.eIuuYe a'),
highlightClass: 'default-focused-search-result',
},
{
nodes: document.querySelectorAll('#pnprev, #pnnext'),
highlightClass: 'default-focused-search-result',
},
];
if (this.options.googleIncludeCards) {
const nearestChildOrParentAnchor = (element) => {
const childAnchor = element.querySelector('a');
if (childAnchor && childAnchor.href) {
return childAnchor;
}
return element.closest('a');
};
const nearestCardContainer = (element) => {
return element.closest('g-inner-card');
};
includedElements.push(
// Top stories, Twitter, and videos.
{
nodes: document.querySelectorAll(
'[data-init-vis=true] [role=heading]'),
anchorSelector: nearestChildOrParentAnchor,
highlightedElementSelector: nearestCardContainer,
highlightClass: 'google-focused-card',
},
// Small top stories section.
{
nodes: document.querySelectorAll('.P5BnJb'),
anchorSelector: (n) => n.parentElement,
highlightClass: 'default-focused-search-result',
},
);
}
return getSortedSearchResults(includedElements,
document.querySelectorAll(
'#search .kp-blk:not(.c2xzTb) .r > a:first-of-type'),
);
}
// Array storing tuples of tabs navigation keybindings and their corresponding
// CSS selector
get tabs() {
if (this.isImagesTab()) {
const visibleTabs = document.querySelectorAll('.T47uwc > a');
// NOTE: The order of the tabs after the first two is dependent on the
// query. For example:
// - "cats": videos, news, maps
// - "trump": news, videos, maps
// - "california": maps, news, videos
return {
navigateSearchTab: visibleTabs[0],
navigateMapsTab: document.querySelector(
'.T47uwc > a[href*="maps.google."]'),
navigateVideosTab: document.querySelector(
'.T47uwc > a[href*="&tbm=vid"]'),
navigateNewsTab: document.querySelector(
'.T47uwc > a[href*="&tbm=nws"]'),
navigateShoppingTab: document.querySelector(
'a[role="menuitem"][href*="&tbm=shop"]'),
navigateBooksTab: document.querySelector(
'a[role="menuitem"][href*="&tbm=bks"]'),
navigateFlightsTab: document.querySelector(
'a[role="menuitem"][href*="&tbm=flm"]'),
navigateFinancialTab: document.querySelector(
'a[role="menuitem"][href*="&tbm=fin"]'),
};
}
return {
navigateSearchTab: document.querySelector(
'a.q.qs:not([href*="&tbm="]):not([href*="maps.google."])'),
navigateImagesTab: document.querySelector('a.q.qs[href*="&tbm=isch"]'),
navigateVideosTab: document.querySelector('a.q.qs[href*="&tbm=vid"]'),
navigateMapsTab: document.querySelector('a.q.qs[href*="maps.google."]'),
navigateNewsTab: document.querySelector('a.q.qs[href*="&tbm=nws"]'),
navigateShoppingTab: document.querySelector('a.q.qs[href*="&tbm=shop"]'),
navigateBooksTab: document.querySelector('a.q.qs[href*="&tbm=bks"]'),
navigateFlightsTab: document.querySelector('a.q.qs[href*="&tbm=flm"]'),
navigateFinancialTab: document.querySelector('a.q.qs[href*="&tbm=fin"]'),
navigatePreviousResultPage: document.querySelector('#pnprev'),
navigateNextResultPage: document.querySelector('#pnnext'),
};
}
/**
* Filter the results based on special properties
* @param {*} period, filter identifier. Accpeted filter are :
* 'h' : get results from last hour
* 'd' : get result from last day
* 'w' : get results from last week
* 'm' : get result from last month
* 'y' : get result from last year
* 'v' : verbatim search
*/
changeTools(period) {
// Save current period and sort.
const res = /&(tbs=qdr:.)(,sbd:.)?/.exec(location.href);
const currPeriod = (res && res[1]) || '';
const currSort = (res && res[2]) || '';
// Remove old period and sort.
const strippedHref = location.href.replace(/&tbs=qdr:.(,sbd:.)?/, '');
if (period) {
location.href = strippedHref +
(period === 'a' ? '' : '&tbs=qdr:' + period + currSort);
} else if (currPeriod) {
// Can't apply sort when not using period.
location.href = strippedHref +
'&' + currPeriod + (currSort ? '' : ',sbd:1');
}
}
}
class StartPage {
constructor(options) {
this.options = options;
}
get urlPattern() {
return /(www\.)?startpage\./;
}
get searchBoxSelector() {
return '.search-form__form input[id=q]';
}
get marginTop() {
if (this.isSearchTab()) {
return 113;
}
return 0;
}
isSearchTab() {
return document.querySelector('div.layout-web') != null;
}
isImagesTab() {
return document.querySelector('div.layout-images') != null;
}
getSearchResults() {
// Don't initialize results navigation on image search, since it doesn't
// work there.
if (this.isImagesTab()) {
return [];
}
const highlightedElementSelector = (element) => {
if (this.isSearchTab()) {
return element.closest('.w-gl__result');
}
return element;
};
const includedElements = [
{
nodes: document.querySelectorAll(
'.w-gl--default.w-gl .w-gl__result a.w-gl__result-url'),
highlightedElementSelector: highlightedElementSelector,
highlightClass: 'startpage-focused-search-result',
containerSelector: (n) => n.parentElement,
},
// As of 2020-06-20, this doesn't seem to match anything.
{
nodes: document.querySelectorAll(
'.vo-sp.vo-sp--default > a.vo-sp__link'),
highlightedElementSelector: highlightedElementSelector,
highlightClass: 'startpage-focused-search-result',
},
];
return getSortedSearchResults(includedElements, []);
}
get tabs() {
const menuLinks = document.querySelectorAll('.inline-nav-menu__link');
if (!menuLinks || menuLinks.length < 4) {
return {};
}
return {
navigateSearchTab: menuLinks[0],
navigateImagesTab: menuLinks[1],
navigateVideosTab: menuLinks[2],
navigateNewsTab: menuLinks[3],
navigatePreviousResultPage: document.querySelector(
'form.pagination__form.next-prev-form--desktop:first-of-type'),
navigateNextResultPage: document.querySelector(
'form.pagination__form.next-prev-form--desktop:last-of-type'),
};
}
changeTools(period) {
const forms = document.forms;
let timeForm;
for (let i = 0; i < forms.length; i++) {
if (forms[i].className === 'search-filter-time__form') {
timeForm = forms[i];
}
}
switch (period) {
case 'd':
timeForm.elements['with_date'][1].click();
break;
case 'w':
timeForm.elements['with_date'][2].click();
break;
case 'm':
timeForm.elements['with_date'][3].click();
break;
case 'y':
timeForm.elements['with_date'][4].click();
break;
default:
break;
}
}
}
class Youtube {
constructor(options) {
this.options = options;
}
get urlPattern() {
return /(www)\.youtube\./;
}
get searchBoxSelector() {
return 'input#search';
}
get endlessScrollingContainer() {
return document.querySelector('div#contents div#contents');
}
getSearchResults() {
const includedElements = [
// Videos
{
nodes: document.querySelectorAll('#title-wrapper h3 a#video-title'),
highlightClass: 'youtube-focused-search-result',
containerSelector: (n) => n.parentElement.parentElement,
},
// Playlists
{
nodes: document.querySelectorAll('div#content a.ytd-playlist-renderer'),
highlightClass: 'youtube-focused-search-result',
},
{
nodes: document.querySelectorAll(
'div#content a.ytd-playlist-video-renderer'),
highlightClass: 'youtube-focused-search-result',
},
// Mixes
{
nodes: document.querySelectorAll('div#content a.ytd-radio-renderer'),
highlightClass: 'youtube-focused-search-result',
},
// Channels
{
nodes: document.querySelectorAll('div#info.ytd-channel-renderer'),
highlightClass: 'youtube-focused-search-result',
},
];
return getSortedSearchResults(includedElements, []);
}
get tabs() {
// TODO: Support tabs in Youtube.
return {};
}
changeTools(period) {
if (!document.querySelector('div#collapse-content')) {
const toggleButton = document.querySelectorAll(
'a.ytd-toggle-button-renderer')[0];
// Toggling the buttons ensures that div#collapse-content is loaded
toggleButton.click();
toggleButton.click();
}
const forms = document.querySelectorAll(
'div#collapse-content > *:first-of-type ytd-search-filter-renderer');
let neededForm = null;
switch (period) {
case 'h':
neededForm = forms[0];
break;
case 'd':
neededForm = forms[1];
break;
case 'w':
neededForm = forms[2];
break;
case 'm':
neededForm = forms[3];
break;
case 'y':
neededForm = forms[4];
break;
}
if (neededForm) {
neededForm.childNodes[1].click();
}
}
}
class GoogleScholar {
constructor(options) {
this.options = options;
}
get urlPattern() {
return /scholar\.google\.com\/scholar/;
}
get searchBoxSelector() {
return '#gs_hdr_tsi';
}
getSearchResults() {
const includedElements = [
{
nodes: document.querySelectorAll('.gs_rt a'),
highlightClass: 'default-focused-search-result',
containerSelector: (n) => n.parentElement.parentElement,
},
{
nodes: document.querySelectorAll(
'.gs_ico_nav_previous, .gs_ico_nav_next'),
anchorSelector: (n) => n.parentElement,
highlightClass: 'google-scholar-next-page',
highlightedElementSelector: (n) => n.parentElement.children[1],
containerSelector: (n) => n.parentElement.children[1],
},
];
return getSortedSearchResults(includedElements, []);
}
get tabs() {
return {
navigatePreviousResultPage:
document.querySelector('.gs_ico_nav_previous').parentElement,
navigateNextResultPage:
document.querySelector('.gs_ico_nav_next').parentElement,
};
}
}
class Amazon {
constructor(options) {
this.options = options;
}
get urlPattern() {
return /(www\.)?amazon\.com\/s\?/;
}
get searchBoxSelector() {
return '#twotabsearchtextbox';
}
getSearchResults() {
const includedElements = [
{
nodes: document.querySelectorAll(
'.s-search-results h2 .a-link-normal.a-text-normal'),
highlightClass: 'amazon-focused-search-result',
// containerSelector: (n) => n.closest('.sg-row').parentElement.closest('.sg-row')
},
{
nodes: document.querySelectorAll('.a-pagination a'),
highlightClass: 'amazon-focused-search-result',
},
];
return getSortedSearchResults(includedElements, []);
}
get tabs() {
const pagesTabs = {
navigateNextResultPage:
document.querySelector('.a-pagination .a-last a'),
};
const paginationContainer = document.querySelector('.a-pagination');
if (paginationContainer && paginationContainer.children[0] && !paginationContainer.children[0].classList.contains('a-normal')) {
pagesTabs.navigatePreviousResultPage =
paginationContainer.children[0].querySelector('a');
}
return pagesTabs;
}
}
// Get search engine object matching the current url
/* eslint-disable-next-line no-unused-vars */
const getSearchEngine = (options) => {
const searchEngines = [
new GoogleSearch(options),
new StartPage(options),
new Youtube(options),
new GoogleScholar(options),
new Amazon(options),
];
// Switch over all compatible search engines
const href = window.location.href;
for (let i = 0; i < searchEngines.length; i++) {
if (href.match(searchEngines[i].urlPattern)) {
return searchEngines[i];
}
}
return null;
};