User:SD0001/private-sandbox.js

/** * Deprecated. 2020-10-04. * I recommend use of the "Wikitext" extension[1] in Visual Studio Code, * which brings in the reliability and stability of the local file system * while allowing previews and syntax highlightening. The "sandboxes" are files * on your system so you can make as many as you want! * * This script causes the saved sandbox text to be exported on evey page load. * * [1]: https://marketplace.visualstudio.com/items?itemName=RoweWilsonFrederiskHolme.wikitext * */

/** * Creates a private sandbox for you, accessible at Special:PrivateSandbox * Useful for things like drafting evidence for ArbCom/ANI, which are better * done privately but not off-wiki because you want to be able to preview. */ $.when(	$.ready,	mw.loader.using(['mediawiki.user', 'mediawiki.api']) ).then(function {	if (mw.config.get('wgPageName') !== 'Special:PrivateSandbox' && mw.config.get('wgPageName') !== 'Special:BlankPage/PrivateSandbox') {

return; }

document.title = 'Private sandbox'; $('#firstHeading').text('Private sandbox'); var api = new mw.Api;

$('#mw-content-text').empty.append(		$(' ').attr('id', 'ps-preview'),		$(' ').val(mw.user.options.get('userjs-pvt-sandbox'))			.attr({ id: 'ps-textarea', cols: 80, rows: 25, tabindex: 1, accesskey: ',' })			.css({ 'margin': '20px 0 10px 0', 'font-family': mw.user.options.get('editfont') }),		$(' ').text('Save').click(function { $('#ps-status').text('Saving').css('color', 'blue'); var text = $('#ps-textarea').val; api.saveOption('userjs-pvt-sandbox', text).then(function {				$('#ps-status').empty;				$('#ps-lastsaved').text('Last saved at ' + new Date.toLocaleString);				mw.user.options.set('userjs-pvt-sandbox', text);			}, function(err) {				$('#ps-status').text('Failed to save: ', err).css('color', 'red');			}); }),		$(' ').text('Preview').click(function { $('#ps-status').text('Retrieving preview').css('color', 'blue'); var text = $('#ps-textarea').val; api.post({				action: 'parse',				text: text,				title: 'PrivateSandbox', 				prop: 'text|categorieshtml',				disableeditsection: '1',				formatversion: '2'			}).then(function(json) {				$('#ps-status').empty;				$('#ps-preview').html(json.parse.text + json.parse.categorieshtml);			}, function(err) {				$('#ps-status').text('Failed to fetch preview: ', err).css('color', 'red');			}); }).css({ 'padding-left': '5px' }),		$(' ').attr('id', 'ps-status'),		$(' ').attr('id', 'ps-lastsaved')	);

window.onbeforeunload = function(e) { if( $('#ps-textarea').val !== mw.user.options.get('userjs-pvt-sandbox') ) { e.returnValue = 'Your changes have not been saved'; return e.returnValue; }	};

});