-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathuserscript.js
154 lines (137 loc) · 6.22 KB
/
userscript.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
// ==UserScript==
// @name Better Google
// @namespace google
// @version 0.1.16.9
// @description Restore google search results to older style with green link below title instead of link above title. Just tweaks the CSS and does some dynamic JS reordering of the DIVs.
// @author aligo, adambh, tejaslodaya, drwonky, yut23
// @license MIT
// @homepageURL https://github.com/aligo/better-google
// @match https://*.google.com/search?*
// @include /^https?://(?:www|encrypted|ipv[46])\.google\.[^/]+/(?:$|[#?]|search|webhp)/
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
var betterGoogleRow = function(el) {
var tbwUpd = el.querySelectorAll('.TbwUpd, .HGLrXd');
if (tbwUpd.length > 0) {
/* Google does A/B testing on the search results page, so the
* structure of the page is not always the same. This code
* tries to find the link element in a few different ways.
* If it can't find it, it just gives up and doesn't do
* anything.
*/
var selectors = [
'.yuRUbf > a',
'.yuRUbf > div > a',
'.yuRUbf > div > span > a',
];
for (const selector of selectors) {
var linkEl = el.querySelector(selector);
if (linkEl) {
break;
}
}
var addEl = linkEl.nextSibling;
if (!addEl) {
// try the parent's sibling, for the span case
addEl = linkEl.parentElement.nextSibling;
}
var betterAddEl = document.createElement('div');
betterAddEl.className = 'btrAdd';
if (addEl) {
// this loop moves the "More options" button into betterAddEl
for (var i = 0; i < addEl.children.length; i++) {
var _el = addEl.children[i];
if (_el.className.includes('TbwUpd') || _el.className.includes('HGLrXd')) {
continue;
}
betterAddEl.appendChild(_el);
}
} else {
// entry isn't fully loaded yet
betterAddEl.remove();
return;
}
var betterEl = document.createElement('div');
betterEl.className = 'btrG';
betterEl.appendChild(betterAddEl);
el.appendChild(betterEl);
var urlEl = document.createElement('a');
urlEl.href = linkEl.href;
urlEl.target = '_blank';
urlEl.className = 'btrLink';
var urlCiteEl = document.createElement('cite');
urlCiteEl.innerText = linkEl.href;
urlCiteEl.className = 'iUh30 bc';
urlEl.appendChild(urlCiteEl);
var maxWidth = el.clientWidth - betterAddEl.offsetWidth - 10;
betterEl.insertBefore(urlEl, betterAddEl);
if (urlEl.offsetWidth > maxWidth) {
urlEl.style.width = maxWidth.toString() + 'px';
}
var aboutResult = el.querySelectorAll('.csDOgf');
if (aboutResult.length > 0) {
betterEl.appendChild(aboutResult[0]);
}
tbwUpd.forEach(function(el) { el.remove() });
linkEl.querySelector('br:first-child').remove();
}
}
var prevResultCount = 0;
var bettered = false;
var runBetterGoogle = function() {
if (prevResultCount != document.querySelectorAll('.g .yuRUbf').length) {
document.querySelectorAll('.g .yuRUbf').forEach(betterGoogleRow);
prevResultCount = document.querySelectorAll('.g .yuRUbf').length;
}
if ( !bettered ) {
if ( MutationObserver != undefined ) {
var searchEl = document.getElementById('rcnt');
var observer = new MutationObserver(runBetterGoogle);
observer.observe(searchEl, {childList: true, subtree: true});
}
bettered = true;
}
};
var prepareStyleSheet = function() {
// if dark mode is enabled (either manually or by device default),
// Google adds a meta tag to the document which we can check
var link_color = '#006621';
var meta_color_scheme = document.querySelector('meta[name="color-scheme"]');
if (meta_color_scheme != undefined && meta_color_scheme.content.includes('dark')) {
// use a lighter green in dark mode
link_color = '#40965b';
}
var style = document.createElement('style');
style.setAttribute('media', 'screen');
style.appendChild(document.createTextNode(''));
document.head.appendChild(style);
style.sheet.insertRule(`:root { --btrG-link-color: ${link_color}; }`);
style.sheet.insertRule('.btrG { word-break: normal; line-height: 18px; }');
style.sheet.insertRule('.btrG .btrAdd { display: inline-block; vertical-align: top; line-height: 0; }');
style.sheet.insertRule('.btrG .btrLink { display: inline-block; vertical-align: top; line-height: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-decoration: none !important; color: var(--btrG-link-color); }');
style.sheet.insertRule('.btrG .btrLink cite.iUh30 { color: var(--btrG-link-color); font-size: 16px; }');
// remove extra space used for new multiline link info card
style.sheet.insertRule('.yuRUbf h3.DKV0Md { margin-top: 0px; }');
};
var checkElementThenRun = function(selector, func) {
var el = document.querySelector(selector);
if ( el == null ) {
if (window.requestAnimationFrame != undefined) {
window.requestAnimationFrame(function(){ checkElementThenRun(selector, func)});
} else {
document.addEventListener('readystatechange', function(e) {
if (document.readyState == 'complete') {
func();
}
});
}
} else {
func();
}
}
checkElementThenRun('head', prepareStyleSheet);
checkElementThenRun('#rcnt', runBetterGoogle);
})();