更多操作
小 |
小 |
||
第6行: | 第6行: | ||
//mw.loader.load( '//www.mediawiki.org/w/load.php?modules=ext.gadget.Global-DownloadPDF' ); | //mw.loader.load( '//www.mediawiki.org/w/load.php?modules=ext.gadget.Global-DownloadPDF' ); | ||
mw.loader.load( '//docs.guohao.asia/w/load.php?modules=ext.gadget.Global-DownloadPDF' ); | mw.loader.load( '//docs.guohao.asia/w/load.php?modules=ext.gadget.Global-DownloadPDF' ); | ||
/** | |||
* This script prompts the user to print or download a specified set of pages | |||
* Documentation: https://www.mediawiki.org/wiki/Template:DownloadPDF | |||
* Author: Felipe Schenone (User:Sophivorus) | |||
* License: GNU General Public License (http://www.gnu.org/licenses/gpl.html) | |||
*/ | |||
// <nowiki> | |||
var DownloadPDF = { | |||
init: function () { | |||
$( '#mw-content-text' ).find( '.DownloadPDF' ).on( 'click', DownloadPDF.makePDF ); | |||
}, | |||
makePDF: function () { | |||
// Get the list of pages | |||
var $button = $( this ); | |||
var separator = $button.data( 'separator' ); | |||
var pages = $button.data( 'pages' ); | |||
pages = pages.split( separator ); | |||
// Make the wikitext of the book | |||
var wikitext = ''; | |||
for ( var page of pages ) { | |||
wikitext += '\n<div class="DownloadPDF-chapter">'; | |||
wikitext += '\n<h1 class="DownloadPDF-chapter-title">' + page + '</h1>'; | |||
wikitext += '\n<div class="DownloadPDF-chapter-content">{{:' + page + '}}</div>'; | |||
wikitext += '\n</div>'; | |||
} | |||
// Get the HTML of the book | |||
new mw.Api().parse( wikitext ).then( function ( html ) { | |||
// Save HTML of whatever page we're at to restore it later | |||
var $pageContent = $( '#mw-content-text .mw-parser-output' ); | |||
var pageContent = $pageContent.html(); | |||
// Replace the page HTML for the book HTML | |||
$pageContent.html( html ); | |||
// Wait for all images to load | |||
// @todo Use something like https://stackoverflow.com/a/75570052/809356 | |||
setTimeout( function () { | |||
// Hide elements we don't want to print | |||
var $firstHeading = $( '#firstHeading' ); | |||
$firstHeading.hide(); | |||
// Finally, print | |||
window.print(); | |||
// After print, restore the original page | |||
$firstHeading.show(); | |||
$pageContent.html( pageContent ); | |||
}, 1000 ); | |||
} ); | |||
} | |||
}; | |||
$( DownloadPDF.init ); | |||
// </nowiki> |
2025年4月9日 (三) 09:51的版本
/**
* DownloadPDF enables printing or downloading a specified set of pages
* Documentation: https://www.mediawiki.org/wiki/DownloadPDF
* Source code: https://www.mediawiki.org/wiki/MediaWiki:Gadget-Global-DownloadPDF.js
*/
//mw.loader.load( '//www.mediawiki.org/w/load.php?modules=ext.gadget.Global-DownloadPDF' );
mw.loader.load( '//docs.guohao.asia/w/load.php?modules=ext.gadget.Global-DownloadPDF' );
/**
* This script prompts the user to print or download a specified set of pages
* Documentation: https://www.mediawiki.org/wiki/Template:DownloadPDF
* Author: Felipe Schenone (User:Sophivorus)
* License: GNU General Public License (http://www.gnu.org/licenses/gpl.html)
*/
// <nowiki>
var DownloadPDF = {
init: function () {
$( '#mw-content-text' ).find( '.DownloadPDF' ).on( 'click', DownloadPDF.makePDF );
},
makePDF: function () {
// Get the list of pages
var $button = $( this );
var separator = $button.data( 'separator' );
var pages = $button.data( 'pages' );
pages = pages.split( separator );
// Make the wikitext of the book
var wikitext = '';
for ( var page of pages ) {
wikitext += '\n<div class="DownloadPDF-chapter">';
wikitext += '\n<h1 class="DownloadPDF-chapter-title">' + page + '</h1>';
wikitext += '\n<div class="DownloadPDF-chapter-content">{{:' + page + '}}</div>';
wikitext += '\n</div>';
}
// Get the HTML of the book
new mw.Api().parse( wikitext ).then( function ( html ) {
// Save HTML of whatever page we're at to restore it later
var $pageContent = $( '#mw-content-text .mw-parser-output' );
var pageContent = $pageContent.html();
// Replace the page HTML for the book HTML
$pageContent.html( html );
// Wait for all images to load
// @todo Use something like https://stackoverflow.com/a/75570052/809356
setTimeout( function () {
// Hide elements we don't want to print
var $firstHeading = $( '#firstHeading' );
$firstHeading.hide();
// Finally, print
window.print();
// After print, restore the original page
$firstHeading.show();
$pageContent.html( pageContent );
}, 1000 );
} );
}
};
$( DownloadPDF.init );
// </nowiki>