-
Notifications
You must be signed in to change notification settings - Fork 166
/
Google Images direct link.user.js
266 lines (239 loc) · 8.37 KB
/
Google Images direct link.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
// ==UserScript==
// @name Google Images direct link
// @namespace http://userscripts.org/users/lorentz
// @description Add direct link to images and pages in google image search
// @include http*://images.google.*/images*
// @include http*://www.google.*/images*
// @include http*://www.google.*/webhp*
// @include http*://www.google.*/search?*
// @include http*://www.google.*/imgres*
// @include http*://images.google.*/search?*
// @include https://encrypted.google.com/search?*
// @version 5.2
// @grant none
// @icon http://s3.amazonaws.com/uso_ss/icon/78355/large.png
// @updateURL https://userscripts.org/scripts/source/78355.meta.js
// @downloadURL https://userscripts.org/scripts/source/78355.user.js
// @note similar search image support AutoPager.
// ==/UserScript==
/**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var isUndefined = function(val) {
return ((typeof val) === 'undefined');
}
var decode = function(component) {
if (isUndefined(component))
return false;
return decodeURIComponent(component);
}
var doubleDecode = function(component) {
if (isUndefined(component))
return false;
var tmp = decodeURIComponent(component);
tmp = decodeURIComponent(tmp);
return tmp;
}
var parseUrl = function(url) {
var pos = url.indexOf('?');
if (pos < 0)
return [];
var qparms = url.substring(pos + 1);
var rawparams = qparms.split('&');
var par = [];
for (var i = 0; i < rawparams.length; i++) {
var p = rawparams[i].split("=");
par[p[0]] = p[1];
}
return par;
}
var getImageLinks = function(url) {
var param = parseUrl(url);
var links = new Object();
links.toImgHref = decode(param["imgurl"]);
links.toPageHref = decode(param["url"]);
return links;
}
var getNewImageLinks = function(url) {
var param = parseUrl(url);
var links = new Object();
links.toImgHref = doubleDecode(param["imgurl"]);
links.toPageHref = decode(param["imgrefurl"]);
return links;
}
var firstOrNull = function(elems) {
return (elems.length > 0) ? elems[0] : null;
}
var imgTable = firstOrNull(document.getElementsByClassName('images_table'));
if (imgTable) { // for basic version
var imgCell = imgTable.getElementsByTagName('td');
for (j = 0; j < imgCell.length; j++) {
var imageAnchor = imgCell[j].getElementsByTagName('a')[0];
var domainText = imgCell[j].getElementsByTagName('cite')[0];
console.log(imageAnchor.href);
var links = getImageLinks(imageAnchor.href);
//links.toPageHref = imageAnchor.href; // TODO fixme
links.toImgHref = imageAnchor.href; // TODO fixme
domainText.innerHTML = '<a href="' + links.toPageHref + '">' + domainText.innerHTML + '/…<\a>';
imageAnchor.href = links.toImgHref;
}
} else { // standard version
console.log("standard version");
var stopEvent = function(event) {
event.stopPropagation()
}
var fixStyle = function(target) {
var parent = target.parentNode;
parent.style.height = target.style.height;
parent.style.width = target.style.width;
parent.style.left = target.style.left;
target.style.left = 'auto';
}
var fixBoxObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var target = mutation.target;
var parent = mutation.target.parentNode;
if (mutation.attributeName === 'style' && target.style.left !== 'auto') {
fixStyle(target);
}
});
});
var fixBoxMutationConfig = {
attributes: true,
childList: true,
characterData: false,
subtree: false
};
var fixImageBox = function(image) {
if (/\blinkOk\b/.test(image.className)) {
return;
}
var span = image.querySelector('span.rg_ilmn');
if (span !== null) {
var a = firstOrNull(image.getElementsByTagName('a'));
var links = getNewImageLinks(a.href);
a.href = links.toImgHref;
a.addEventListener('click', stopEvent, false);
a.addEventListener('mousedown', stopEvent, false);
var newContainer = document.createElement('div');
newContainer.className = 'newCont';
a.parentNode.appendChild(newContainer);
newContainer.appendChild(a);
newContainer.appendChild(span.parentNode);
fixStyle(a);
var desc = span.innerHTML;
span.innerHTML = '<a style="color:#fff" href="' + links.toPageHref + '">' + desc + '</a>';
image.className += ' linkOk'
fixBoxObserver.observe(a, fixBoxMutationConfig);
} else {
console.log("incomplete span");
image.className += ' notComplete';
}
}
var fixImages = function() {
var imagesContainer = document.getElementById('rg_s');
if (imagesContainer == null) return;
var images = imagesContainer.getElementsByClassName('rg_di');
for (var i = 0; i < images.length; i++) {
fixImageBox(images[i]);
}
}
var newBoxMutationConfig = {
attributes: false,
childList: true,
characterData: false,
subtree: true
};
var newBoxObserver = new MutationObserver(function(mutations) {
var needFix = false;
mutations.forEach(function(mutation) {
needFix = needFix || mutation.target.id == 'rg_s';
});
if (needFix)
fixImages();
});
fixImages();
newBoxObserver.observe(document.body, newBoxMutationConfig);
var css = [];
var i = 0;
css[i++] = '.newCont { position: relative; }';
css[i++] = '.newCont .rg_ilmbg { display: none; }';
css[i++] = '.newCont:hover .rg_ilmbg { display: block; }';
css[i++] = '.imgSiteLnk {'; //img preview
css[i++] = ' background-color: rgba(255, 255, 255, 0.77);';
css[i++] = ' bottom: 0;';
css[i++] = ' color: #000000;';
css[i++] = ' display: block;';
css[i++] = ' line-height: normal;';
css[i++] = ' position: absolute;';
css[i++] = ' text-decoration: none;';
css[i++] = ' width: 100%; ';
css[i++] = ' display: none }';
css[i++] = '.imgPrev:hover .imgSiteLnk { display: block }'; //img preview
var style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(css.join('\n')));
document.head.appendChild(style);
//img preview in google search (only links to page)
var fixImagePreview = function(div) {
var images = document.getElementsByClassName('bicc');
console.log('img preview in google search ' + images.length);
for (var i = 0; i < images.length; i++) {
var div = images[i];
var el = div.getElementsByTagName('a');
if (el.length == 1) {
div.className += ' imgPrev';
//div.style.border = '4em solid black';
var href = el[0].href;
var link = doubleDecode(parseUrl(href)['imgil']);
if (link === false) continue;
link = decode(link.split(';')[5]);
var a = document.createElement('a');
a.href = link;
a.className = 'imgSiteLnk';
a.textContent = link.split('/')[2];
div.appendChild(a);
}
}
}
var searchObserver = new MutationObserver(function(mutations) {
fixImagePreview();
fixSimilars();
});
searchObserver.observe(document.body, {
attributes: false,
childList: true,
characterData: false,
subtree: true
});
function fixSimilars() {
// visually similar search img preview (oly links to image)
var similars = document.querySelectorAll('div#ires div.th a');
console.log('visually similar search ' + similars.length)
for (var i = 0; i < similars.length; i++) {
var a = similars[i];
var href = getNewImageLinks(a.href);
//console.log(a.href,href);
if (href.toImgHref === false) {
continue;
}
var newA = document.createElement('a');
newA.href = href.toImgHref;
newA.appendChild(a.firstChild);
a.parentNode.replaceChild(newA, a);
}
}
fixSimilars();
console.log('end standard version');
} //end standard version