User:The Evil IP address/hdedit.js

/**

  • Allows for one click modification of section headings when viewing a page
  • add the following line to your .js file to use it:

importScript( 'User:The Evil IP address/hdedit.js' ); // Backlink: User:The Evil IP address/hdedit.js

  • @author Janko Hoener (The Evil IP address)
  • @version 1.1.1
  • /

mw.loader.load('mediawiki.api');

window.hdedit = {

openeditform: function ($e) {

hdedit.anchor = $e.attr('id');

hdedit.pagename = mw.config.get('wgPageName');

hdedit.api = new mw.Api();

hdedit.api.get( {

action: 'parse',

page: this.pagename,

prop: 'sections',

format: 'json'

}).done( function (data) {

error = false;

error_msg = '';

$.each(data.parse.sections, function (i, v) {

if (v.anchor == hdedit.anchor) {

if (v.index !== '') {

hdedit.index = v.index;

}

else {

error = true;

error_msg = 'This section cannot be edited automatically ' +

'because it uses HTML formatting.';

}

if (v.fromtitle && (v.fromtitle != hdedit.pagename)) {

error = true;

error_msg += 'This section cannot be edited automatically ' +

'because it is transcluded from a template.';

}

return false;

}

});

if (error) {

hdedit.seterror(error_msg);

return;

}

if (hdedit.$e) {

hdedit.cancel();

}

hdedit.$e = $e;

hdedit.api.get({

action: 'parse',

page: hdedit.pagename,

section: hdedit.index,

prop: 'wikitext',

format: 'json'

}).done( function (obj) {

hdedit.wikitext = obj.parse.wikitext['*'];

hdedit.section_wikitext = hdedit.wikitext.replace(/^(=+)\s*(.+?)\s*\1[\s\S]*$/, '$2');

hdedit.inputsize = hdedit.section_wikitext.length*1.5;

var form = $('

').css('display', 'inline').submit(hdedit.save);

var input = $('').attr('id', 'hdedit_input').attr('size', hdedit.inputsize).val(hdedit.section_wikitext);

var button1 = $('