User:SD0001/private-sandbox.js

Source: Wikipedia, the free encyclopedia.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * 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(
		$('<div>').attr('id', 'ps-preview'),
		$('<textarea>').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') }),
		$('<button>').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');
			});
		}),
		$('<button>').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' }),
		$('<div>').attr('id', 'ps-status'),
		$('<div>').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;
		}
	};

});