User:Ocaasi/TWAcontents.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() {
			if ( !$('.wp-twa').length ) {
				return;
			}
			$('.wp-twa').find( '.submit' )
				.click( function( e ) {
					e.preventDefault();
					var targetPage = $( '.wp-twa' ).find( '.wp-twa-page' ).text();
					var msgPage = $( '.wp-twa' ).find( '.wp-twa-msg' ).text();
					var api = new mw.Api();
 
					api.get( {
						'action' : 'query',
						'titles' : msgPage,
						'prop'   : 'revisions|info',
						'intoken' : 'edit',
						'rvprop' : 'content',
						'indexpageids' : 1
					}, {
						'ok' : function(result) {
							result = result.query;
							var page = result.pages[result.pageids[0]];
							var text = page.revisions[0]['*'];
							api.post(
								{
									'action' : 'edit',
									'title' : targetPage,
									'appendtext' : "\n" + text,
									'summary' : 'New Message ([[WP:The Wikipedia Adventure|The Wikipedia Adventure]])',
									'token' : page.edittoken
								},
								{
									'ok' : function() {window.location.reload();}
								}
							);
						}
					}	);
				});
			}
		);
	});