User:Ricordisamoa/SectionTools.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* <nowiki>
 * adds some links to simply *rename* or *remove* a wiki-section
 * extended and improved version of [[commons:User:Ricordisamoa/RenameSection.js]]
 * @author [[User:Ricordisamoa]]
*/
$(document).ready(function(){
	if(mw.config.get('wgAction') !== 'view'){
		return;// in 'view' mode
	}
	var divider = $(".mw-editsection-divider").first().clone(true).show(),
	api = new mw.Api();
	if(divider.length === 0){
		divider = $('<span>').addClass('mw-editsection-divider').show().text(' | ');
	}
	else if(divider.text() === ''){
		divider.text(' | ');
	}
	$('.mw-editsection').each(function(){
		var el = $(this),
		index = el
		.children('a')
		.not('.mw-editsection-visualeditor')
		.first().attr('href').match(/^.+[\?&]section=([0-9]+)(&.+)?$/);
		if(index === null){
			return;// no valid section-index
		}
		index = parseInt(index[1]);
		$.ajax({
			url: mw.util.wikiScript(),
			data:{
				title: mw.config.get('wgPageName'),
				action: 'raw',
				section: index
			},
			cache: false// important
		})
		.done(function(d){
			var toolsLink = '[[' + (mw.config.get('wgServer') === '//meta.wikimedia.org' ? '': 'm:') + 'User:Ricordisamoa/SectionTools|',
			oldTitle = d.match(/^[\s\n\r]*=+\s*([^=\n\r]+[^\s=])\s*=+/)[1];
			$('<a>')
			.text('rename')
			.attr({
				title: 'rename this section',
				href: '#'
			})
			.click(function(event){
				event.preventDefault();
				$(el).siblings('.mw-headline')
				.text(oldTitle)// putting the original heading
				.attr('contenteditable', true)// make editable
				.keydown(function(evt){
					if(evt.keyCode === 13){// 'enter'
						evt.preventDefault();
						var newTitle = $(this).text(),
						newText = d.replace(/^([\s\n\r]*=+\s*)[^=\n\r]+[^\s=](\s*=+)/, '$1' + newTitle + '$2');
						api.postWithToken('edit', {
							action: 'edit',
							format: 'json',
							text: newText,
							title: mw.config.get('wgPageName'),
							section: index,
							summary: '/* ' + newTitle + ' */ section ' + toolsLink + 'renamed]] from "' + oldTitle + '"'
						},
						function(){
							mw.notify('Section renamed');
							setTimeout(function(){
								window.location.reload();
							}, 2500);
						},
						function(){
							mw.notify('The AJAX request failed.');
						});
					}
				});
			})
			.insertBefore($(el).find('.mw-editsection-bracket').last())
			.before(divider.clone(true));
			$('<a>')
			.text('remove')
			.attr({
				title: 'remove this section',
				href: '#'
			})
			.click(function(event){
				event.preventDefault();
				api.postWithToken('edit', {
					action: 'edit',
					text: '',
					title: mw.config.get('wgPageName'),
					section: index,
					summary: '/* ' + oldTitle + ' */ ' + toolsLink + 'removed]] section',
				},
				function(){
					mw.notify('Section removed');
					setTimeout(function(){
						window.location.reload();
					}, 2500);
				},
				function(){
					mw.notify('The AJAX request failed.');
				});
			})
			.insertBefore($(el).find('.mw-editsection-bracket').last())
			.before(divider.clone(true));
		});
	});
});
/* </nowiki> */