MediaWiki:Common.js

From Portland Homeless Resources Wiki
Revision as of 21:49, 26 February 2026 by PHRWadmin (talk | contribs) (Created page with "Any JavaScript here will be loaded for all users on every page load.: // iOS map-link rewrite: Google Maps -> Apple Maps // Requires exported address links to be: <a class="oc-maplink" ... data-addr="...">...</a> (function () { function isIOS() { // Covers iPhone/iPad/iPod; modern iPadOS may identify as Mac, so include touch heuristic. const ua = navigator.userAgent || ""; const iOSUA = /iPhone|iPad|iPod/i.test(ua); const iPadOS13Plus = (navigator....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
// iOS map-link rewrite: Google Maps -> Apple Maps
// Requires exported address links to be: <a class="oc-maplink" ... data-addr="...">...</a>
(function () {
  function isIOS() {
    // Covers iPhone/iPad/iPod; modern iPadOS may identify as Mac, so include touch heuristic.
    const ua = navigator.userAgent || "";
    const iOSUA = /iPhone|iPad|iPod/i.test(ua);
    const iPadOS13Plus = (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1);
    return iOSUA || iPadOS13Plus;
  }

  function encodeAppleQuery(addr) {
    // Apple Maps accepts q=; encodeURIComponent is fine.
    return encodeURIComponent(addr);
  }

  function extractGoogleQuery(href) {
    try {
      const u = new URL(href, window.location.href);
      // https://www.google.com/maps/search/?api=1&query=...
      const q = u.searchParams.get("query");
      if (q) return q; // this is already URL-encoded typically
      // sometimes Google links use q=
      const q2 = u.searchParams.get("q");
      if (q2) return q2;
    } catch (e) {}
    return null;
  }

  function rewriteMapLinks() {
    if (!isIOS()) return;

    const links = document.querySelectorAll("a.oc-maplink");
    links.forEach(function (a) {
      // Prefer raw address from data-addr (best)
      const raw = a.getAttribute("data-addr");
      if (raw && raw.trim()) {
        a.href = "https://maps.apple.com/?q=" + encodeAppleQuery(raw.trim());
        return;
      }

      // Fallback: try to convert existing Google Maps href
      const gq = extractGoogleQuery(a.href);
      if (gq) {
        // gq might be encoded already; Apple will tolerate it, but decoding then encoding is cleaner.
        let decoded = gq;
        try { decoded = decodeURIComponent(gq.replace(/\+/g, "%20")); } catch (e) {}
        a.href = "https://maps.apple.com/?q=" + encodeAppleQuery(decoded);
      }
    });
  }

  // Run now + after MW loads content (covers some skins/extensions)
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", rewriteMapLinks);
  } else {
    rewriteMapLinks();
  }

  if (window.mw && mw.hook) {
    mw.hook("wikipage.content").add(rewriteMapLinks);
  }
})();