MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
Tag: Reverted
No edit summary
Tags: Manual revert Reverted
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,364: Line 1,349:
     // inject HTML
     // inject HTML
     doc.body.innerHTML = printHtml;
     doc.body.innerHTML = printHtml;
    // --- sanitize: kill any inner .print-no-border (when user chose WITH border) ---
    (function () {
      var stray = doc.querySelectorAll(".print-no-border");
      console.log(
        "[swprint] stray .print-no-border count BEFORE:",
        stray.length
      );
      if (borderPref === "with" && stray.length) {
        Array.prototype.forEach.call(stray, function (el) {
          el.className = el.className
            .replace(/\bprint-no-border\b/g, "")
            .trim();
        });
        console.log("[swprint] stray .print-no-border removed");
      }
      console.log(
        "[swprint] stray .print-no-border count AFTER:",
        doc.querySelectorAll(".print-no-border").length
      );
    })();


     // border preference -> <html> class
     // border preference -> <html> class
Line 1,406: Line 1,368:
           );
           );
       }
       }
      console.log(
        "[swprint] html class after apply:",
        htmlEl.className || "<empty>"
      );
    })();
    // (optional) enforcement so the user’s choice always wins
    (function enforceOuterBorderPreference() {
      var css =
        borderPref === "without"
          ? ".entry-wrapper,.entry-wrapper.print-a4-narrow{border:0!important;box-shadow:none!important;}"
          : ".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] outer border rule injected for:", borderPref);
     })();
     })();


Line 1,612: Line 1,558:
   /* 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,632: Line 1,568:
     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,726: Line 1,661:
     }
     }
   );
   );
  // ------ 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