🇮🇷 Iran Proxy | https://www.wikipedia.org/wiki/User:Polygnotus/Scripts/Piped.js
Jump to content

User:Polygnotus/Scripts/Piped.js

From Wikipedia, the free encyclopedia
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.
// 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();
  }
})();