-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
60 lines (53 loc) · 1.43 KB
/
util.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
/**
* Load document
*
* @param {string} url
*/
export async function loadDocument(url) {
const r = await fetch(url);
const txt = await r.text();
const parser = new DOMParser();
const doc = parser.parseFromString(txt, "text/html");
return doc;
}
/**
* Fetch items from page and
* merge into existing list
*
* @param {Document} doc
* @param {string} CSS_GALLERY Selector for node of interest
*/
export function loadPage(doc, CSS_GALLERY) {
const pics = doc.querySelector(CSS_GALLERY);
if(!pics) return; // nothing to add (reached the end)
const G = document.querySelector(CSS_GALLERY);
const P = G.parentNode;
// hide and do work
const sib = G.previousSibling;
P.removeChild(G);
const newImages = Array.from(pics.querySelectorAll('li'))
.map(e => e.parentNode.removeChild(e));
newImages.forEach(e => G.appendChild(e));
// show
P.insertBefore(G, sib);
mw.notify(`Loaded ${newImages.length} items`);
}
/**
* Bump parameter with given name to the end of the URL.
*
* @param {string} name parameter name
*/
export const bumpParam = (name) => {
const params = new URLSearchParams(location.search.substring(1));
let lastKey;
for (const [key] of params.entries()) {
lastKey = key;
}
const Q = params.get(name);
if (Q && lastKey !==name) { // place query at the end (ezier editing)
params.delete(name);
params.set(name, Q);
const nextHref = location.pathname + '?'+ params.toString();
location.href = nextHref;
}
};