This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
web.mjs
301 lines (281 loc) · 9.17 KB
/
web.mjs
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
/**
* notion-enhancer: api
* (c) 2021 dragonwocky <[email protected]> (https://dragonwocky.me/)
* (https://notion-enhancer.github.io/) under the MIT license
*/
'use strict';
/**
* helpers for manipulation of a webpage
* @namespace web
*/
import { fs } from './index.mjs';
let _hotkeyListenersActivated = false,
_hotkeyEventListeners = [],
_documentObserver,
_documentObserverListeners = [];
const _documentObserverEvents = [];
/**
* wait until a page is loaded and ready for modification
* @param {array=} selectors - wait for the existence of elements that match these css selectors
* @returns {Promise} a promise that will resolve when the page is ready
*/
export const whenReady = (selectors = []) => {
return new Promise((res, _rej) => {
const onLoad = () => {
const interval = setInterval(isReady, 100);
function isReady() {
const ready = selectors.every((selector) => document.querySelector(selector));
if (!ready) return;
clearInterval(interval);
res(true);
}
isReady();
};
if (document.readyState !== 'complete') {
document.addEventListener('readystatechange', (_event) => {
if (document.readyState === 'complete') onLoad();
});
} else onLoad();
});
};
/**
* parse the current location search params into a usable form
* @returns {Map<string, string>} a map of the url search params
*/
export const queryParams = () => new URLSearchParams(window.location.search);
/**
* replace special html characters with escaped versions
* @param {string} str
* @returns {string} escaped string
*/
export const escape = (str) =>
str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/'/g, ''')
.replace(/"/g, '"')
.replace(/\\/g, '\');
/**
* a tagged template processor for raw html:
* stringifies, minifies, and syntax highlights
* @example web.raw`<p>hello</p>`
* @returns {string} the processed html
*/
export const raw = (str, ...templates) => {
const html = str
.map(
(chunk) =>
chunk +
(['string', 'number'].includes(typeof templates[0])
? templates.shift()
: escape(JSON.stringify(templates.shift(), null, 2) ?? ''))
)
.join('');
return html.includes('<pre')
? html.trim()
: html
.split(/\n/)
.map((line) => line.trim())
.filter((line) => line.length)
.join(' ');
};
/**
* create a single html element inc. attributes and children from a string
* @example web.html`<p>hello</p>`
* @returns {Element} the constructed html element
*/
export const html = (str, ...templates) => {
const $fragment = document.createRange().createContextualFragment(raw(str, ...templates));
return $fragment.children.length === 1 ? $fragment.children[0] : $fragment.children;
};
/**
* appends a list of html elements to a parent
* @param $container - the parent element
* @param $elems - the elements to be appended
* @returns {Element} the updated $container
*/
export const render = ($container, ...$elems) => {
$elems = $elems
.map(($elem) => ($elem instanceof HTMLCollection ? [...$elem] : $elem))
.flat(Infinity)
.filter(($elem) => $elem);
$container.append(...$elems);
return $container;
};
/**
* removes all children from an element without deleting them/their behaviours
* @param $container - the parent element
* @returns {Element} the updated $container
*/
export const empty = ($container) => {
while ($container.firstChild && $container.removeChild($container.firstChild));
return $container;
};
/**
* loads/applies a css stylesheet to the page
* @param {string} path - a url or within-the-enhancer filepath
*/
export const loadStylesheet = (path) => {
const $stylesheet = html`<link
rel="stylesheet"
href="${path.startsWith('https://') ? path : fs.localPath(path)}"
/>`;
render(document.head, $stylesheet);
return $stylesheet;
};
/**
* copy text to the clipboard
* @param {string} str - the string to copy
* @returns {Promise<void>}
*/
export const copyToClipboard = async (str) => {
try {
await navigator.clipboard.writeText(str);
} catch {
const $el = document.createElement('textarea');
$el.value = str;
$el.setAttribute('readonly', '');
$el.style.position = 'absolute';
$el.style.left = '-9999px';
document.body.appendChild($el);
$el.select();
document.execCommand('copy');
document.body.removeChild($el);
}
};
/**
* read text from the clipboard
* @returns {Promise<string>}
*/
export const readFromClipboard = () => {
return navigator.clipboard.readText();
};
const triggerHotkeyListener = (event, hotkey) => {
const inInput = document.activeElement.nodeName === 'INPUT' && !hotkey.listenInInput;
if (inInput) return;
const modifiers = {
metaKey: ['meta', 'os', 'win', 'cmd', 'command'],
ctrlKey: ['ctrl', 'control'],
shiftKey: ['shift'],
altKey: ['alt'],
},
pressed = hotkey.keys.every((key) => {
key = key.toLowerCase();
for (const modifier in modifiers) {
const pressed = modifiers[modifier].includes(key) && event[modifier];
if (pressed) {
// mark modifier as part of hotkey
modifiers[modifier] = [];
return true;
}
}
if (key === 'space') key = ' ';
if (key === 'plus') key = '+';
if (key === event.key.toLowerCase()) return true;
});
if (!pressed) return;
// test for modifiers not in hotkey
// e.g. to differentiate ctrl+x from ctrl+shift+x
for (const modifier in modifiers) {
const modifierPressed = event[modifier],
modifierNotInHotkey = modifiers[modifier].length > 0;
if (modifierPressed && modifierNotInHotkey) return;
}
hotkey.callback(event);
};
/**
* register a hotkey listener to the page
* @param {array|string} keys - the combination of keys that will trigger the hotkey.
* key codes can be tested at http://keycode.info/ and are case-insensitive.
* available modifiers are 'alt', 'ctrl', 'meta', and 'shift'.
* can be provided as a + separated string.
* @param {function} callback - called whenever the keys are pressed
* @param {object=} opts - fine-tuned control over when the hotkey should be triggered
* @param {boolean=} opts.listenInInput - whether the hotkey callback should be triggered
* when an input is focused
* @param {boolean=} opts.keydown - whether to listen for the hotkey on keydown.
* by default, hotkeys are triggered by the keyup event.
*/
export const addHotkeyListener = (
keys,
callback,
{ listenInInput = false, keydown = false } = {}
) => {
if (typeof keys === 'string') keys = keys.split('+');
_hotkeyEventListeners.push({ keys, callback, listenInInput, keydown });
if (!_hotkeyListenersActivated) {
_hotkeyListenersActivated = true;
document.addEventListener('keyup', (event) => {
for (const hotkey of _hotkeyEventListeners.filter(({ keydown }) => !keydown)) {
triggerHotkeyListener(event, hotkey);
}
});
document.addEventListener('keydown', (event) => {
for (const hotkey of _hotkeyEventListeners.filter(({ keydown }) => keydown)) {
triggerHotkeyListener(event, hotkey);
}
});
}
};
/**
* remove a listener added with web.addHotkeyListener
* @param {function} callback
*/
export const removeHotkeyListener = (callback) => {
_hotkeyEventListeners = _hotkeyEventListeners.filter(
(listener) => listener.callback !== callback
);
};
/**
* add a listener to watch for changes to the dom
* @param {onDocumentObservedCallback} callback
* @param {string[]=} selectors
*/
export const addDocumentObserver = (callback, selectors = []) => {
if (!_documentObserver) {
const handle = (queue) => {
while (queue.length) {
const event = queue.shift(),
matchesAddedNode = ($node, selector) =>
$node instanceof Element &&
($node.matches(selector) ||
$node.matches(`${selector} *`) ||
$node.querySelector(selector)),
matchesTarget = (selector) =>
event.target.matches(selector) ||
event.target.matches(`${selector} *`) ||
[...event.addedNodes].some(($node) => matchesAddedNode($node, selector));
for (const listener of _documentObserverListeners) {
if (!listener.selectors.length || listener.selectors.some(matchesTarget)) {
listener.callback(event);
}
}
}
};
_documentObserver = new MutationObserver((list, _observer) => {
if (!_documentObserverEvents.length)
requestIdleCallback(() => handle(_documentObserverEvents));
_documentObserverEvents.push(...list);
});
_documentObserver.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
});
}
_documentObserverListeners.push({ callback, selectors });
};
/**
* remove a listener added with web.addDocumentObserver
* @param {onDocumentObservedCallback} callback
*/
export const removeDocumentObserver = (callback) => {
_documentObserverListeners = _documentObserverListeners.filter(
(listener) => listener.callback !== callback
);
};
/**
* @callback onDocumentObservedCallback
* @param {MutationRecord} event - the observed dom mutation event
*/