MediaWiki:Common.js: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
/* | /* Clean Print mode + PDF & Share buttons for Vector-2022 */ | ||
$(function () { | $(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) | // Only on article pages (namespace 0) | ||
if (mw.config.get('wgNamespaceNumber') !== 0) return; | if (mw.config.get('wgNamespaceNumber') !== 0) return; | ||
// Find the Vector-2022 action bar ( | // Find the Vector-2022 action bar (Read / Edit / View history) | ||
var $actions = $('.vector-page-toolbar .vector-page-tools .vector-menu-content-list'); | var $actions = $('.vector-page-toolbar .vector-page-tools .vector-menu-content-list'); | ||
if ($actions.length) { | if ($actions.length) { | ||
// PDF | // Remove any old PDF buttons (cleanup) | ||
$actions.find('a:contains("PDF")').closest('li').remove(); | |||
// PDF button (uses clean print mode) | |||
$actions.prepend( | $actions.prepend( | ||
'<li class="vector-menu-content-list-item"><a href="javascript:window.print()" title="Download | '<li class="vector-menu-content-list-item"><a href="javascript:window.print()" title="Download clean PDF">PDF</a></li>' | ||
); | ); | ||
// Share | // Share button | ||
$actions.prepend( | $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>' | '<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>' | ||
Revision as of 22:26, 17 January 2026
/* 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>'
);
}
});