User:Chlod/Scripts/GoToTitle.js

// GoToTitle
// Author: Chlod
// Version: 1.0.1-REL

// Navigate to any page using an input box that opens when you click on the page title.
// Non-intrusive to work with other title-modifying userscripts.

(function() {
	
	var overlayId = "goto_titleOverlay";
	
	function getTitleInput(parentContainer) {
		var titleInput = document.createElement("input");
		titleInput.style.padding = "0";
		titleInput.style.width = "100%";
		titleInput.style.height = "100%";
		titleInput.style.border = "0";
		titleInput.style.fontSize = "inherit";
		titleInput.style.fontFamily = "inherit";
		
		titleInput.addEventListener("keydown", function(kdEvent) {
			if (Array.isArray(window.GoToTitleHooks))
				window.GoToTitleHooks.forEach(f => f(kdEvent));
			if (kdEvent.key === "Escape")
				parentContainer.parentElement.removeChild(parentContainer);
			else if (kdEvent.key === "Enter")
				window.location.href = mw.util.getUrl(titleInput.value);
		});
		
		// Because there's no inbuilt value for plain title with namespace.
		var namespace = mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber")];
		titleInput.value = (namespace.length > 0 ? namespace + ":" : "") + mw.config.get("wgTitle");
		
		return titleInput;
	}
	
	document.body.addEventListener("click", function(event) {
		var currentParent = event.target;
		
		// Traverse click event and check if it hits the title heading.
		do {
			if (currentParent == null)
				// Whoops, we went too far!
				return;
			if (currentParent.id === overlayId) return;
			if (
				// Standard heading
				currentParent.id === "firstHeading"
				// No overlay existing
				&& document.getElementById(overlayId) === null
			) {
				var relativeContainer = document.createElement("div");
				var absoluteContainer = document.createElement("div");
				var titleHeight = currentParent.getBoundingClientRect().height;
				
				relativeContainer.id = overlayId;
				relativeContainer.style.position = "relative";
				relativeContainer.style.top = "-" + (+(titleHeight + 1)) + "px";
				relativeContainer.style.width = "100%";
				
				absoluteContainer.style.position = "absolute";
				absoluteContainer.style.width = "100%";
				absoluteContainer.style.height = titleHeight + "px";
				absoluteContainer.style.top = absoluteContainer.style.left = "0";
				
				relativeContainer.style.fontSize = 
					absoluteContainer.style.fontSize = "inherit";
				relativeContainer.style.fontFamily = 
					absoluteContainer.style.fontFamily = "inherit";
				
				var titleInput = getTitleInput(relativeContainer);
				absoluteContainer.appendChild(titleInput);
				relativeContainer.appendChild(absoluteContainer);
				currentParent.appendChild(relativeContainer);
				
				titleInput.focus();
				
				return;
			} else if (
				// Vector 2022 floating header
				currentParent.classList.contains("vector-sticky-header-context-bar-primary")
				// No overlay existing
				&& document.getElementById(overlayId) === null
			) {
				var titleHeight = currentParent.getBoundingClientRect().height;
				currentParent.style.position = "relative";
				
				var absoluteContainer = document.createElement("div");
				absoluteContainer.id = overlayId;
				absoluteContainer.style.position = "absolute";
				absoluteContainer.style.width = "100%";
				absoluteContainer.style.height = titleHeight + "px";
				absoluteContainer.style.top = absoluteContainer.style.left = "0";
				absoluteContainer.style.minHeight = "1.2em";
				
				var titleInput = getTitleInput(absoluteContainer);
				absoluteContainer.appendChild(titleInput);
				currentParent.appendChild(absoluteContainer);
				
				titleInput.focus();
				
				return;
			}
		} while ((currentParent = currentParent.parentElement) !== document.body);
		
		// Did not hit event heading. Close if not found.
		var overlayElement = document.getElementById(overlayId);
		if (overlayElement != null) {
			overlayElement.parentElement.removeChild(overlayElement);
		}
		
	});
	
})();

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.