User:Ingenuity/preview.js - Wikipedia


Article Images

Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.

const mwapi = new mw.Api();
let previewElem = null;

async function getPageHTML(page) {
	try {
		return (await mwapi.get({
			action: "parse",
			page: page,
			prop: "text",
			formatversion: 2
		})).parse.text;
	} catch (err) {
		return "<i>Page has no content, or an error occured when attempting to fetch the page</i>";
	}
}

function addCloseButton() {
	previewElem.innerHTML += `<div style="position:absolute;left:calc(100% - 50px);top:0;padding:10px;cursor:pointer;user-select:none;" onclick="closePreview()">[x]</div>`;
}

function closePreview() {
	previewElem.remove();
	previewElem = null;
	const content = document.getElementById("content");
	content.style.width = "initial";
	content.style.height = "initial";
	content.style.overflowY = "initial";
}

async function showPagePreview(page) {
	if (previewElem === null) {
		const content = document.getElementById("content");
		content.style.width = "calc(50% - 145px)";
		content.style.height = "calc(100% - 80px)";
		content.style.overflowY = "auto";
		previewElem = document.createElement("div");
		previewElem.style.width = "calc(50% - 84px)";
		previewElem.style.overflowX = "auto";
		previewElem.style.position = "absolute";
		previewElem.style.top = "80px";
		previewElem.style.left = "calc(50% + 84px)";
		previewElem.style.padding = "20px";
		previewElem.style.paddingTop = "0";
		previewElem.style.boxSizing = "border-box";
		previewElem.style.background = "white";
		previewElem.style.borderLeft = "1px solid #ccc";
		document.body.appendChild(previewElem);
		previewElem.innerHTML = "<div class='vector-body'><h1 style='margin-top:0'>" + page.replaceAll("_", " ") + "<a style='font-size:0.7em;margin-left:20px;' href='/w/index.php?title=" + page + "&action=history'>history</a></h1>" + "Loading page content..." + "</div>";
		previewElem.innerHTML = "<div class='vector-body'><h1 style='margin-top:0'>" + page.replaceAll("_", " ") + "<a style='font-size:0.7em;margin-left:20px;' href='/w/index.php?title=" + page + "&action=history'>history</a></h1>" + (await getPageHTML(page)) + "</div>";
		previewElem.style.height = Math.max(Math.min(content.clientHeight, window.innerHeight - 80), 400) + "px";
		previewElem.style.overflowY = "auto";
		addCloseButton();

		for (let elem of Array.prototype.slice.call(previewElem.querySelectorAll(".mw-editsection"))) {
			elem.remove();
		}

		for (let elem of Array.prototype.slice.call(previewElem.querySelectorAll(".toc"))) {
			elem.remove();
		}
	} else {
		previewElem.innerHTML = "<div class='vector-body'><h1 style='margin-top:0'>" + page.replaceAll("_", " ") + "<a style='font-size:0.7em;margin-left:20px;' href='/w/index.php?title=" + page + "&action=history'>history</a></h1>" + (await getPageHTML(page)) + "</div>";
		addCloseButton();
	}
}

window.addEventListener("click", (event) => {
	if (event.target.tagName.toLowerCase() === "a" && event.shiftKey) {
		event.preventDefault();
		const pageName = decodeURIComponent(event.target.href.split("/wiki/")[1]);
		showPagePreview(pageName);
	}
});