MediaWiki:Gadget-EditPresets.js

if (mw.config.get('wgAction') === 'edit' && !!mw.user.getName) { $('#wpSummaryWidget').after(' '); $('Clear #preset-edit-buttons').appendTo('#preset-edit-buttons'); $('#clear-button').click(function {		$('#wpSummary').val('');	}); var api = new mw.Api; api.get({		action:'parse',		page: 'User:' + mw.user.getName + '/editPresets',		prop: ['text'],		format: 'json'	}).done(function(data) {		var array = [];		$(data.parse.text['*']).find('li').each(function(i, t) { array.push($(t).text); });		placeButtons(array);	}).fail(function(err) {		if (err === 'missingtitle') placeButtons([]);		else console.error(err);	}); }

function placeButtons(array) { if (!array.length) { var $createPresets = $('Click to set some custom presets! ').appendTo('#preset-edit-buttons'); return $createPresets.click(createEditPresets); }	var $editPresets = $('Edit Presets ').appendTo('#preset-edit-buttons'); $editPresets.click(openEditPresets); $.each(array, function(i, t) {		var name = ;		var text = ;		if (t.includes('|')) {			name = t.split('|')[0];			text = t.split('|')[1];		} else name = text = t;		var $button = $('' + name + ' #preset-edit-buttons').appendTo('#preset-edit-buttons');		$button.unbind('click');		$button.click(presetClick);	}); }

function presetClick(el) { var currText = $('#wpSummary').val; var text = $(el.target).attr('title'); if (currText.includes(text)) return; if (currText && !/^\/\*\s+\w+\s+\*\/\s*$/.test(currText)) $('#wpSummary').val(currText + ', ' + $(el.target).attr('title')); else $('#wpSummary').val(currText + (!!currText ? ' ' : '') + text); }

function openEditPresets(el) { var win = window.open('/User:' + mw.user.getName + '/editPresets?action=edit', '_blank'); win.focus; $(el.target).text('Reload the page to see your changes!'); $(el.target).prop('disabled', true); }

function createEditPresets(el) { new mw.Api.postWithToken('csrf', {		action: 'edit',		text: '',		title: 'User:' + mw.user.getName + '/editPresets',		summary: 'Created editPresets page for ' + mw.user.getName	}).done(function(res) {		var win = window.open('/' + res.edit.title + '?action=edit', '_blank');		win.focus;		$(el.target).text('Reload the page to see your changes!');		$(el.target).prop('disabled', true);	}).fail(function(err) {		console.error(err);	}); }