-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.js
More file actions
413 lines (355 loc) · 12.1 KB
/
utils.js
File metadata and controls
413 lines (355 loc) · 12.1 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Check if the 'browser' namespace is available (Firefox) and use it,
// otherwise fall back to the 'chrome' namespace (Chrome).
export const runtime = typeof browser !== 'undefined' ? browser.runtime : chrome.runtime;
/**
* @param {string} url URL of image to check
* @returns {Promise<number>} The score of the image
*/
export async function getImageScore(url) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img.naturalWidth * img.naturalHeight);
img.onerror = () => resolve(0); // fallback score if image fails to load
img.src = url;
});
}
export function logMarian(message, object = null) {
if (!object) {
console.log(`[👩🏻🏫 Marian] ${message}`);
} else {
console.group(`[👩🏻🏫 Marian] ${message}`);
console.log(object);
console.groupEnd();
}
}
/**
* Returns a promise waiting a timeout in milliseconds
*
* @param {number} ms - time to wait in milliseconds
* @returns {Promise<None>}
*/
export function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Race a promise against a timeout, resolving with a fallback value if the timeout finishes first.
*
* @template T
* @param {Promise<T>} promise Promise to await.
* @param {number} ms Timeout length in milliseconds.
* @param {T} fallback Value to resolve with if the timeout elapses first.
* @returns {Promise<T>} Promise that settles with either the original result or the fallback.
*/
export function withTimeout(promise, ms, fallback) {
let timer;
return Promise.race([
promise.finally(() => clearTimeout(timer)),
new Promise(resolve => {
timer = setTimeout(() => resolve(fallback), ms);
})
]);
}
/**
* Extract text from HTML element while preserving paragraph breaks
*
* @param {HTMLElement} element HTML element to extract text from
* @returns {string} Formatted text with preserved paragraph breaks
*/
export function getFormattedText(element) {
if (!element) return "";
let result = '';
function processNode(/**@type {HTMLElement}*/node) {
if (node.nodeType === Node.TEXT_NODE) {
// Add text content, normalizing whitespace
result += node.textContent.replace(/\s+/g, ' ');
} else if (node.nodeType === Node.ELEMENT_NODE) {
const tagName = node.tagName.toLowerCase();
// Process child nodes first
for (const child of node.childNodes) {
processNode(child);
}
// Add appropriate line breaks after processing content
if (tagName === 'p') {
result += '\n\n'; // Double newline after paragraphs
} else if (tagName === 'br') {
result += '\n'; // Single newline for <br>
} else if (['div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li'].includes(tagName)) {
result += '\n'; // Single newline for other block elements
}
}
}
processNode(element);
result = result
.replace(/[ \t]+/g, ' ') // Multiple spaces/tabs to single space
.replace(/\n /g, '\n') // Remove spaces after newlines
.replace(/ \n/g, '\n') // Remove spaces before newlines
.replace(/\n{3,}/g, '\n\n') // Max 2 consecutive newlines
.trim();
return result;
// return cleanText(result);
}
const deepQueryCache = new Map();
/**
* Clear cached deep query results, typically once per extraction run.
*/
export function clearDeepQueryCache() {
deepQueryCache.clear();
}
/**
* Perform a deep DOM query that includes shadow roots under the provided hosts.
*
* @param {string} selector CSS selector to match.
* @param {string[]} [hostSelectors=[]] CSS selectors whose shadow roots should be traversed.
* @returns {Element[]} Unique matching elements discovered across light and shadow DOM.
*/
export function queryAllDeep(selector, hostSelectors = []) {
const cacheKey = `${selector}::${hostSelectors.join(',')}`;
if (deepQueryCache.has(cacheKey)) {
return deepQueryCache.get(cacheKey);
}
const results = new Set();
document.querySelectorAll(selector).forEach(el => results.add(el));
const stack = [];
if (hostSelectors.length) {
document.querySelectorAll(hostSelectors.join(',')).forEach(host => {
stack.push(host);
if (host.shadowRoot) stack.push(host.shadowRoot);
});
}
const visited = new Set();
while (stack.length) {
const root = stack.pop();
if (!root || visited.has(root)) continue;
visited.add(root);
if (typeof root.querySelectorAll !== 'function') continue;
root.querySelectorAll(selector).forEach(el => results.add(el));
root.querySelectorAll('*').forEach(node => {
if (node.shadowRoot) stack.push(node.shadowRoot);
});
}
const arr = Array.from(results);
deepQueryCache.set(cacheKey, arr);
return arr;
}
/**
* Variant of queryAllDeep that returns only the first match.
*
* @param {string} selector CSS selector to match.
* @param {string[]} [hostSelectors=[]] CSS selectors whose shadow roots should be traversed.
* @returns {Element | null} First matching element, or null when none found.
*/
export function queryDeep(selector, hostSelectors = []) {
const matches = queryAllDeep(selector, hostSelectors);
return matches[0] || null;
}
/**
* takes a cover URL, or a list of covers and returns the details of the one with the best score
*
* @typedef {{img: string, imgScore: number}} CoverObject image URL and image score
* @param {string | string[] | undefined} covers the cover URL, or a list of cover URLs
* @return {Promise<CoverObject>} the image URL and score of the best cover
*/
export async function getCoverData(covers) {
if (covers && covers.length === 0) {
covers = undefined;
}
if (!Array.isArray(covers)) {
// single URL was passed
const coverUrl = covers;
return {
img: coverUrl,
imgScore: coverUrl ? await getImageScore(coverUrl) : 0
}
}
// an array of images was passed in
const coversSettled = await Promise.allSettled(covers.map(getCoverData));
const coversObj = coversSettled
.filter(({ status }) => status === "fulfilled")
.map((res) => /**@type{CoverObject}*/(res.value));
// get the best one
const highestScoreCover = coversObj.reduce((highest, current) =>
current.imgScore > highest.imgScore ? current : highest
);
// logMarian("covers", { covers, coversObj, highestScoreCover });
return highestScoreCover;
}
/**
* Returns a new object with keys renamed according to the mapping.
* Keys not in the mapping are preserved as-is.
*
* setting the target name of the key to undefined will delete it from the new object
*
* @param {Record<string, string|undefined>} mapping - Map of old keys to new keys
* @param {Record<string,any>} object - Source object
* @returns {Record<string, any>} New object with mapped keys
*/
export function remapKeys(mapping, object) {
const newObj = {};
for (const key of Object.keys(object)) {
// If the key exists in mapping, use the new name, otherwise keep original
const finalKey = Object.hasOwn(mapping, key) ? mapping[key] : key;
if (finalKey == undefined) continue;
newObj[finalKey] = object[key];
}
return newObj;
}
/**
* Normalizes Description fields from various formats into a plain string.
* @param {object} description
* @returns {string} Normalized description text
*/
export function normalizeDescription(description) {
if (typeof description === "string") return description;
if (Array.isArray(description)) return normalizeDescription(description[0]);
if (description && typeof description === "object") {
if (typeof description.value === "string") return description.value;
if (typeof description.text === "string") return description.text;
}
return "";
}
/**
* @typedef {{name: string, roles: string[]}} contributor
*/
/**
* adds a contributor with one or more roles to the contributor list
*
* @param {contributor[]} contributors - list of contributors
* @param {string} name - name of the contributor
* @param {string | string[]} roles - a role or list of roles to add to a contributor
* @returns {contributor[]}
*/
export function addContributor(contributors, name, roles) {
if (!Array.isArray(roles)) {
roles = [roles];
}
const contributor = contributors.findIndex((contributor) => contributor.name === name);
if (contributor == -1) {
contributors.push({ name, roles: roles });
return contributors;
}
for (const role of roles) {
if (contributors[contributor].roles.includes(role)) continue;
contributors[contributor].roles.push(role);
}
return contributors;
}
/**
* @typedef {{[name: string]: string[]}} mappings
*/
/**
* adds a mapping with one or more IDs
*
* @param {mappings} mappings
* @param {string} name
* @param {string | string[]}
*
* @returns {mappings}
*/
export function addMapping(mappings, name, ids) {
if (!Array.isArray(ids)) {
ids = [ids];
}
let map = mappings[name] ?? [];
for (const id of ids) {
if (!map.includes(id)) map.push(id);
}
mappings[name] = map;
return mappings;
}
/**
* Normalize arbitrary text by stripping invisible characters and squeezing whitespace.
*
* @param {string | null | undefined} text Raw text content to sanitize.
* @returns {string} Sanitized text with normalized spacing.
*/
export function cleanText(text) {
if (!text || text == null) return '';
return text
.normalize('NFKC') // Normalize Unicode to one style
.replace(/\p{Cf}/gu, '') // Remove Unicode control chars
.replace(/[\u200E\u200F\u202A-\u202E\u00A0\uFEFF]/g, ' ') // Normalize invisible formatting chars (NBSP, BOM, Bidi marks) to spaces (should not be needed but keeping for consistency)
.replace(/^\s*,+\s*/, '') // Remove artifact leading commas/spaces (e.g. ", value")
.replace(/\s+/g, ' ') // Collapse all recurring whitespace (tabs, newlines) into a single space
.trim();
}
/**
* Normalizes raw format string to one of: Audiobook, Ebook, or Physical Book.
* @param {string} rawFormat
* @returns {string} 'Audiobook' | 'Ebook' | 'Physical Book'
*/
export function normalizeReadingFormat(rawFormat) {
if (!rawFormat) return 'Physical Book';
const format = rawFormat.toLowerCase();
if (
format.includes("audio") ||
format.includes("audible") ||
format.includes("narrat") ||
format.includes("mp3") ||
format.includes("cd")
) return "Audiobook";
if (
format.includes("web") ||
format.includes("nook") ||
format.includes("ebook") ||
format.includes("e-book") ||
format.includes("digital") ||
format.includes("kindle")
) {
return "Ebook";
}
if (
format.includes("physical") ||
format.includes("hardcover") ||
format.includes("paperback") ||
format.includes("book")
) {
return "Physical Book";
}
return "Physical Book"; // Fallback
}
/**
* collect a list of objects or promises of objects into a single new object
* overrides keys based on order of objects in list
*
* @typedef {Record<string,any>} obj a object
* @param {Array<Promise<obj>|obj>|obj|Promise<obj>} items item or items
*
* @returns {Promise<obj>}
*/
export async function collectObject(items) {
if (!Array.isArray(items)) return await items;
const objList = await Promise.all(items);
let obj = {};
for (let i = 0; i < objList.length; i += 1) {
const elm = objList[i];
if (elm == undefined) {
logMarian(`WARN: element number ${i} is ${elm}`)
continue;
}
Object.entries(elm)
.forEach(([k, v]) => obj[k] = v);
}
return obj;
}
/**
* preforms a http request and returns a dom
*
* @param {string} url
* @param {RequestInit?} args
* @returns {Promise<DOMParser|undefined>}
*/
export async function fetchHTML(url, args = undefined) {
try {
const response = await fetch(url, args);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const html = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
return doc;
} catch (error) {
console.error('Error fetching HTML:', error);
}
}
export { StorageBackedSet } from "./StorageSet.js";