MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
Tag: Manual revert
No edit summary
Line 1,295: Line 1,295:
     jQuery("#show-article").removeClass("print-opts-open");
     jQuery("#show-article").removeClass("print-opts-open");
   }
   }
  (function () {
    try {
      console.log("[swprint] probe on load", {
        printButton: !!document.getElementById("print-button"),
        chooserExists: !!document.getElementById("print-chooser"),
        localPrintOnlyCount: jQuery(".print-only").length,
        showArticleExists: !!document.getElementById("show-article"),
      });
    } catch (e) {}
  })();


   /* core: build iframe and print */
   /* core: build iframe and print */
   function swBuildIframeAndPrint(printHtml, borderPref, $btn) {
   function swBuildIframeAndPrint(printHtml, borderPref, $btn) {
    console.log("[swprint] buildIframeAndPrint()", {
      borderPref: borderPref,
      htmlLen: printHtml ? printHtml.length : 0,
    });
     // iframe
     // iframe
     var iframe = document.createElement("iframe");
     var iframe = document.createElement("iframe");
Line 1,350: Line 1,365:
     doc.body.innerHTML = printHtml;
     doc.body.innerHTML = printHtml;


     // border preference -> <html> class
     // --- sanitize: kill any inner .print-no-border (when user chose WITH border) ---
    (function () {
      var stray = doc.querySelectorAll(".print-no-border");
      if (borderPref === "with" && stray.length) {
        Array.prototype.forEach.call(stray, function (el) {
          el.className = el.className
            .replace(/\bprint-no-border\b/g, "")
            .trim();
        });
      }
    })();
 
     (function () {
     (function () {
       var htmlEl = doc.documentElement;
       var htmlEl = doc.documentElement;
Line 1,368: Line 1,394:
           );
           );
       }
       }
      console.log(
        "[swprint] html class after apply:",
        htmlEl.className || "<empty>"
      );
    })();
    // Enforce via CSS (covers cases where wrapper class differs)
    (function () {
      var css =
        borderPref === "without"
          ? "@media print{.entry-wrapper,.entry-wrapper.print-a4-narrow{border:0!important;box-shadow:none!important;}}"
          : "@media print{.entry-wrapper,.entry-wrapper.print-a4-narrow{border:1px solid #000!important;}}";
      var style = doc.createElement("style");
      style.appendChild(doc.createTextNode(css));
      doc.head.appendChild(style);
      console.log("[swprint] CSS enforcement injected for:", borderPref);
    })();
    // FINAL INLINE FALLBACK: force the visible wrapper’s border directly
    (function () {
      // try common wrappers; fall back to first element in body
      var wrapper =
        doc.querySelector(".entry-wrapper") ||
        doc.querySelector(".print-a4-narrow") ||
        doc.body.firstElementChild;
      if (!wrapper) {
        console.warn("[swprint] no wrapper found for inline border fallback");
        return;
      }
      var old = wrapper.getAttribute("style") || "";
      var force =
        borderPref === "without"
          ? "border:0!important;"
          : "border:1px solid #000!important;";
      wrapper.setAttribute("style", force + old);
      console.log(
        "[swprint] inline border fallback applied on:",
        wrapper.className || wrapper.tagName,
        "pref:",
        borderPref
      );
     })();
     })();


Line 1,558: Line 1,628:
   /* decide source & kick print */
   /* decide source & kick print */
   function swHandlePrintChoice(id, $btn) {
   function swHandlePrintChoice(id, $btn) {
    try {
      console.log("[swprint] swHandlePrintChoice START", {
        id: id,
        btn:
          $btn && $btn[0] && $btn[0].outerHTML
            ? $btn[0].outerHTML.slice(0, 80) + "…"
            : "<none>",
        hasLocalPrintOnly: jQuery(".print-only").length,
      });
    } catch (e) {}
     if ($btn && $btn.data("busy")) return;
     if ($btn && $btn.data("busy")) return;
     if ($btn && $btn.length) $btn.data("busy", true);
     if ($btn && $btn.length) $btn.data("busy", true);
Line 1,568: Line 1,648:
     if (localPrintOnly.length) {
     if (localPrintOnly.length) {
       swHidePrintUI();
       swHidePrintUI();
      console.log("[swprint] using LOCAL .print-only (Entry page)");
       swBuildIframeAndPrint(localPrintOnly.prop("outerHTML"), borderPref, $btn);
       swBuildIframeAndPrint(localPrintOnly.prop("outerHTML"), borderPref, $btn);
       return;
       return;
Line 1,631: Line 1,712:
     }
     }
   );
   );
  // --- extra safety bindings for Entry page / widgets ---
  // 1) Directly bind the anchors (works even if container delegation misses)
  jQuery(document).on(
    "click.swprintChoice",
    "a#print-with-border, a#print-no-border, #print-with-border, #print-no-border",
    function (e) {
      console.log("[print] direct anchor handler fired:", this.id);
      e.preventDefault();
      swHandlePrintChoice(this.id, jQuery(this));
    }
  );
  // 2) If using the Button widget (<a id=...><button>...</button></a>),
  //    capture clicks on the <button> and route via its parent <a>.
  jQuery(document).on(
    "click.swprintChoiceBtn",
    "#print-options button",
    function (e) {
      var $a = jQuery(this).closest("a[id]");
      if (!$a.length) return;
      console.log(
        "[print] widget button handler fired; parent anchor id:",
        $a.attr("id")
      );
      e.preventDefault();
      swHandlePrintChoice($a.attr("id"), $a);
    }
  );
  // ------ HARD CATCHER (capture phase) ------
  // If some widget stops propagation or the click lands on a nested element,
  // this capture listener still sees it and routes to swHandlePrintChoice.
  (function () {
    if (window.__swprintHardCatcherInstalled) return;
    window.__swprintHardCatcherInstalled = true;
    document.addEventListener(
      "click",
      function (ev) {
        try {
          var target = ev.target;
          var a = target && target.closest ? target.closest("a") : null;
          var id = a && a.getAttribute ? a.getAttribute("id") : "";
          // Debug what we clicked
          // console.log("[swprint] CAPTURE", { tag: target && target.tagName, id: id });
          if (id === "print-with-border" || id === "print-no-border") {
            // prevent default once here; we'll handle printing
            ev.preventDefault();
            // Use jQuery wrapper if present, otherwise fall back to null
            var $a = (window.jQuery && jQuery(a)) || null;
            console.log("[swprint] CAPTURE routed to swHandlePrintChoice:", id);
            swHandlePrintChoice(id, $a);
          }
        } catch (e) {
          // swallow
        }
      },
      true
    ); // <-- capture phase
  })();


   // also hide choices on ESC; your close-button handler already hides them
   // also hide choices on ESC; your close-button handler already hides them

Navigation menu