User:DannyS712 test/stubname.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:DannyS712 test/stubname. |
// Install this version with:
// <code><nowiki> {{subst:Iusc|User:DannyS712/Stub name.js}} </nowiki></code>
// or with
// <code><nowiki> importScript( 'User:DannyS712/Stub name.js' ); // Backlink: [[User:DannyS712/Stub name.js]] </nowiki></code>
//
// If forking this script, please note my contributions / give me credit
//<nowiki>
$(function (){
var SN_config = {
name: '[[User:DannyS712/Stub name|Stub name.js]]',
version: 1.0,
debug: true
};
var SN_summary = 'Correct name parameter with ' + SN_config.name + ' (version ' + SN_config.version + ')';
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
mw.loader.using( 'mediawiki.util', function () {
$(document).ready( function () {
mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'Stub name', 'ca-stub-name', 'Correct stub name');
$('#ca-stub-name').on('click', function() {
stub_name();
} );
} );
} );
function stub_name(){
var name = mw.config.get( 'wgPageName' );
var page = get_page ( name );
if (SN_config.debug) console.log ( page );
var new_page = page.replace(/(\| name\s*=).*/i, "$1 {{subst:FULLPAGENAME}}");
if (SN_config.debug) console.log ( new_page );
set_new ( name, new_page );
}
function get_page( name ){
var page_to_get = {
action: 'query',
titles: name,
prop: 'revisions',
rvprop: 'content',
format: 'json',
formatversion: 2
};
var result = null;
$.ajax({
url: scriptUrl,
type: 'get',
data: page_to_get,
dataType: 'json',
async: false,
success: function(page) {
if (SN_config.debug) console.log( page );
result = page.query.pages["0"].revisions["0"].content;
if (SN_config.debug) console.log( result );
}
});
return result;
}
function set_new ( page, new_content ){
console.log( page, new_content );
var to_send = {
action: 'edit',
title: page,
text: new_content,
minor: true,
summary: SN_summary,
token: mw.user.tokens.get( 'csrfToken' )
};
console.log( to_send );
$.when(
$.post( scriptUrl, to_send, function( response ){ } )
).done( function() {
location.reload();
} );
}
});
//</nowiki>