跳转到内容

User:PexEric/QuickNav.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/* https://en.wikipedia.org/w/index.php?title=User:Ingenuity/quickNavigate.js */

let lastPressed = -1;
const chineseColonMap = {
  ":": ":",
  ";": ";",
};

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") {
					let searchTerm = input.value;
					// 将中文冒号替换为半角冒号
					for (const [key, value] of Object.entries(chineseColonMap)) {
						searchTerm = searchTerm.replace(new RegExp(key, "g"), value);
					}
					location.href = "https://wikicn.playgoteam.workers.dev/wiki/" + searchTerm;
				}
			};

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