|
| 1 | +(() => { |
| 2 | + const status = document.createElement("div"); |
| 3 | + status.setAttribute("role", "status"); |
| 4 | + status.setAttribute("aria-live", "polite"); |
| 5 | + Object.assign(status.style, { |
| 6 | + background: "#ffffff", |
| 7 | + border: "1px solid #b7c0c7", |
| 8 | + bottom: "8px", |
| 9 | + color: "#172026", |
| 10 | + display: "none", |
| 11 | + font: "12px -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif", |
| 12 | + left: "8px", |
| 13 | + lineHeight: "1.35", |
| 14 | + maxWidth: "min(720px, calc(100vw - 16px))", |
| 15 | + overflowWrap: "anywhere", |
| 16 | + padding: "6px 8px", |
| 17 | + position: "fixed", |
| 18 | + zIndex: "9999", |
| 19 | + }); |
| 20 | + |
| 21 | + document.body.append(status); |
| 22 | + show("Loading presentation..."); |
| 23 | + |
| 24 | + function show(message) { |
| 25 | + status.textContent = message; |
| 26 | + status.style.display = "block"; |
| 27 | + } |
| 28 | + |
| 29 | + function hide() { |
| 30 | + status.style.display = "none"; |
| 31 | + } |
| 32 | + |
| 33 | + function formatAssetUrl(assetUrl) { |
| 34 | + try { |
| 35 | + const url = new URL(assetUrl, window.location.href); |
| 36 | + const currentPathParts = window.location.pathname.split("/"); |
| 37 | + currentPathParts.pop(); |
| 38 | + const currentDirectory = `${currentPathParts.join("/")}/`; |
| 39 | + const relativePath = url.pathname.startsWith(currentDirectory) |
| 40 | + ? url.pathname.slice(currentDirectory.length) |
| 41 | + : url.pathname; |
| 42 | + |
| 43 | + return decodeURI(`${relativePath}${url.search}${url.hash}`); |
| 44 | + } catch { |
| 45 | + return assetUrl; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + function getMediaUrl(element) { |
| 50 | + return element.currentSrc || element.src || element.getAttribute("src"); |
| 51 | + } |
| 52 | + |
| 53 | + function isMediaLoaded(element) { |
| 54 | + if (element instanceof HTMLImageElement) { |
| 55 | + return element.complete; |
| 56 | + } |
| 57 | + |
| 58 | + if (element instanceof HTMLVideoElement) { |
| 59 | + return element.readyState >= HTMLMediaElement.HAVE_METADATA; |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + window.brickSimLoadingStatus = { |
| 66 | + hide, |
| 67 | + show, |
| 68 | + trackMedia(root) { |
| 69 | + const pendingMedia = new Map(); |
| 70 | + |
| 71 | + function showNextPending() { |
| 72 | + const nextUrl = pendingMedia.values().next().value; |
| 73 | + |
| 74 | + if (nextUrl) { |
| 75 | + show(`Loading ${formatAssetUrl(nextUrl)}`); |
| 76 | + } else { |
| 77 | + hide(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + for (const element of root.querySelectorAll("img, video")) { |
| 82 | + const assetUrl = getMediaUrl(element); |
| 83 | + |
| 84 | + if (!assetUrl || isMediaLoaded(element)) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + const finish = () => { |
| 89 | + pendingMedia.delete(element); |
| 90 | + showNextPending(); |
| 91 | + }; |
| 92 | + |
| 93 | + pendingMedia.set(element, assetUrl); |
| 94 | + element.addEventListener("load", finish, { once: true }); |
| 95 | + element.addEventListener("loadedmetadata", finish, { once: true }); |
| 96 | + element.addEventListener("error", finish, { once: true }); |
| 97 | + } |
| 98 | + |
| 99 | + showNextPending(); |
| 100 | + }, |
| 101 | + }; |
| 102 | +})(); |
0 commit comments