User:BPositive/prepare.js

mw.loader.using( ['jquery.ui.dialog', 'mediawiki.api'], function {	//Function to show the content in a dialog box	function renderContentDialog ( content ) {		var $dialog = $( '  ' )			.html ( content )			.dialog ({ autoOpen: true, title: 'Text after preparing the page ' + wgPageName + " for translation", width: '70%', modal: true });	}	function addLanguageBar(pageContent){		//&lt; and &gt; are just for display purpose. Will be substituded with    	pageContent = "&lt;languages/&gt;\n" + pageContent.replace(/+/gi, "");    	console.log(pageContent);    	//regexp = +    	return pageContent;    }	function fixInternalLinks ( pageContent ) {		var normalizeRegex = new RegExp( /\[\[([^|]*?)\]\]/gi );		// First convert all links into two-party form. If a link is not having a pipe,		// add a pipe and duplicate the link text		pageContent = pageContent.replace( normalizeRegex, "$1");		var linkPrefixRegex = new RegExp( /\[\[(?!Category:|m:|w:)(.*?)\]\]/gi );		// Add the Special:MyLanguage/ prefix for all internal links except those starting		// with Category, w:, m:. Other namespaces can be added eventually.		pageContent = pageContent.replace( linkPrefixRegex, "Special:MyLangauge/$1");		return pageContent;	} ///w/api.php?action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&titles=User%3ABPositive //Function to prepare the page for translation function prepareForTranslation{ var api = new mw.Api; //This api call returns the raw source text of the page api.get ({	       action:'query',	        prop: 'revisions',	        format: 'json',	        rvprop: 'content',	        rvlimit: '1',	        titles: wgPageName	    }).done ( function( data ) {	    	var pageContent = data['query']['pages'][wgArticleId]['revisions'][0]['*'];	    	//pageContent.replace('\\n', ' ');	    	pageContent = addLanguageBar( pageContent );	    	pageContent = fixInternalLinks( pageContent );	    	renderContentDialog(pageContent);	    }); }   $(document).ready( function {		// Add a link to the toolbox		var link = mw.util.addPortletLink( 'p-tb', '#',			'Prepare', 't-preparetranslation', 'Prepare the page for translation', null, '#t-whatlinkshere' );		$(link).click( function( e ) { // Avoid the browser going to '#' e.preventDefault; prepareForTranslation; });	}); });