Files
hudra/page-detection.js
2026-07-26 16:02:28 +10:00

123 lines
3.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(() => {
if (window.__hudraPageDetector) return;
const contentRootSelectors = [
"#content .user_content",
".show-content .user_content",
".wiki-page-content .user_content",
".user_content",
"[role='main'] .page-content",
"[role='main'] .content"
];
const titleSelectors = [
"h1.page-title",
"h1.wiki-page-title",
"#content h1"
];
function hasMeaningfulContent(element) {
return Boolean(
element?.textContent?.trim() ||
element?.querySelector("img, video, iframe, svg, canvas, picture, table")
);
}
function resolve() {
let contentRoot = null;
let contentRootSelector = null;
for (const selector of contentRootSelectors) {
const candidate = document.querySelector(selector);
if (candidate) {
contentRoot = candidate;
contentRootSelector = selector;
break;
}
}
if (!contentRoot) {
return { supported: false, reason: "NO_LESSON_CONTAINER" };
}
const pageTitleElement =
contentRoot.querySelector("h1") ||
document.querySelector(titleSelectors.join(", "));
if (!pageTitleElement) {
return { supported: false, reason: "NO_LESSON_TITLE" };
}
if (!hasMeaningfulContent(contentRoot)) {
return { supported: false, reason: "EMPTY_LESSON" };
}
return {
supported: true,
reason: null,
contentRoot,
contentRootSelector,
pageTitleElement
};
}
function inspect() {
const result = resolve();
return {
supported: result.supported,
reason: result.reason,
contentRootSelector: result.contentRootSelector || null
};
}
function showNotice(message) {
document.getElementById("hudra-page-notice")?.remove();
const notice = document.createElement("div");
notice.id = "hudra-page-notice";
notice.setAttribute("role", "status");
notice.setAttribute("aria-live", "polite");
notice.style.cssText = [
"position:fixed",
"z-index:2147483647",
"right:20px",
"bottom:20px",
"max-width:420px",
"box-sizing:border-box",
"padding:16px 48px 16px 18px",
"border:1px solid #bac4d2",
"border-radius:10px",
"background:#fff",
"color:#1f2937",
"box-shadow:0 8px 28px rgba(15,23,42,.2)",
"font:16px/1.4 system-ui,sans-serif"
].join(";");
const text = document.createElement("span");
text.textContent = message;
notice.appendChild(text);
const close = document.createElement("button");
close.type = "button";
close.setAttribute("aria-label", "Dismiss Hudra message");
close.textContent = "×";
close.style.cssText = [
"position:absolute",
"top:8px",
"right:10px",
"border:0",
"background:transparent",
"color:#374151",
"font:24px/1 system-ui,sans-serif",
"cursor:pointer"
].join(";");
close.addEventListener("click", () => notice.remove());
notice.appendChild(close);
document.body.appendChild(notice);
window.setTimeout(() => notice.remove(), 8000);
}
window.__hudraPageDetector = { inspect, resolve, showNotice };
})();