-
Notifications
You must be signed in to change notification settings - Fork 3
/
google-go-to-cache.user.js
114 lines (93 loc) · 3.49 KB
/
google-go-to-cache.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
// ==UserScript==
// @name Google Search Go To Cache
// @namespace UserScript
// @version 0.2.0
// @description Show a tooltip with Google Cache link for external links in Google search results
// @author CY Fung
// @license MIT
// @match https://www.google.com/search*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const filterRule = '#search a[href]'
const forceHTTPS = false;
function isGoogleHost(hostname) {
if (hostname === 'www.google.com') return true;
if (hostname === 'google.com') return true;
if (hostname.endsWith('.google.com')) return true;
return false;
}
let tooltipTimeout;
const tooltip = document.createElement('div');
let tooltipInner = document.createElement('div');
tooltipInner.className = 'cache-tooltip-inner'
tooltip.className = 'cache-tooltip';
tooltipInner.textContent = 'Cache page';
tooltip.style.position = 'absolute';
tooltipInner.style.backgroundColor = 'black';
tooltipInner.style.color = 'white';
tooltipInner.style.padding = '5px';
tooltipInner.style.borderRadius = '5px';
tooltip.style.zIndex = '1000';
tooltip.style.cursor = 'pointer';
tooltip.style.display = 'none';
tooltipInner.style.display='inline-block'
tooltip.appendChild(tooltipInner);
tooltip.addEventListener('click', function() {
window.location.href = tooltip.dataset.cacheLink;
});
document.body.appendChild(tooltip);
document.addEventListener('mouseenter', function(event) {
if (event.target.tagName === 'A' && event.target.href && event.target.matches(filterRule)) {
const link = event.target.href;
const url = new URL(link);
if (!isGoogleHost(url.hostname)) {
clearTimeout(tooltipTimeout);
showTooltip(event.target);
}
}
}, true);
document.addEventListener('mouseleave', function(event) {
if (event.target.tagName === 'A' && event.target.href) {
hideTooltipWithDelay();
}
}, true);
tooltip.addEventListener('mouseleave', function() {
hideTooltipWithDelay();
});
tooltip.addEventListener('mouseenter', function() {
clearTimeout(tooltipTimeout);
});
let cssAdded = false;
function showTooltip(element) {
if(!cssAdded){
cssAdded = true;
document.head.appendChild(document.createElement('style')).textContent = `
.cache-tooltip{
opacity: 0.75;
user-select: none;
z-index: 999;
}
.cache-tooltip:hover {
opacity: 1;
}
`
}
let link = element.href;
if(forceHTTPS) link = link.replace(/^http\:\/\//, 'https://');
const cacheLink = `https://webcache.googleusercontent.com/search?q=cache:${encodeURIComponent(link)}`;
tooltip.dataset.cacheLink = cacheLink;
const rect = element.getBoundingClientRect();
tooltip.style.top = `${rect.top + window.scrollY + rect.height}px`;
tooltip.style.left = `${rect.left + window.scrollX}px`;
tooltip.style.display = 'block';
tooltip.style.width = `${rect.width}px`;
}
function hideTooltipWithDelay() {
clearTimeout(tooltipTimeout);
tooltipTimeout = setTimeout(() => {
tooltip.style.display = 'none';
}, 160); // Delay before hiding the tooltip
}
})();