میڈیاویکی:Gadget-saveandedit.js

ویکی اقتباس سے

یاددہانی: محفوظ کرنے کے بعد، تبدیلیاں دیکھنے کے لیے آپ کو اپنے متصفح (browser) کے ابطن (cache) کو خالی کرنا ہوگا۔

  • فائرفاکس/ سفاری: جب Reload طق کریں تو Shift دباکر رکھیں، یا Ctrl-F5 یا Ctrl-R دبائیں (Mac پر R-⌘)
  • گوگل کروم: Ctrl-Shift-R دبائیں (Mac پر Shift-R-⌘)
  • انٹرنیٹ ایکسپلورر: جب Refresh طق کریں تو Ctrl یا Ctrl-F5 دبائیں
  • اوپیرا: Tools → Preferences میں ابطن (cache) صاف کریں
//[[en:User:Mabdul/saveandedit.js]]
 
//adding a new buttom
if(document.getElementsByClassName('editButtons')[0]!='undefined')
{
	document.getElementsByClassName('editButtons')[0].innerHTML += '<input type="button" id="saveandedit_button" name="saveandedit_button" value="محفوظ پھر ترمیم" onclick="saveandedit_prompt()" />';
}
 
//function for returning
function saveandedit_prompt(){
	//for displaying message on fail:
	form = '<div id="saveandedit_status"></div>';
	jsMsg(form);
	var newtext = document.editform.wpTextbox1.value;
	var summary = document.editform.wpSummary.value;
	var notminor = document.editform.wpMinoredit.checked;
	summary += ' “[[MediaWiki:Gadget-saveandedit|محفوظ پھر ترمیم]]” '; 
 
	//get the token
	var req = sajax_init_object();
	req.open("GET", wgScriptPath + "/api.php?action=query&prop=info&indexpageids=1&intoken=edit&format=json&titles="+encodeURIComponent(mw.config.get('wgPageName')), false);
	req.send(null);
	var response = eval('(' + req.responseText + ')');
	pageid = response['query']['pageids'][0];
	token = response['query']['pages'][pageid]['edittoken'];
	delete req;
 
	//Check if only a section will be edited
	var url = String(window.location);
	var section = "no";
	if(window.location.search != ""){
		var undef = url.split("?");
		var args  = undef[1].split("&");
		for(i=0; i < args.length; i++) {
			var a = args[i].split("=");
			if(a[0] == 'section')
				section = a[1];
		}
	}
 
	//do the edit
	var req = sajax_init_object();
	var params = "action=edit";
	if(section!="no")
		params += "&section="+section;
	params += "&format=json&token="+encodeURIComponent(token)+"&title="+encodeURIComponent(mw.config.get('wgPageName'))+"&text="+encodeURIComponent(newtext)+"&notminor="+notminor+"&summary="+encodeURIComponent(summary);
	url = wgScriptPath + "/api.php";
	req.open("POST", url, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");
	req.onreadystatechange = function() {
		if(req.readyState == 4 && req.status == 200) {
			response = eval('(' + req.responseText + ')');
			try {
				if (response['edit']['result'] == "Success") {
					//reload on the success
					window.location.reload();
				} else {
					//or handle out the error message
					document.getElementById('saveandedit_status').innerHTML = '<div style="color:red"><b>Edit failed on <a href="'+mw.config.get('wgArticlePath').replace("$1", encodeURI(mw.config.get('wgPageName')))+'" title="'+mw.config.get('wgPageName')+'">'+mw.config.get('wgPageName')+'</a></b></div>. Best try it again (<b>Don\'t forget to copy the text</b>). Error info:' +response['error']['code'] + ' : ' + response['error']['info'];
				}
			}
			catch(err) {
				document.getElementById('saveandedit_status').innerHTML = '<div style="color:red"><b>Edit failed on <a href="'+mw.config.get('wgArticlePath').replace("$1", encodeURI(mw.config.get('wgPageName')))+'" title="'+mw.config.get('wgPageName')+'">'+mw.config.get('wgPageName')+'</a></b></div>';
			}
			delete req;
		}
	};
	req.send(params);
}