User:DanCherek/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. |
This user script seems to have a documentation page at User:DanCherek/sandbox. |
//<nowiki>
/**
* Attribution: This script is a derivative of [[MediaWiki:Request-page-protection-form.js]],
* authored by User:Enterprisey and User:SD0001.
*/
$.when($.ready, mw.loader.using(['mediawiki.api', 'mediawiki.widgets', 'mediawiki.util'])).then(function(config) {
//if ((mw.config.get("wgPageName") !== "Wikipedia:WikiProject_Resource_Exchange/Resource_Request/Form")) {
// return;
//}
var api = new mw.Api();
var previewApi = new mw.Api();
var requestsPageTitle = 'User:DanCherek/sandbox';
var template = `== $heading ==
$request`;
var headingInput = new OO.ui.TextInputWidget({
'required': true,
});
var requestInput = new OO.ui.MultilineTextInputWidget({
'value': `<!-- Brief citation of the requested materials. -->
{{refbegin}}
* {{cite journal |ref=none |no-tracking=yes<!-- prevents error categorization --> |last= |first= |date= |title= |work= |publisher= |location= |page= |url= |access-date= |isbn= |issn= |oclc= }}
{{refend}}
<!-- Wikipedia articles being improved. -->
For [[Example article]]
Thanks, ~~~~`,
'required': 'true',
'autosize': 'true',
'classes': [ 'mw-editfont-monospace' ]
});
var requestPreview = new OO.ui.LabelWidget();
var submitButton = new OO.ui.ButtonWidget({
'label': 'Submit request',
'flags': ['progressive', 'primary'],
'accesskey': 's'
});
var fieldset = new OO.ui.FieldsetLayout();
fieldset.addItems([
new OO.ui.FieldLayout(headingInput, {
'label': 'Section heading (briefly describe your request):',
align: 'top'
}),
new OO.ui.FieldLayout(requestInput, {
'label': 'Request:',
align: 'top'
}),
new OO.ui.FieldLayout(requestPreview, {
'label': 'Preview:',
align: 'top'
}),
new OO.ui.FieldLayout(submitButton)
]);
$("#mw-content-text").empty().append(fieldset.$element);
function makeRequestText() {
return template.replace(/\$heading/g, headingInput.getValue()).replace(/\$request/g, requestInput.getValue());
}
function updateForm() {
var hasHeading = headingInput.getValue().trim().length > 0;
var hasRequest = requestInput.getValue().trim().length > 0;
var formEnabled = hasHeading && hasRequest;
submitButton.setDisabled(!formEnabled);
if (!hasHeading && !hasRequest) submitButton.setLabel('Add heading and enter request');
else if (!hasHeading) submitButton.setLabel('Add heading');
else if (!hasRequest) submitButton.setLabel('Enter request');
else submitButton.setLabel('Submit request');
if (formEnabled) {
previewApi.abort();
previewApi.parse(makeRequestText(), {
pst: true,
title: requestsPageTitle
}).then(function(text) {
text = text.replace(/<script/g, '<script');
requestPreview.$element.html(text);
});
}
}
headingInput.on('change', updateForm);
requestInput.on('change', updateForm);
submitButton.on('click', function() {
updateForm();
if (submitButton.isDisabled()) {
return;
}
submitButton.setDisabled(true);
submitButton.setLabel('Submitting...');
api.edit(requestsPageTitle, function() {
return {
appendtext: '\n\n' + makeRequestText(),
summary: '/* ' + headingInput.getValue() + ' */ new section'
};
}).then(function() {
window.location.href = mw.util.getUrl(requestsPageTitle);
});
});
});
//</nowiki>