User:Blue/scripts/hotarticlerate.js

From RationalWiki
Jump to navigation Jump to search

Note: After saving, 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: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
//<nowiki>
 
function setupForHAR() {
	if (wgNamespaceNumber == 0 && document.getElementById("ca-talk").getElementsByTagName("a")[0].href.search(/redlink/i) == -1) {
		if (window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
		} else {
			var req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		req.onreadystatechange = function() {
			if (req.readyState == 4 && req.status == 200) {
				if (req.responseText.search(/{{rated/i) == -1) {
					
					var ratebutton = document.createElement("a");
					ratebutton.title = "Rate this article";
					ratebutton.innerHTML = "<span>Rate</span>";
					ratebutton.onclick = function() {
						var aform = document.createElement("div");
						aform.id = "ratingForm";
						var catsOutput = "<select id='catInput'>";
						for (var c in wgCategories) {
							catsOutput += "<option value='" + wgCategories[c] + "'>" + wgCategories[c] + "</option>";
						}
						catsOutput += "</select>";
						aform.innerHTML = "<div style='padding: .7em; margin: .4em; border: 1px solid #aaa; background-color: #f5f5f5; width: 500px;'><b>Rate this article</b><br/>Rating: <select id='ratingInput'><option value='0'>No brainstar</option><option value='1'>Copper</option><option value='2'>Bronze</option><option value='3'>Silver</option><option value='4'>Gold</option></select><br/>Category: " + catsOutput + " <em>or*</em> <input type='text' id='catInputText' /><br/>Icon (optional): <input type='text' id='iconInput' /><br/>Importance: <select id='importanceInput'><option value='LOW'>LOW</option><option value='MID'>MID</option><option value='HIGH'>HIGH</option></select><br/>Comments (optional): <textarea id='commentsInput' width='30' height='30'></textarea><br/><button onclick='doAR();'>Rate</button><button onclick='cancelAR();' style='margin-left: 1em;'>Cancel</button><br/><small><em>*If the box has text in it the dropdown option will be discarded</div>";
						document.getElementById("content").insertBefore(aform, document.getElementById("bodyContent"));
					}
					var holdingtab = document.createElement("li");
					holdingtab.appendChild(ratebutton);
					document.getElementById("ca-talk").parentNode.appendChild(holdingtab);
				}
			}
		}
		req.open("GET", wgServer + wgScript + "?title=Talk:" + wgPageName + "&action=raw", false);
		req.send(null);
	} else if (wgNamespaceNumber == 0 && document.getElementById("ca-talk").getElementsByTagName("a")[0].href.search(/redlink/i) != -1) {
		var ratebutton = document.createElement("a");
		ratebutton.title = "Rate this article";
		ratebutton.innerHTML = "<span>Rate</span>";
		ratebutton.onclick = function() {
			var aform = document.createElement("div");
			aform.id = "ratingForm";
			var catsOutput = "<select id='catInput'>";
			for (var c in wgCategories) {
				catsOutput += "<option value='" + wgCategories[c] + "'>" + wgCategories[c] + "</option>";
			}
			catsOutput += "</select>";
			aform.innerHTML = "<div style='padding: .7em; margin: .4em; border: 1px solid #aaa; background-color: #f5f5f5; width: 500px;'><b>Rate this article</b><br/>Rating: <select id='ratingInput'><option value='0'>No brainstar</option><option value='1'>Copper</option><option value='2'>Bronze</option><option value='3'>Silver</option><option value='4'>Gold</option></select><br/>Category: " + catsOutput + " <em>or*</em> <input type='text' id='catInputText' /><br/>Icon (optional): <input type='text' id='iconInput' /><br/>Importance: <select id='importanceInput'><option value='LOW'>LOW</option><option value='MID'>MID</option><option value='HIGH'>HIGH</option></select><br/>Comments (optional): <textarea id='commentsInput' width='30' height='30'></textarea><br/><button onclick='doAR();'>Rate</button><button onclick='cancelAR();' style='margin-left: 1em;'>Cancel</button><br/><small><em>*If the box has text in it the dropdown option will be discarded</div>";
			document.getElementById("content").insertBefore(aform, document.getElementById("bodyContent"));
		}
		var holdingtab = document.createElement("li");
		holdingtab.appendChild(ratebutton);
		document.getElementById("ca-talk").parentNode.appendChild(holdingtab);
	}
}
 
function cancelAR() {
	document.getElementById("content").removeChild(document.getElementById("ratingForm"));
}
 
function doAR() {
	var rating = document.getElementById("ratingInput").value;
	if (document.getElementById("catInputText").value != "") {
		var cat = document.getElementById("catInputText").value;
	} else {
		var cat = document.getElementById("catInput").value;
	}
	var icon = document.getElementById("iconInput").value;
	var imp = document.getElementById("importanceInput").value;
	var comments = document.getElementById("commentsInput").value;
	if (window.XMLHttpRequest) {
		var req = new XMLHttpRequest();
	} else {
		var req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	function gettoken_api() {
		var api = sajax_init_object();
		api.open('GET', wgServer + wgScriptPath + '/api.php?format=json&action=query&prop=info&indexpageids=1&intoken=edit&titles=Whatever', false);
		api.send(null);
		if (api.readyState == 4 && api.status == 200) {
			var response = eval('(' + api.responseText + ')');
			var token = response['query']['pages'][response['query']['pageids'][0]]['edittoken'];
		}
		return token;
	}
	var editToken = encodeURIComponent(gettoken_api());
	var pr = "{{rated|1=" + rating + "|2=" + cat;
	if (icon != "") pr = pr + "|3=" + icon;
	pr = pr + "|4=" + imp;
	if (comments != "") pr = pr + "|5=\n" + comments + "\n";
	pr += "}}\n\n";
	function bottest() {
		if (wgUserGroups.indexOf("bot") == -1) {
			return "";
		} else {
			return "&bot";
		}
	}
	var edittoken = encodeURIComponent(gettoken_api());
	if (rating == "1") {
		var ratingurl = "[[Category:Copper-level articles|copper]]";
	} else if (rating == "2") {
		var ratingurl = "[[Category:Bronze-level articles|bronze]]";
	} else if (rating == "3") {
		var ratingurl = "[[Category:Silver-level articles|silver]]";
	} else if (rating == "4") {
		var ratingurl = "[[Category:Cover story articles|gold]]";
	} else if (rating == "0") {
		var ratingurl = "ordinary";
	}
	// Add rating to talk page
	req.open("POST", wgServer + wgScriptPath + "/api.php?action=edit" + bottest() + "&prependtext=" + encodeURIComponent(pr) + "&title=Talk:" + wgPageName + "&notminor&summary=Added%20article%20rating%20" + encodeURIComponent(ratingurl) + ",%20" + encodeURIComponent("[[:Category:" + imp + " priority articles|" + imp + "]]") + "%20using%20[[User:Blue/scripts/hotarticlerate.js|HotArticleRate]]" + "&token=" + edittoken, true)
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(null);
	// Check whether page has brainstar
	if (rating != "0") {
		if (window.XMLHttpRequest) {
			var preq = new XMLHttpRequest();
		} else {
			var preq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		preq.onreadystatechange = function() {
			if (preq.readyState == 4 && preq.status == 200) {
				if (preq.responseText.search(/({{copper}}|{{bronze}}|{{silver}}|{{gold}})/i) == -1) {
					// Add brainstar to page
					if (window.XMLHttpRequest) {
						var breq = new XMLHttpRequest();
					} else {
						var breq = new ActiveXObject("Microsoft.XMLHTTP");
					}
					function gettoken_api2() {
						var api = sajax_init_object();
						api.open('GET', wgServer + wgScriptPath + '/api.php?format=json&action=query&prop=info&indexpageids=1&intoken=edit&titles=Whatever', false);
						api.send(null);
						if (api.readyState == 4 && api.status == 200) {
							var response = eval('(' + api.responseText + ')');
							var token = response['query']['pages'][response['query']['pageids'][0]]['edittoken'];
						}
						return token;
					}
					function bottest2() {
						if (wgUserGroups.indexOf("bot") == -1) {
							return "";
						} else {
							return "&bot";
						}
					}
					if (document.getElementById("ratingInput").value == "1") {
						var brainstar = "{{copper}}";
					} else if (document.getElementById("ratingInput").value == "2") {
						var brainstar = "{{bronze}}";
					} else if (document.getElementById("ratingInput").value == "3") {
						var brainstar = "{{silver}}";
					} else if (document.getElementById("ratingInput").value == "4") {
						var brainstar = "{{gold}}";
					}
					var edittoken2 = encodeURIComponent(gettoken_api2());
					breq.open("POST", wgServer + wgScriptPath + "/api.php?action=edit" + bottest2() + "&prependtext=" + encodeURIComponent(brainstar) + "&title=" + wgPageName + "&minor&summary=Added%20brainstar%20" + encodeURIComponent(brainstar) + "&token=" + edittoken2, true);
					breq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					breq.send(null);
					window.location.reload();
				} else { window.location.reload(); }
			}
		}
		preq.open("GET", wgServer + wgScript + "?title=" + wgPageName + "&action=raw", false);
		preq.send(null);
	}
}
 
addOnloadHook(setupForHAR);
 
//</nowiki>