User:Alex brollo/replacer.js

From Wikisource
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
(function () {
	var alex = alex || {};
	alex.getRawCode=function (title, site) {
	console.log(title,site);
	if (site===undefined) site=mw.config.get("wgServer");
	var data= {format: 'json',action: 'query',titles: title,   prop: 'revisions',rvlimit: '1',rvprop: 'content|user|ids'};
	var rawCode=""; var user=""; var revid="";
	$.ajax({
		url: site+'/w/api.php',
		async:true,
		data:data,
		dataType: 'jsonp'
	}).done(function (data) {
		if (data.query.pages[-1]===undefined) {
			var pageData=""; 
			for (var i in data.query.pages) {
				pageData=data.query.pages[i];
			} 
		rawCode=pageData.revisions[0]["*"];
		// remove header/footer
		rawCode=rawCode.substring(rawCode.indexOf("</noinclude>")+12,rawCode.lastIndexOf("<noinclude>"));
		user=pageData.revisions[0].user;
		revid=pageData.revisions[0].revid;
		rawCode="<!--\nuser:"+user+"\nrevid:"+revid+"\niwpage code:"
			+$("#wpTextbox1").val().match(/\{\{[iI]wpage\|(\w+)\}\}/)[1]+"\n-->"+rawCode;
		$("#wpTextbox1").val(rawCode);
		$("#replacerButton").remove();
		} else {
			alert("Pagina non esistente");
		}
	});
};
function attiva (){
var test= $("#wpTextbox1").val().match(/\{\{[iI]wpage\|(\w+)\}\}/);
if (test.length===2) {
	// creo bottone
	$("<div>")
		.css({"position":"fixed","top":"400px","left":"400px","z-index":"1000"})
		.attr("id","replacerButton")
		.append(
			$("<img>").attr("src","//upload.wikimedia.org/wikipedia/commons/1/17/Silk_arrow_refresh.png")
				.click(function (){
					alex.getRawCode(mw.config.get("wgPageName"),"//"+test[1]+".wikisource.org");
					} )
			)
		.appendTo($("#content"));
	}
	}
$(document).ready(function () {if (mw.config.get("wgCanonicalNamespace")==="Page" 
			&& mw.config.get("wgAction")==="edit") attiva();
	})
	
	
})();