-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (55 loc) · 2 KB
/
script.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
class PreviewPlugin {
#preview = null;
#abortController = null;
constructor() {
this.#preview = document.createElement('div');
this.#preview.classList.add('plugin-preview');
this.#preview.style.position = 'absolute';
this.#preview.style.top = '0';
this.#preview.style.left = '0';
this.#preview.style.display = 'none';
document.body.append(this.#preview);
}
attach() {
const selector = JSINFO.plugin.preview.selector;
const links = document.querySelectorAll(selector + ' a.wikilink1');
links.forEach(link => {
link.addEventListener('mouseenter', this.onMouseEnter.bind(this));
link.addEventListener('mouseleave', this.onMouseLeave.bind(this));
link.removeAttribute('title');
});
}
async loadPreview(id) {
try {
if (this.#abortController !== null) this.#abortController.abort();
this.#abortController = new AbortController();
const data = await fetch(
DOKU_BASE + 'lib/exe/ajax.php?call=plugin_preview&id=' + encodeURIComponent(id),
{
signal: this.#abortController.signal,
method: 'POST',
}
);
if (data.ok) {
this.#preview.innerHTML = await data.text();
this.#preview.style.display = 'block';
}
} catch (ignored) {
// we don't care about errors
}
}
async onMouseEnter(e) {
this.#preview.style.top = e.pageY + 10 + 'px';
this.#preview.style.left = e.pageX + 10 + 'px';
await this.loadPreview(e.target.dataset.wikiId);
}
onMouseLeave(e) {
this.#preview.style.display = 'none';
if (this.#abortController !== null) this.#abortController.abort();
this.#abortController = null;
}
}
document.addEventListener('DOMContentLoaded', () => {
const preview = new PreviewPlugin();
preview.attach();
});