Jump to content

User:Ingenuity/quickNavigate.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ingenuity (talk | contribs) at 01:31, 13 March 2022 (create script). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
let lastPressed = -1;

window.addEventListener("keydown", event => {
	if (event.key === "\\") {
		if (new Date().getTime() - lastPressed < 1000) {
			let input = document.createElement("input", { id: "searchInput" });
			document.body.appendChild(input);

			input.style.border = "none";
			input.style.outline = "none";
			input.style.background = "none";
			input.style.position = "fixed";
			input.style.top = "calc(100% - 100px)";
			input.style.left = "0px";
			input.style.width = "100%";
			input.style.textAlign = "center";
			input.style.zIndex = "5";
			input.style.fontSize = "3em";

			input.focus();
			input.onblur = function() {
				input.remove();
			}

			input.onkeydown = e => {
				if (e.key.toLowerCase() === "enter") {
					location.href = "https://en.wikipedia.org/wiki/" + input.value;
				}
			}

			input.oninput = e => {
				if (e.data === "\\") {
					input.value = "";
				}
			}
		} else {
			lastPressed = new Date().getTime();
		}
	}
});