-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle_cache_redirect.user.js
75 lines (68 loc) · 2.13 KB
/
google_cache_redirect.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
// Google Cache Redirect Remover
// GNU General Public License
// ==UserScript==
// @name Google Search Redirect Changer Remover
// @namespace https://github.com/lilydjwg/userscripts
// @description Change or remove the Google Search redirect in cached links
// @include https://www.google.com/*
// @include https://encrypted.google.*/*
// @include https://www.google.*/*
// @version 1.0
// @grant none
// ==/UserScript==
const REDIRECT_VISITED_HTTPS = true
const REMOVE_SEARCH_LINKS = false
const APPEND_SEARCH_LINKS = true
const doit = function() {
if(REMOVE_SEARCH_LINKS) {
const links = document.querySelectorAll('h3.r > a')
for(let linkNode of links) {
try {
linkNode.removeAttribute('onmousedown')
} catch(e) {
console.error(e)
continue
}
}
}
if(APPEND_SEARCH_LINKS) {
const links = document.querySelectorAll('h3.r > a')
for(let linkNode of links) {
try {
const newLinkNode = document.createElement('a')
newLinkNode.href = linkNode.href
newLinkNode.className = 'fl'
newLinkNode.setAttribute('target', '_blank')
newLinkNode.textContent = '原始链接'
const urlNode = linkNode.parentNode.nextSibling.querySelector('cite')
console.log(urlNode)
urlNode.parentNode.appendChild(document.createTextNode(' - '))
urlNode.parentNode.appendChild(newLinkNode)
} catch(e) {
console.error(e)
continue
}
}
}
}
doit()
document.addEventListener('overflow', function(e){
const main = document.querySelector('h2.hd')
if(e.target == main){
doit()
}
})
//visited link indication fix
if(REDIRECT_VISITED_HTTPS){
document.addEventListener('mouseup', function(e){
let el = e.target
//find up to <a> because there will be <em> etc inside.
while(el.tagName != 'A' && el.parentNode){
el = el.parentNode
}
//major search results or related ones
if(el.parentNode && el.parentNode.className == 'r' || el.className == 'fl'){
el.href = el.href.replace('http://www.google.com/url', 'https://www.google.com/url')
}
})
}