Jump to content

MediaWiki:Common.js

From International Treaty Law Wiki

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. */
/* Clean Print mode + PDF & Share buttons for Vector-2022 */

$(function () {

    // --- 1. Inject Clean Print CSS (full-width A4, desktop + mobile) ---
    var printCss = `
@media print {

    /* Hide all non-content elements */
    #mw-page-base,
    #mw-head-base,
    #mw-navigation,
    #mw-panel,
    #footer,
    .vector-header,
    .vector-page-toolbar,
    .vector-toc,
    .vector-sticky-header,
    .vector-page-tools,
    .vector-menu,
    .mw-indicators,
    .vector-user-links,
    .vector-search-box,
    .vector-pinned-container {
        display: none !important;
    }

    /* Full-width content for A4 printing */
    #content,
    .mw-body,
    .mw-content-container,
    .mw-body-content {
        margin: 0 !important;
        padding: 0 !important;
        width: 100% !important;
        max-width: none !important;
    }

    /* White background for clean PDF */
    body,
    #content {
        background: white !important;
    }

    /* Better print typography */
    .mw-body-content {
        font-size: 14pt !important;
        line-height: 1.5 !important;
    }
}
    `;

    var styleEl = document.createElement('style');
    styleEl.type = 'text/css';
    styleEl.appendChild(document.createTextNode(printCss));
    document.head.appendChild(styleEl);


    // --- 2. Add PDF + Share buttons to Vector-2022 action bar ---

    // Only on article pages (namespace 0)
    if (mw.config.get('wgNamespaceNumber') !== 0) return;

    // Find the Vector-2022 action bar (Read / Edit / View history)
    var $actions = $('.vector-page-toolbar .vector-page-tools .vector-menu-content-list');

    if ($actions.length) {

        // Remove any old PDF buttons (cleanup)
        $actions.find('a:contains("PDF")').closest('li').remove();

        // PDF button (uses clean print mode)
        $actions.prepend(
            '<li class="vector-menu-content-list-item"><a href="javascript:window.print()" title="Download clean PDF">PDF</a></li>'
        );

        // Share button
        $actions.prepend(
            '<li class="vector-menu-content-list-item"><a href="javascript:navigator.share ? navigator.share({title: document.title, url: location.href}) : prompt(\'Copy link:\', location.href)" title="Share this page">Share</a></li>'
        );
    }
});