-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_page.js
50 lines (44 loc) · 1.66 KB
/
random_page.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
var observer = new MutationObserver(function (e) {
// Check for existing link
if (document.getElementById("BottomNavWebPart_RandomPage")) return;
// Create new link
var link = document.createElement("a");
link.textContent = "Random Page";
link.classList.add("BottomNavLnk");
link.setAttribute("id", "BottomNavWebPart_RandomPage");
link.setAttribute("href", "#");
link.setAttribute("role", "button");
link.addEventListener("click", async function (e) {
e.preventDefault();
// Add loading icon
var loader = document.createElement("div");
loader.classList.add("ShellLayout_WaitIcon");
var loaderParent = document.getElementById("MainContent");
var contentDiv = loaderParent.childNodes[0]
loaderParent.insertBefore(loader, contentDiv);
try {
// Load sitemap and extract links
var response = await fetch("https://www.mcmaster.com/sitemap.xml", {
mode: 'same-origin',
referrerPolicy: 'origin',
});
var content = await response.text();
var links = [...content.matchAll(/<loc>(.*)<\/loc>/g)].map(m => m[1]);
// Choose random link
var link = links[Math.floor(Math.random() * links.length)];
window.location = link;
} finally {
// Remove loading icon
loader.remove();
}
});
// Create seperator
var separator = document.createElement("span");
separator.textContent = "|";
separator.classList.add("VerticalSeprt");
//Add link to bottom nav
var container = document.getElementById("BottomNavWebPart_LnksCntnr");
container.appendChild(separator);
container.appendChild(link);
});
observer.observe(document.body, { childList: true, subtree: true });