User:Equazcion/post-to-talk.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(function(){
	mw.loader.using( ['jquery.ui', 'mediawiki.api'], function() {

		var header = 'Header';
		var body = 'Body';
		var summary = 'an edit summary';

		var wikitext = '\n\n==' + header + '==\n\n' + body;

		$('.ui-button-text:first').click(function(){
			postToTalk(wikitext, summary);
		});

		//postToTalk(wikitext, summary);


		function postToTalk( wikitext, summary ) {

			var api = new mw.Api();

			api.get( {
				'action' : 'query',
				'titles' : 'User:' + wgUserName,
				'prop'   : 'revisions|info',
				'intoken' : 'edit',
				'rvprop' : 'content',
				'indexpageids' : 1
			}, {
				'ok' : function(result) {
					result = result.query;
					var page = result.pages[result.pageids[0]];

					api.post(
						{
							'action' : 'edit',
							'titles' : 'User:' + wgUserName,
							'appendtext' : wikitext,
							'summary' : summary,
							'token' : page.edittoken
						},
						{
							'ok' : function() {window.location.reload();}
						}
					);
				}
			});
		}
	})
})