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:49, 13 October 2013 (Ohh! The variable has the same name as a function! That's why!!!). 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.
/* 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 === '' ) { var editPage = "User:" + currUser + "/Availability"; }
else { editPage = "User:" + currUser + "/" + editPage; }
console.warn('Working on page: %s', editPage);
if ( pageTitle === currUser && mw.config.get( 'wgNamespaceNumber' ) === 2 ) {
	getPageText();
	loadAvailabilities();
}
/* GLOBAL */
 
function jqEsc( expression ) {
    return expression.replace( /[!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~]/g, '\\$&' );
}
 
function getPageText() {
	console.warn('Pinging the api with URL: %s%s/api.php?titles=%s&action=query&prop=revisions&rvprop=content&format=json&indexpageids=true', mw.config.get( 'wgServer' ), mw.config.get( 'wgScriptPath' ), editPage.replace(' ','_'))
	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;
			console.warn('%s is page number %d\nresponse: %o', editPage.replace(' ','_'), pageID, response);
			var pageCurrText = response.query.pages[pageID].revisions[0]['*'];
			console.log( 'Success!\nPage ID: %d contains:\n%s', pageID, pageCurrText );
		},
		error: function(response){
			console.log( 'Failed! Page contains:\n%s', response );
			var pageCurrText = '';
		}
	});
}

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 editThePage( newPageText, pageSummary ) {
	var anEditToken = mw.user.tokens.get( 'editToken' );
	console.log( 'Got token: %s', editToken );
	pageSummary = pageSummary.substring(0, 198) + " - [[User:Technical 13/Scripts/wikiSwitcher|wiki break switcher]]";
	console.log( '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>
	editThePage( pageNewText, ' is now ' + availability );
}