Jump to content

User:Bardic Wizard/reading list sync.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.
/*UNTESTED SCRIPT PLEASE BE CAREFUL 
It will pop up an error on every page load. I have given up fixing it. 
*/
( function($, mw) {
//Thank you [[User:The Transhumanist/SearchSuite.js]] for explaining script error

//some Code from [[Wikipedia:User_scripts/Guide]]
 	var page = getPname();
 	var listAddress = "User:"+mw.config.get('wgUserName')+"/Reading list";
 	var pages = "[["+ page+"]]";
/* 	function getUserName(){// [[Wikipedia:WikiProject User scripts/Scripts/Get user name]]

  switch (wgCanonicalNamespace) {
    case 'User': case 'User_talk': return wgTitle.split('/')[0];
    default: return null;
  }

}*/
 
function getPname() { //thank you [[Wikipedia:WikiProject User scripts/Scripts/Get Page Name]]
  return mw.config.get('wgPageName').replace(/_/g, ' ');
}
function listAdd() {
 /*if (wgCanonicalNamespace == "User" || wgCanonicalNamespace == "User_talk") {
  	return();
   }*/
    var newElement = mw.util.addPortletLink("p-cactions", '', "Add to reading list", "ca-readinglist", "Add to your reading list");
    $(newElement).on( 'click', function(e){
    	e.preventDefault();
    	editPage( {
    		title: listAddress,
    		text: pages,
    		summary: "adding to reading list [[User:Bardic Wizard/reading list sync.js|using script]]"}
    	);
    } );
}

// Edit page (must be done through POST)
// the line "text: info.text," will cause the call 
// to replace entire page content with supplied data.
// alternatively, one can append or prepend the data to the page, by using
// "appendtext: info.text," or "prependtext: info.text," instead.
// when using "appendtext", it is possible to append the text to a specific section,
// by setting the optional field "section".
function editPage( info ) {
	var api = new mw.Api();
	api.postWithToken( 'edit', {
		action: 'edit',
		title: info.title,
		appendtext: info.text, // will replace entire page content
		summary: info.summary
	})
	.done (function( data ) {
		if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
			alert( 'Page edited!' );
		} else {
			alert( 'The edit query returned an error. =(' );
		}
	})
	.fail ( function() {
		alert( 'The ajax request failed.' );
	});
}

$.when( $.ready, mw.loader.using( [
	'mediawiki.util',
	'user.options',
	'mediawiki.api'
])).done(listAdd);

} ) ( jQuery, mediaWiki );