User:Ingenuity/preview.js

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 "Page has no content, or an error occured when attempting to fetch the page"; } }

function addCloseButton { previewElem.innerHTML += `[x] `; }

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 = " " + page.replaceAll("_", " ") + "history " + "Loading page content..." + " ";		previewElem.innerHTML = " " + page.replaceAll("_", " ") + "history " + (await getPageHTML(page)) + " "; 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 = " " + page.replaceAll("_", " ") + "history " + (await getPageHTML(page)) + " "; 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);	} });