User:Gryllida/js/addInstantSaveToCodeEditor.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:Gryllida/js/addInstantSaveToCodeEditor. |
/*
Author : Svetlana Tkachenko svetlana@members.fsf.org
This file is a part of addInstantSaveToCodeEditor.
Licence: GPLv3+
Version: 0.1
Release date: 2018-02-26
Description: adds an instant save button to CodeEditor
*/
// Check that CodeEditor is loaded
mw.loader.using(['mediawiki.api', 'oojs-ui'], function () {
if('.wikiEditor-ui'){
var button = new OO.ui.ButtonWidget( {
label: 'Instant Save'
} );
// Instant save on click
button.$element.click(function(){
// Update button text
button.setLabel( 'Saving...');
// Get text area contents
var textbox = $('#wpTextbox1');
var context = textbox && textbox.data('wikiEditor-context');
var currentText = context.$textarea.textSelection( 'getContents' );
// Save the page via AJAX edit api
var api = new mw.Api();
api.postWithToken("edit", {
action: 'edit',
title: mw.config.get ('wgPageName'),
text: currentText,
summary: $('#wpSummary').val() + ' ([[User:Gryllida/js/addInstantSaveToCodeEditor.js|assisted]])'
}).done(function (data){
// Success; Update button text
button.setLabel( 'Instant Save');
});
});
$('#wpDiffWidget').after(button.$element);
}
});
// text: $('#wpTextbox1').text()