-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBetterGoogle.js
301 lines (261 loc) · 9.07 KB
/
BetterGoogle.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
// ==UserScript==
// @name Better Google by X-Force
// @version 0.2.3
// @namespace http://www.iplaysoft.com/
// @description Keep Google Search Box on Top
// @include *//www.google.*/*
// @require http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.3.min.js
// @downloadURL https://raw.githubusercontent.com/xtremforce/UserScripts/master/BetterGoogle.js
// @updateURL https://raw.githubusercontent.com/xtremforce/UserScripts/master/BetterGoogle.js
// ==/UserScript==
if(window.location.href.indexOf("q=")==-1){return;}
//============================
// float Google Search box
//============================
var webSearchBoxId = "#tsf";
var imageSearchBoxId="#gb";//"#tphdr";
var myWindow = $(window);
$(document).keypress(function(e){
if (e.shiftKey && e.which==63) { //Shift+?(问号)
if(document.activeElement.id!="lst-ib" && document.activeElement.id!="gbqfq"){
$("#lst-ib,#gbqfq").focus(function(){e.preventDefault();}).select();
}
}
});
if($(webSearchBoxId).length>0){
var webSearchBoxTop = $(webSearchBoxId).position().top;
myWindow.scroll(function(event) {
scrollTop = myWindow.scrollTop();
if(scrollTop > webSearchBoxTop){
if($(webSearchBoxId).css("position")!="fixed"){
$(webSearchBoxId).css({
"position": "fixed",
"top": "0",
"z-index":99999,
"height":"40px",
"width":"100%",
"padding-top":"10px",
"background-color": "#f5f5f5",
"border-bottom": "1px solid #ddd"
});
}
}else{
$(webSearchBoxId).removeAttr("style");
}
});
}
if($(imageSearchBoxId).length>0){
var imageSearchBoxTop = $(imageSearchBoxId).position().top;
myWindow.scroll(function(event) {
scrollTop = myWindow.scrollTop();
if(scrollTop > imageSearchBoxTop){
if($(imageSearchBoxId).css("position")!="fixed"){
$(imageSearchBoxId).css({
"position": "fixed",
"top": "0",
"z-index":99999,
"border-bottom": "1px solid #ddd"
});
}
}else{
$(imageSearchBoxId).removeAttr("style");
}
});
}
//==========================
// Remove Google redirect
//==========================
if(window.location.href.indexOf("tbm=isch")>0){
//要等待列表加载完成才能移除
waitForKeyElements("#irc_cc", rmGoogleImageRedirect);
}else{
waitForKeyElements("#ires", rmGoogleRedirect);
}
function rmGoogleRedirect() {
//网页搜索
console.debug('rmGoogleRedirect');
$('h3.r a').unbind('mousedown').removeAttr('onmousedown');
//links.href=links.attr('data-href');
$('#resultStats').append('<a style="color:#6DA70F" target="_blank" href="http://www.iplaysoft.com">转向已移除</a>');
}
function rmGoogleImageRedirect(){
console.debug('rmGoogleImageRedirect');
//图片搜索
$('table.irc_but_r tr td a,div.irc_mic div a,div._LAd a.irc_tas').removeAttr('jsaction');
$('.irc_sbl').append(' <a style="color:#6DA70F;text-decoration:none" target="_blank" href="http://www.iplaysoft.com">转向已移除</a>');
}
//以前的方法
//==========================
// Remove Google redirect
//==========================
//source: http://userscripts.org/scripts/source/183690.user.js
//name: Fuck Google
function RemoveRedirect() {
var results;
//for google web results
results = document.getElementsByClassName("r");
for (var i = 0; i < results.length; ++i) {
var link = results[i].getElementsByTagName("a")[0];
if (link == undefined) {
continue;
}
link.removeAttribute("onmousedown");
link.target = "_blank";
link.rel = "external";
console.log("remove One!");
var reg = new RegExp('/url\\?q=(.*)&sa=U');
var real = reg.exec(link.href);
if (real != null) {
link.href = real[1];
link.target = "_blank";
link.rel = "external";
//console.log("find:", link.href);
}
}
//for google image
/*
results = document.getElementsByClassName("rg_l");
for (var i = 0; i < results.length; ++i) {
var link = results[i];
var reg = new RegExp("imgrefurl=(.*)&docid");
var real = reg.exec(link.href);
if (real != null) {
link.href = real[1];
//console.log("img:", link.href);
}
}
*/
results = document.getElementsByClassName("irc_ifr");
var mo = new MutationObserver(setImagesRealLinkMulti);
for (var i = 0; i < results.length; ++i) {
//results[i].addEventListener('DOMAttrModified', setImagesRealLink, false);
mo.observe(results[i], {'attributes': true,'attributeOldValue': true});
}
}
function setImagesRealLink(a){
var reg=new RegExp('&url=(.*)&ei=');
var match=reg.exec(a.href);
if(match){
var real=match[1];
if(real){
a.href=decodeURIComponent(real);
}
}
}
function setImagesRealLinkMulti() {
var ifms = document.getElementsByClassName("irc_ifr");
for (var i = 0; i < ifms.length; ++i) {
var ifm = ifms[i].contentWindow;
var a = ifm.document.getElementById("irc_mil");
if (a) {
setImagesRealLink(a);
//这里试试 Wrap 个透明的A覆盖它!!!!!
a.setAttribute("onclick",
"(function(){\
alert('这里试试 Wrap 个透明的A覆盖它!!!!! ');\
var reg=new RegExp('&url=(.*)&ei=');\
var match=reg.exec(this.href);\
if(match){\
var real=match[1];\
if(real){\
this.href=decodeURIComponent(real);\
}\
}\
})();"
);
}
}
var fsl = document.getElementsByClassName("irc_fsl");
for (var i = 0; i < fsl.length; ++i) {
if (fsl[i]) {
setImagesRealLink(fsl[i]);
fsl[i].setAttribute("onclick",
"(function(){\
var reg=new RegExp('&url=(.*)&ei=');\
var match=reg.exec(this.href);\
if(match){\
var real=match[1];\
if(real){\
this.href=decodeURIComponent(real);\
}\
}\
})();"
);
}
}
}
//by xforce
//已经有插件使用了
//waitForKeyElements("#ires", RemoveRedirect);
/*--- waitForKeyElements(): A handy, utility function that
* does what it says.
*/
function waitForKeyElements(
selectorTxt,
/* Required: The jQuery selector string that
specifies the desired element(s).
*/
actionFunction,
/* Required: The code to run when elements are
found. It is passed a jNode to the matched
element.
*/
bWaitOnce,
/* Optional: If false, will continue to scan for
new elements even after the first match is
found.
*/
iframeSelector
/* Optional: If set, identifies the iframe to
search.
*/
) {
var targetNodes, btargetsFound;
if (typeof iframeSelector == "undefined")
targetNodes = $(selectorTxt);
else
targetNodes = $(iframeSelector).contents()
.find(selectorTxt);
if (targetNodes && targetNodes.length > 0) {
/*--- Found target node(s). Go through each and act if they
* are new.
*/
targetNodes.each(function() {
var jThis = $(this);
var alreadyFound = jThis.data('alreadyFound') || false;
if (!alreadyFound) {
//--- Call the payload function.
actionFunction(jThis);
jThis.data('alreadyFound', true);
}
});
btargetsFound = true;
} else {
btargetsFound = false;
}
//--- Get the timer-control variable for this selector.
var controlObj = waitForKeyElements.controlObj || {};
var controlKey = selectorTxt.replace(/[^\w]/g, "_");
var timeControl = controlObj[controlKey];
//--- Now set or clear the timer as appropriate.
if (btargetsFound && bWaitOnce && timeControl) {
//--- The only condition where we need to clear the timer.
clearInterval(timeControl);
delete controlObj[controlKey]
} else {
//--- Set a timer, if needed.
if (!timeControl) {
timeControl = setInterval(function() {
waitForKeyElements(selectorTxt,
actionFunction,
bWaitOnce,
iframeSelector
);
},
500
);
controlObj[controlKey] = timeControl;
}
}
waitForKeyElements.controlObj = controlObj;
}