Externalize license footer script to satisfy CSP

This commit is contained in:
Jennie Robinson Faber 2026-05-17 18:59:53 +01:00
parent 05082b6f01
commit acecd619e9
3 changed files with 87 additions and 2 deletions

76
theme/license-footer.js Normal file
View file

@ -0,0 +1,76 @@
(function () {
var URL_HREF = "https://creativecommons.org/licenses/by-sa/4.0/";
var PREFIX =
"Content on this site by Baby Ghosts Studio Development Fund is licensed under CC BY-SA 4.0. " +
"To view a copy of this license, visit ";
function isDoc() {
return location.pathname.indexOf("/doc/") === 0;
}
function findTarget() {
var pm = document.querySelector(".ProseMirror");
if (pm && pm.parentElement) return pm.parentElement;
return (
document.querySelector("article") ||
document.querySelector("[role=main]") ||
document.querySelector("main")
);
}
function render() {
var existing = document.querySelector(".cc-license-footer");
if (!isDoc()) {
if (existing) existing.remove();
return;
}
if (existing) return;
var target = findTarget();
if (!target) return;
var footer = document.createElement("footer");
footer.className = "cc-license-footer";
var p = document.createElement("p");
p.appendChild(document.createTextNode(PREFIX));
var a = document.createElement("a");
a.href = URL_HREF;
a.target = "_blank";
a.rel = "noopener noreferrer";
a.textContent = URL_HREF;
p.appendChild(a);
footer.appendChild(p);
target.appendChild(footer);
}
var pending = false;
function schedule() {
if (pending) return;
pending = true;
requestAnimationFrame(function () {
pending = false;
render();
});
}
["pushState", "replaceState"].forEach(function (m) {
var orig = history[m];
history[m] = function () {
orig.apply(this, arguments);
schedule();
};
});
window.addEventListener("popstate", schedule);
function start() {
new MutationObserver(schedule).observe(document.body, {
childList: true,
subtree: true,
});
schedule();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", start);
} else {
start();
}
})();