Jump to content

User:Technical 13/SandBox/wikiSwitcher.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Technical 13 (talk | contribs) at 20:11, 13 October 2013 (Okay, this fix anything?). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
if ( mw.config.get( 'wgTitle' ) == mw.config.get( 'wgUserName' ) && mw.config.get( 'wgNamespaceNumber' ) == 2 ) {

	/* GLOBAL (needed???) */
	var pageTitle = mw.config.get( 'wgTitle' );
	var currUser = mw.config.get( 'wgUserName' );
	var pageID = mw.config.get( 'wgArticleId' );
	if ( typeof(editPage) == 'undefined' || editPage === '' ) { editPage = "User:" + currUser + "/Availability"; }
	else { editPage = "User:" + currUser + "/" + editPage; }
	var pageCurrText = getPageText();
	loadAvailabilities();
	/* GLOBAL */
	 
	function jqEsc( expression ) {
	    return expression.replace( /[!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~]/g, '\\$&' );
	}
	 
	function getPageText() {
		return $.ajax({
			url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?titles=' + editPage.replace(' ','_') + '&action=query&prop=revisions&rvprop=content&format=json&indexpageids=true',
			dataType: 'json',
			success: function( response ){
				pageID = response.query.pageids[0];
				pageCurrText = response.query.pages[pageID].revisions[0]['*'];
				console.info( 'Success!\nPage ID: %d contains:\n%s', pageID, pageCurrText );
			},
			error: function( response ){
				console.warn( 'Failed! Page contains:\n%s', response );
				pageCurrText = 'empty';
			}
		});
	}
	
	function loadAvailabilities() {
		var linkAvailable = mw.util.addPortletLink(
			'p-personal',
			'#',
			'Update status',
			'pt-available',
			'Set wikiBreak status',
			null,
			'#pt-watchlist'
		);
		$( listStyle ).click( function ( e ) {
			e.preventDefault();
			/* STUFF TO DO */alert( 'Popup and kick and scream!!!!' )/* STUFF TO DO */
		});
	}
	 
	function editPage( newPageText, pageSummary ) {
		var anEditToken = mw.user.tokens.get( 'editToken' );
		console.info( 'Got token: %s', editToken );
		pageSummary = pageSummary.substring(0, 198) + " - [[User:Technical 13/Scripts/wikiSwitcher|wiki break switcher]]";
		console.info( 'Edit summary:\n%s\nNew content:\n%s', pageSummary )
		var request = {
			'action': 'edit',
			'title': pageTitle,
			'text': newPageText,
			'summary': pageSummary,
			'token': anEditToken
		}
		var api = new mw.Api();
		api.post( request )
	}
	
	function changeAvailability() {
		console.log( 'Setting availability to %s', availability );
		//<nowiki> Set up the new template to use
		var pageNewText = '{{' + 'WikibreakSwitch|' + availability + '}}';
		//</nowiki>
		editPage( pageNewText, ' is now ' + availability );
	}
}