User:SD0001/private-sandbox.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:SD0001/private-sandbox. |
/**
* 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;
}
};
});