forked from yanorei32/GSI4D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsi4d.user.js
661 lines (564 loc) · 21 KB
/
gsi4d.user.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// ==UserScript==
// @name GSI4D - Google search improve for developers
// @description Google search improve for developers.
// @version 1.4.12
// @include /^https://www\.google\.co(m|\.jp)/search.+$/
// @author yanorei32(modified by mustcodeal)
// @supportURL https://github.com/yanorei32/GSI4D/issues
// @website http://yano.teamfruit.net/~rei/
// @namespace http://yano.teamfruit.net/~rei/
// @updateURL https://raw.githubusercontent.com/yanorei32/GSI4D/master/gsi4d.user.js
// @license MIT License
// @run-at document-end
// @grant none
// ==/UserScript==
(function () {
'use strict';
function debounce(func, wait) {
let timeout;
return function() {
const context = this, args = arguments;
const later = function() {
timeout = null;
func.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
const site = {
reference: {
isBlackList: false,
color: '#0F01',
list: [
// Microsoft
'windows.com',
'msdn.microsoft.com',
'docs.microsoft.com',
// Oracle
'docs.oracle.com',
// Mozilla
'developer.mozilla.org',
// Google
'cloud.google.com',
'developers.google.com',
// RedHat
'access.redhat.com/documentation',
// Docker
'hub.docker.com',
// GitHub
'github.com',
// OSDN
'osdn.net',
// sourceforge
'sourceforge.net',
// Golang
'golang.org',
'godoc.org',
// Ruby
'ruby-lang.org',
'ruby-doc.org',
'railsdoc.com',
'rubygems.org',
// Python
'postgresql.org',
'python.org',
'requests-docs-ja.readthedocs.io',
'python-requests.org',
// Raspberry Pi
'raspberrypi.org',
// IPA
'ipa.go.jp',
// w3school.com
'w3schools.com',
// PHP
'php.net',
// Apache
'apache.org',
// Unity 3D
'docs.unity3d.com',
// Rustlang
'doc.rust-lang.org',
'rust-lang.org',
'docs.rs',
'source.chromium.org',
'graphql.org',
'docs.diesel.rs',
'sm.alliedmods.net',
],
},
recommend: {
isBlacklist: false,
color: '#0FF1',
list: [
'30secondsofcode.org',
'actix.rs',
'pypi.org',
'andreasbm.github.io',
'arewewebyet.org',
'arewegameyet.rs',
'arewedistributedyet.rs',
'arewefastyet.com',
'areweguiyet.com',
'gist.github.com',
'mysqltutorial.org',
'awesome-rust.com',
'awesome.red-badger.com',
'blog.cloudflare.com',
'blog.logrocket.com',
'blog.trailofbits.com',
'cbyexample.com',
'cheat.sh',
'cheatography.com',
'cheatsheetseries.owasp.org',
'codechalleng.es',
'cppcheatsheet.com',
'cpppatterns.com',
'cppreference.com',
'crates.io',
'csharp-station.com',
'css-tricks.com',
'csslayout.io',
'ctf101.org',
'dev.to',
'devdocs.io',
'developer.amazon.com',
'developer.apple.com',
'developer.mozilla.org',
'developer.valvesoftware.com',
'doc.qt.io',
'doc.rust-lang.org',
'docs.near.org',
'digitalocean.com',
'freecodecamp.org',
'codeacademy.com',
'geeksforgeeks.org',
'getfrontend.tips',
'golang.org',
'grepper.com',
'htmq.com',
'learnxinyminutes.com',
'lib.rs',
'martin.kleppmann.com',
'mui.com',
'nodejs.dev',
'os.phil-opp.com',
'programming-idioms.org',
'realpython.com',
'refactoring.guru',
'roadmap.sh',
'rosettacode.org',
'rust-lang.github.io',
'rust-unofficial.github.io',
'rust-lang-nursery.github.io',
'rustwasm.github.io',
'searchcode.com',
'serde.rs',
'source.chromium.org',
'sourcegraph.com',
'sourcemod.net',
'systemdesign.one',
'tauri.app',
'tohoho-web.com',
'tokio.rs',
'tutorialspoint.com',
'wiki.osdev.org',
'windowscentral.com',
'webassemblyman.com',
'composingprograms.com',
'learncomputerscienceonline.com',
'exploit-db.com',
'infoq.com',
'yew.rs',
'zenrows.com',
],
},
recommendForum: {
isBlacklist: false,
color: '#3331',
list: [
'stackexchange.com',
'stackoverflow.com',
'superuser.com',
'teratail.com',
'askubuntu.com',
'reddit.com',
'answers.unity.com',
'indiehackers.com',
'medium.com',
'alliedmods.net',
],
},
// 任意のユーザーが使えるサービス。比較的良質な物が多い。
publicService: {
isBlacklist: false,
color: '#FF01',
list: [
'qiita.com',
],
},
blackList: {
isBlacklist: true,
color: null,
list: [
// Wikipedia スクレイピング(?)
'wikiwand.com',
// 微妙な入門講座が引っかかった事があった。
'employment.en-japan.com',
// 誤った情報を大々的に掲載している。
'profession-net.com',
'symmetric.co.jp',
'mupon.net',
'marucoblog.com',
'suke-log.com',
'proengineer.internous.co.jp',
// ADBlockerが有効だとコンテンツを見せない
'server-setting.info',
// 侍エンジニア。画像が多くて嫌い。
'sejuku.net',
// 画像や広告が多くて嫌い。
'techacademy.jp',
'programming-study.com',
'codecamp.jp',
'tadaken3.hatenablog.jp',
'udemy.benesse.co.jp',
'techplay.jp',
'furien.jp',
'eng-entrance.com',
'unitopi.com',
'internetacademy.jp',
'kurashi-no.jp',
'kokinn.com',
// コピペ(翻訳)サイト
'bunnyinside.com',
'forsenergy.com',
'jp.mytory.net',
'phpspot.net/php/man/',
'code.i-harness.com',
'stackovernet.com',
'stackoverrun.com',
'codeday.me',
'code-examples.net',
'code-adviser.com',
'kotaeta.com',
'tutorialmore.com',
'living-sun.com',
'it-swarm.net',
'voidcc.com',
'qastack.jp',
'coder.work',
'it-swarm.dev',
'sobrelinux.info',
// AdSite
'solvusoft.com',
'reviversoft.com',
'dll-files.com',
'softonic.com',
'softonic.jp',
'systweak.com',
'chip.de',
'qpdownload.com',
'jaleco.com',
'findmysoft.com',
'akvatoriyal.ru',
'89139849001.ru',
'ukgorod37.ru',
'krasota-olimpia.ru',
'korobkaoo.ru',
'bkrolik.ru',
'hockorder.ru',
],
},
};
const SearchTypes = {
Default: '',
Image: 'isch',
Video: 'vid',
News: 'nws',
Shop: 'shop',
Books: 'bks',
Patent: 'ptd',
Unknown: null,
};
const deleteElement = (e) => {
if (e.parentElement !== null)
e.parentElement.removeChild(e);
};
const formatGSI4DLog = (log) => {
if (!log) {
return 'No log to report';
}
if (!log.blockedCount && !log.trackedCount) {
return 'No blocked or tracked connections';
}
return `GSI4D Blocked: ${log.blockedCount} Tracked: ${log.trackedCount}`;
};
const pageStyle = {
PC: {
supportSearchTypes: [
SearchTypes.Default,
SearchTypes.Video,
SearchTypes.Image,
],
getLinkElems: (searchType, parentElement) => {
let linkElements;
if (searchType === SearchTypes.Image) {
linkElements = parentElement.querySelectorAll('a.VFACy.kGQAp.sMi44c.lNHeqe');
} else {
linkElements = parentElement.querySelectorAll('div.g a:not(.fl):not(.GHDvEf)');
}
return linkElements || [];
},
coloriseCandidateByLinkElem: (searchType, linkElem, color) => {
const isImageSearch = searchType === SearchTypes.Image;
const candidateElem = isImageSearch ? linkElem.parentElement : linkElem.parentElement.parentElement.parentElement;
if (!candidateElem) {
return;
}
candidateElem.style.backgroundColor = color;
},
// Delete the element containing the candidate
// link, which is the grandparent of the link.
deleteCandidateByLinkElem: (searchType, linkElem) => {
switch (searchType) {
case SearchTypes.Image:
deleteElement(linkElem.parentElement);
return;
case SearchTypes.Video:
deleteElement(
linkElem.parentElement.parentElement
.parentElement
);
return;
case SearchTypes.Audio:
deleteElement(
linkElem.parentElement.parentElement
.parentElement
.parentElement
);
return;
default:
return;
}
},
writeLog: (st, formatedLog) => {
if (st === SearchTypes.Image)
return;
document.getElementById('result-stats').innerHTML += formatedLog;
},
after: (st) => {
if (st !== SearchTypes.Image) return;
const observerOptions = {childList: true, subtree: true}; // Added subtree to observe deeper changes
const processMutations = debounce((records, observer) => {
for (const record of records) {
for (const addedNode of record.addedNodes) {
if (addedNode.nodeType == Node.ELEMENT_NODE) {
linkProcess(st, addedNode);
}
}
}
}, 300);
const observer = new MutationObserver(processMutations);
// Start observing
observer.observe(document.querySelector('.islrc'), observerOptions);
},
},
Phone: {
supportSearchTypes: [],
// Get the links to the results
getLinkElems: (searchType, parentElement) => {
// If we're searching for images, get the image links
if (searchType === SearchTypes.Image)
return document.querySelectorAll('.iKjWAf');
// Otherwise, get the web links
try {
return parentElement.querySelectorAll('div.kCrYT>a');
} catch (e) {
return [];
}
},
// The linkE parameter is the link element that was clicked on.
// The color parameter is the color to use to highlight the candidate
// link elements.
coloriseCandidateByLinkElem: (searchType, linkE, color) => {
if (!linkE) {
return;
}
if (searchType === SearchTypes.Image) {
linkE.style.backgroundColor = color;
return;
}
linkE.parentElement.parentElement.style.backgroundColor = color;
},
deleteCandidateByLinkElem: (searchType, linkElem) => {
if (searchType === SearchTypes.Image) {
const imageContainer = linkElem.parentElement;
if (imageContainer) {
deleteElement(imageContainer);
}
return;
}
const articleContainer = linkElem.parentElement.parentElement.parentElement;
if (articleContainer) {
deleteElement(articleContainer);
}
},
writeLog: (searchType, formattedLog) => {
if (searchType === SearchTypes.Image)
return;
const logCardContainer = document.createElement('div');
const logCard = document.createElement('div');
const logCardSpan = document.createElement('span');
const logCardText = document.createElement('div');
logCardContainer.classList.add('ZINbbc', 'xpd', 'O9g5cc', 'uUPGi', 'gsi4d');
logCard.classList.add('kCrYT');
logCardText.classList.add('BNeawe', 's3v9rd', 'AP7Wnd');
logCardText.textContent = formattedLog;
logCardSpan.appendChild(logCardText);
logCard.appendChild(logCardSpan);
logCardContainer.appendChild(logCard);
const mainNode = document.getElementById('main');
if (mainNode && mainNode.childNodes.length > 1)
mainNode.childNodes[1].after(logCardContainer);
else
document.body.appendChild(logCardContainer);
},
after: (st) => {
if (st !== SearchTypes.Image) return;
const observerOptions = {childList: true, subtree: true}; // Added subtree to observe deeper changes
const observerCallback = (records, observer) => {
// Pause the observer
observer.disconnect();
for (const record of records) {
if (record.type !== "childList") continue; // Only process childList mutations
for (const addedNode of record.addedNodes) {
if (addedNode.nodeType == Node.ELEMENT_NODE) {
linkProcess(st, addedNode);
}
}
}
// Restart the observer after a delay to reduce CPU usage
setTimeout(() => {
observer.observe(document.querySelector('#islrg'), observerOptions);
}, 300); // 300ms delay
};
const observer = new MutationObserver(observerCallback);
// Start observing
observer.observe(document.querySelector('#islrg'), observerOptions);
},
},
};
const getPageStyle = () => {
if (window.navigator.userAgent.toLowerCase().indexOf('mobile') !== -1) {
console.log('PageStyle: Phone');
return pageStyle.Phone;
}
console.log('PageStyle: PC');
return pageStyle.PC;
};
const parseURL = (url) => {
if (!url.startsWith('/url?') && url.indexOf('://'))
return url;
if (!url.startsWith('/url?')) {
console.error('Unsupported URL: ' + url);
return '';
}
const paramStrs = url.split('?')[1].split('&');
for (const paramStr of paramStrs) {
const parsedParam = paramStr.split('=');
if (parsedParam[0] !== 'q')
continue;
if (parsedParam.length !== 2)
continue;
if (parsedParam[1].indexOf('://')) {
return parsedParam[1];
}
return decodeURI(parsedParam[1]);
}
console.error('Failed to read URL from /url?: ' + url);
return '';
};
const parseSearchURL = (url) => {
const parameterPairs = url.split('?')[1].split('&');
for (const parameterPair of parameterPairs) {
const parameter = parameterPair.split('=');
if (parameter[0] !== 'tbm')
continue;
if (parameter.length !== 2)
continue;
for (const [_, searchType] of Object.entries(SearchTypes))
if (parameter[1] === searchType)
return searchType;
return SearchTypes.Unknown;
}
return SearchTypes.Default;
};
const parseLinkElement = (st, link) => {
if (link.tagName === 'DIV') {
const parsedJSON = JSON.parse(link.innerHTML);
if (parsedJSON['ru'] == undefined) {
console.error('Link element has not ru.');
console.error(link);
return '';
}
return parsedJSON['ru'];
}
const rawHref = link.getAttribute('href');
if (rawHref === null) {
console.error('Link element has not href.');
console.error(link);
return '';
}
return parseURL(rawHref);
};
// Optimization: Use Set for blacklist checks for O(1) inclusion checks
const blacklistSet = new Set(site.blackList.list);
const linkProcess = (st, parentE) => {
const links = p.getLinkElems(st, parentE);
// Cache commonly used functions and values
const deleteCandidate = p.deleteCandidateByLinkElem;
const coloriseCandidate = p.coloriseCandidateByLinkElem;
for (const link of links) {
const url = parseLinkElement(st, link);
if (!url) continue;
const urlHostname = new URL(url).hostname;
// Optimization: Use Set for O(1) blacklist check
if (blacklistSet.has(urlHostname)) {
deleteCandidate(st, link);
log.blockedCount++;
continue;
}
// Original code to check against non-blacklisted categories
let isMatched = false;
for (const [category, siteType] of Object.entries(site)) {
if (siteType.isBlacklist) continue; // Skip blacklisted category
for (const domain of siteType.list) {
if (urlHostname === domain || urlHostname.endsWith('.' + domain)) {
coloriseCandidate(st, link, siteType.color);
log.trackedCount++;
isMatched = true;
break; // Break out of inner loop
}
}
if (isMatched) break; // Break out of outer loop
}
}
};
const currentST = parseSearchURL(location.href);
if (currentST === SearchTypes.Unknown) {
console.debug('Unknown search type');
return;
}
const p = getPageStyle();
if (!p.supportSearchTypes.includes(currentST)) {
console.debug('Unsupported search type in this platform.');
return;
}
const log = {
blockedCount: 0,
trackedCount: 0,
};
linkProcess(currentST, document);
p.writeLog(currentST, formatGSI4DLog(log));
p.after(currentST);
})();