User:Chlod/Scripts/GoToTitle.js
Appearance
< User:Chlod | Scripts
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. |
![]() | This user script seems to have a documentation page at User:Chlod/Scripts/GoToTitle. |
// GoToTitle
// Author: Chlod
// Version: 1.0.0-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";
document.body.addEventListener("click", function(event) {
var currentParent = event.target;
// Traverse click event and check if it hits the title heading.
do {
if (currentParent.id === overlayId) return;
if (currentParent.id === "firstHeading" && document.getElementById(overlayId) === null) {
var relativeContainer = document.createElement("div");
var absoluteContainer = document.createElement("div");
var titleInput = document.createElement("input");
var titleHeight = currentParent.clientHeight;
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";
titleInput.style.padding = "0";
titleInput.style.width = titleInput.style.height = "100%";
titleInput.style.border = "0";
relativeContainer.style.fontSize =
absoluteContainer.style.fontSize =
titleInput.style.fontSize = "inherit";
relativeContainer.style.fontFamily =
absoluteContainer.style.fontFamily =
titleInput.style.fontFamily = "inherit";
titleInput.addEventListener("keydown", function(event) {
if (event.key === "Escape")
relativeContainer.parentElement.removeChild(relativeContainer);
else if (event.key === "Enter")
window.location.href = mw.config.get("wgArticlePath")
.replace('$1', mw.util.wikiUrlencode(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");
absoluteContainer.appendChild(titleInput);
relativeContainer.appendChild(absoluteContainer);
currentParent.appendChild(relativeContainer);
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);
}
});
})();