User:DannyS712 test/sandbox2.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:DannyS712 test/sandbox2. |
//<nowiki>
$(function (){
var Bot_log_config = {
name: '[[User:DannyS712/Log|Log.js]]',
version: 1.0,
debug: true
};
var Bot_log_summary = 'Logging with ' + Bot_log_config.name + ' (version ' + Bot_log_config.version + ')';
mw.loader.using( 'mediawiki.util', function () {
$(document).ready( function () {
mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'log', 'ca-logTest', 'Test the logging function');
$('#ca-logTest').on('click', function() {
add_log();
} );
} );
} );
function add_log(){
var log = [
"Starting task #",
"Error detected with page",
"Ending task"
];
var target = "User:DannyS712 test/sandbox";
var summary = Bot_log_summary;
var title = "Logging – {{subst:#time: d F Y}}";
var content = "* " + log.join('\n* ');
console.log( content );
addNewSectionTo( summary, title, content, target );
}
function addNewSectionTo( summary, title, content, target) {
$.ajax({
url: mw.util.wikiScript( 'api' ),
data: {
format: 'json',
action: 'edit',
title: target,
section: 'new',
sectiontitle: title,
summary: summary,
text: content,
// bot: true,
token: mw.user.tokens.get( 'editToken' )
},
dataType: 'json',
type: 'POST',
success: function( data ) {
if ( data && data.edit && data.edit.result == 'Success' ) {
console.log( 'Logging complete at ' + target ); // reload page if edit was successful
} else if ( data && data.error ) {
alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
} else {
alert( 'Error: Unknown result from API.' );
}
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
}
});
//</nowiki>