This repository was archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeek.js
More file actions
211 lines (190 loc) · 5.35 KB
/
Copy pathpeek.js
File metadata and controls
211 lines (190 loc) · 5.35 KB
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
let HEIGHT = 600
let WIDTH = 500
const HOVERPEEK_ID = 'hoverpeek';
const width = window.innerWidth;
const height = window.innerHeight;
const DEBUG_MODE = false
let peek = false
let fetching = null
if (document.readyState == 'loading') {
document.addEventListener('DOMContentLoaded', getSettings);
} else {
getSettings();
}
// UTILS
class Logger {
static fail(obj) {
if (DEBUG_MODE)
console.error(obj)
}
static log(obj) {
if (DEBUG_MODE)
console.log(obj)
}
static debug(obj) {
if (DEBUG_MODE)
console.debug(obj)
}
}
function getSettings() {
browser.storage.local.get(['height', 'width']).then(settings => {
Logger.debug({
settings
})
hoverPeek.height = settings?.height ?? HEIGHT;
hoverPeek.width = settings?.width ?? WIDTH;
});
}
function strip(dom) {
const strippedText = dom.body.textContent || "";
return strippedText.replace(/(\r\n|\n|\r)/gm, "");
}
function killHoverPeek() {
const hoverPeek = document.getElementById(HOVERPEEK_ID)
hoverPeek.style.display = 'none'
abortControllers.forEach(controller => controller.abort())
}
function debounce(func, wait, immediate = false) {
let timeout;
return function () {
let context = this, args = arguments;
let later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
let callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function getAnchorTag(event) {
let anchorTag = null
if (event?.target?.parentNode?.parentNode?.tagName.toLowerCase() === 'a') {
anchorTag = event.target.parentNode.parentNode;
}
if (event?.target?.parentNode?.tagName.toLowerCase() === 'a') {
anchorTag = event.target.parentNode;
}
if (event?.target?.tagName.toLowerCase() === 'a') {
anchorTag = event.target;
}
return anchorTag;
}
function getPageAndCache(anchorTag) {
if (anchorTag === null || anchorTag === undefined)
return
const url = anchorTag.href;
if (cache.has(anchorTag)) {
Logger.log({
url: anchorTag?.href,
cacheHit: strip(cache.get(anchorTag))
})
killHoverPeek()
return cache.get(anchorTag)
}
Logger.log({
fetch: url
})
// before pushing to abort queue, abort other requests
const controller = new AbortController();
abortControllers.forEach(controller => controller.abort())
abortControllers = []
abortControllers.push(controller)
fetching = url
fetchingIndicator.innerHTML = `<div>Fetching</div> <div>${fetching}</div>...`
hoverPeek.srcdoc = new XMLSerializer().serializeToString(fetchingIndicator);
fetch(url, {
signal: controller.signal,
mode: 'no-cors',
headers: {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1'
}
})
.then((response) => {
return response.text();
})
.then((html) => {
let doc = encodeURIComponent(parser.parseFromString(html, "text/html"));
Logger.log({
url,
response: doc
})
cache.set(anchorTag, doc)
hoverPeek.srcdoc = new XMLSerializer().serializeToString(doc);
})
.catch((err) => {
Logger.fail({
url,
error: err,
message: err.message
})
}).finally(() => {
fetching = null
});
}
// GLOBALS
let abortControllers = []
const cache = new WeakMap()
const parser = new DOMParser();
const hoverPeek = document.createElement('iframe')
hoverPeek.id = HOVERPEEK_ID
hoverPeek.style.position = 'fixed'
hoverPeek.style.background = 'white'
hoverPeek.style.zIndex = 2147483646
hoverPeek.style.border = '2px solid black'
hoverPeek.style.borderRadius = '0.5rem'
hoverPeek.style.display = 'none'
const fetchingIndicator = document.createElement('p');
fetchingIndicator.style.display = 'flex'
fetchingIndicator.style.flexDirection = 'column'
document?.body?.prepend(hoverPeek)
// Dont destroy peek if user hovers back in 250ms
document.addEventListener('keydown', (event) => {
if (event.key === 'Shift') {
Logger.debug("SHIFT DOWN")
peek = true
}
setTimeout(() => {
peek = false
}, 3000)
})
document.addEventListener('keyup', (event) => {
if (event.key === 'Shift') {
Logger.debug("SHIFT UP")
peek = false
}
})
document.addEventListener('mouseover', debounce((event) => {
if (!peek)
return
const anchorTag = getAnchorTag(event);
if (anchorTag) {
const { clientX: x, clientY: y } = event;
const scrollLeft = document.documentElement.scrollLeft;
const scrollTop = document.documentElement.scrollTop;
let leftAnchor = x + scrollLeft;
let topAnchor = y + scrollTop;
// Clamp the frame in viewport
let previewBottom = parseInt(y, 10) + parseInt(hoverPeek.height, 10);
if (previewBottom > height) {
const extra = previewBottom - height
topAnchor -= extra
}
const previewRight = parseInt(x, 10) + parseInt(hoverPeek.width, 10);
const extra = previewRight - width
leftAnchor -= extra
hoverPeek.style.top = topAnchor - 16 + 'px'
hoverPeek.style.left = leftAnchor - 16 + 'px'
if (anchorTag?.href) {
hoverPeek.style.display = 'block'
}
}
}, 50));
// prefetch on hover
document.addEventListener('mouseover', (event) => {
if (!peek)
return
const anchorTag = getAnchorTag(event);
getPageAndCache(anchorTag);
})