User:Polygnotus/Scripts/Piped.js
Appearance
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 code will be executed when previewing this page.
This user script seems to have a documentation page at User:Polygnotus/Scripts/Piped.
// Add "Show piped links" and "Show unpiped links" to External tools on Special:WhatLinksHere
(function () {
"use strict";
// Only run on WhatLinksHere pages
if (mw.config.get("wgCanonicalSpecialPageName") !== "Whatlinkshere") {
return;
}
// Get the target page title from the subpage parameter
var targetPage = decodeURIComponent(
mw.config.get("wgPageName").split("/").slice(1).join("/")
).replace(/_/g, " ");
if (!targetPage) {
return;
}
// Add the links to External tools section
function addLinksSearchLinks() {
// Find the External tools list
var externalToolsList = document.querySelector(".hlist.inline ul");
if (!externalToolsList) return;
// Create unpiped links item
var unpipedItem = document.createElement("li");
var unpipedLink = document.createElement("a");
unpipedLink.href = getUnpipedLinksUrl(targetPage);
unpipedLink.textContent = "unpiped links";
unpipedLink.className = "external text";
unpipedItem.appendChild(unpipedLink);
externalToolsList.appendChild(unpipedItem);
// Create piped links item
var pipedItem = document.createElement("li");
var pipedLink = document.createElement("a");
pipedLink.href = getPipedLinksUrl(targetPage);
pipedLink.textContent = "piped links";
pipedLink.className = "external text";
pipedItem.appendChild(pipedLink);
externalToolsList.appendChild(pipedItem);
}
// Get URL for unpiped links search
function getUnpipedLinksUrl(targetPage) {
var escapedPage = mw.util.escapeRegExp(targetPage);
var searchQuery = `insource:"${targetPage}" insource:/\\[\\[${escapedPage}(#[^\\]|]*)?\\]\\]/i`;
return mw.util.getUrl("Special:Search", {
search: searchQuery,
profile: "advanced",
fulltext: 1,
ns0: 1,
});
}
// Get URL for piped links search
function getPipedLinksUrl(targetPage) {
var escapedPage = mw.util.escapeRegExp(targetPage);
var searchQuery = `insource:"${targetPage}" insource:/\\[\\[${escapedPage}\\|/i`;
return mw.util.getUrl("Special:Search", {
search: searchQuery,
profile: "advanced",
fulltext: 1,
ns0: 1,
});
}
// Wait for page to load
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", addLinksSearchLinks);
} else {
addLinksSearchLinks();
}
})();