MediaWiki:Gadget-SubmitLetter.js

From Tsadra Foundation Advanced Contemplative Scholarships

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.
var pdfFilename = mw.config.get( 'wgPageName' ).replace("/", "-").normalize("NFD").replace(/\p{Diacritic}/gu, ""); + '_TOC.pdf',
	uploadParam = {
		filename: pdfFilename,
		text: "[[" + "Category:" + mw.config.get( 'wgPageName' ) + "]]{{TOC" + " PDF}}",
		format: 'json',
		ignorewarnings: 1
	},
	fileInput = $( '<input class="d-block my-1" />' ).attr( 'type', 'file' ),
	submitBtn = $( '<input class="d-block my-1" />' ).attr({
		type: 'button',
		value: 'Upload PDF'
	}),
	api = new mw.Api();

$( '#TocPdfUploadInterface' ).append( [ fileInput, submitBtn ] );

$( submitBtn ).on( 'click', function () {
	api.upload( fileInput[ 0 ], uploadParam ).done( function ( data ) {
		console.log( data.upload.filename + ' has sucessfully uploaded.' );
	} ).fail( function ( data ) {
		console.log( data );
		console.log( "Upload failed!" );
	} );
	$("input[name='Book[TocPdf]']").attr('value', pdfFilename);
	$("#TocPdf").append(' <span style="color:green;"><i class="fa-duotone fa-check"></i> PDF Uploaded</span>');
} );