Difference between revisions of "MediaWiki:Timeless.js"
From Medivia Online Wiki
| Line 1: | Line 1: | ||
(function () { | (function () { | ||
function normalizeRoot(root) { | |||
// Page Forms / mw.hook sometimes passes non-DOM objects. | |||
if (root && (root.nodeType === 1 || root.nodeType === 9) && typeof root.querySelector === "function") { | |||
return root; // Element (1) or Document (9) | |||
} | |||
return document; | |||
} | |||
function normalizeToFileTitle(val) { | function normalizeToFileTitle(val) { | ||
if (!val) return ""; | if (!val) return ""; | ||
val = val.trim(); | val = ("" + val).trim(); | ||
if (!val) return ""; | if (!val) return ""; | ||
// Allow | // Allow pasting [[File:...|...]] | ||
if (val.startsWith("[[")) val = val.replace(/^\[\[\s*/, "").replace(/\s*\]\]$/, ""); | if (val.startsWith("[[")) val = val.replace(/^\[\[\s*/, "").replace(/\s*\]\]$/, ""); | ||
if (val.includes("|")) val = val.split("|")[0]; | if (val.includes("|")) val = val.split("|")[0]; | ||
if (!/^File:/i.test(val)) val = "File:" + val; | if (!/^File:/i.test(val)) val = "File:" + val; | ||
return val; | return val; | ||
} | } | ||
function fileTitleToThumbUrl(fileTitle) { | function fileTitleToThumbUrl(fileTitle) { | ||
var name = fileTitle.replace(/^File:/i, ""); | var name = fileTitle.replace(/^File:/i, ""); | ||
return mw.util.getUrl("Special:Redirect/file/" + name) + "?width=64"; | return mw.util.getUrl("Special:Redirect/file/" + name) + "?width=64"; | ||
| Line 22: | Line 27: | ||
function hookItemImagePreview(root) { | function hookItemImagePreview(root) { | ||
root = root | root = normalizeRoot(root); | ||
var input = root.querySelector("input.pf-item-image"); | var input = root.querySelector("input.pf-item-image"); | ||
if (!input) { | if (!input) { | ||
input = | input = | ||
| Line 68: | Line 70: | ||
function run(root) { | function run(root) { | ||
setTimeout(function () { hookItemImagePreview(root); }, 150); | setTimeout(function () { hookItemImagePreview(root); }, 150); | ||
} | } | ||
Revision as of 20:17, 17 January 2026
(function () {
function normalizeRoot(root) {
// Page Forms / mw.hook sometimes passes non-DOM objects.
if (root && (root.nodeType === 1 || root.nodeType === 9) && typeof root.querySelector === "function") {
return root; // Element (1) or Document (9)
}
return document;
}
function normalizeToFileTitle(val) {
if (!val) return "";
val = ("" + val).trim();
if (!val) return "";
// Allow pasting [[File:...|...]]
if (val.startsWith("[[")) val = val.replace(/^\[\[\s*/, "").replace(/\s*\]\]$/, "");
if (val.includes("|")) val = val.split("|")[0];
if (!/^File:/i.test(val)) val = "File:" + val;
return val;
}
function fileTitleToThumbUrl(fileTitle) {
var name = fileTitle.replace(/^File:/i, "");
return mw.util.getUrl("Special:Redirect/file/" + name) + "?width=64";
}
function hookItemImagePreview(root) {
root = normalizeRoot(root);
var input = root.querySelector("input.pf-item-image");
if (!input) {
input =
root.querySelector('input[name="image"]') ||
root.querySelector('input[name$="[image]"]');
}
var img = document.getElementById("pf-image-preview");
var txt = document.getElementById("pf-image-preview-text");
if (!input || !img || !txt) return;
function setEmpty(msgHtml) {
img.style.display = "none";
img.removeAttribute("src");
txt.style.display = "";
txt.innerHTML = msgHtml;
}
function update() {
var fileTitle = normalizeToFileTitle(input.value);
if (!fileTitle) return setEmpty("<i>No image selected.</i>");
var url = fileTitleToThumbUrl(fileTitle);
img.onload = function () {
img.style.display = "";
txt.style.display = "none";
};
img.onerror = function () {
setEmpty("<i>Image not found.</i>");
};
img.src = url;
}
input.addEventListener("input", update);
input.addEventListener("change", update);
update();
}
function run(root) {
setTimeout(function () { hookItemImagePreview(root); }, 150);
}
if (window.mw && mw.hook) {
mw.hook("wikipage.content").add(run);
}
document.addEventListener("DOMContentLoaded", function () { run(document); });
})();