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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * This script interacts with [[Template:Get certificate]]
 * [[Category:Template script pages]]
 */

var TemplateCertificate = {

	init: function () {
		$( '.get-certificate' ).click( TemplateCertificate.getCertificate );
	},

	getCertificate: function () {
		var button = $( this );
		var certificate = button.data( 'certificate' );
		if ( !certificate ) {
			return;
		}

		// Ask for a name
		var name = prompt( "What's your name?" );
		if ( !name ) {
			return;
		}

		// Save the original HTML to restore it later
		var pageTitle = $( '#firstHeading' );
		var pageContent = $( '#mw-content-text .mw-parser-output' );
		var originalTitle = pageTitle.html();
		var originalContent = pageContent.html();

		// Generate the certificate
		var wikitext = '{{';  // Break it in two lines or else the MediaWiki parser parses it
		wikitext += certificate;
		if ( name ) {
			wikitext += '| name = ' + name;
		}
		wikitext += '}}';
		new mw.Api().parse( wikitext ).then( function ( html ) {
			// Use imagesLoaded plugin to wait for images to load
			mw.loader.getScript( 'https://unpkg.com/imagesloaded@5/imagesloaded.pkgd.min.js' ).then( function () {
				pageTitle.empty();
				pageContent.html( html );
				pageContent.imagesLoaded( function () {
					window.print();
					pageTitle.html( originalTitle );
					pageContent.html( originalContent );
				} );
			} );
		} );
	}   
};

$( TemplateCertificate.init );
Cookies help us deliver our services. By using our services, you agree to our use of cookies.