User:Blue/scripts/hotstubcat.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 setupForStubcat() {
	var stubtemp = getElementsByClassName(document.getElementById('bodyContent'),"*","stubtemplate");
	if (stubtemp) stubtemp = stubtemp[0]; if (!stubtemp) return false;
	if (stubtemp.getElementsByTagName("i")[0].innerHTML == "This  article is a <b>stub.</b>") { // Uncategorized
		var stubcatDiv = document.createElement("div");
		stubcatDiv.id = "stubcatDiv";
		stubcatDiv.style.backgroundColor = "#f9f9f9";
		stubcatDiv.style.borderLeft = "1px solid #aaa";
		stubcatDiv.style.borderRight = "1px solid #aaa";
		stubcatDiv.style.borderBottom = "1px solid #aaa";
		stubcatDiv.style.padding = ".3em";
		stubcatDiv.style.margin = "0em auto .4em auto";
		stubcatDiv.style.textAlign = "center";
		stubcatDiv.style.width = "100px";
		stubtemp.style.marginBottom = "0";
		var cathotlink = document.createElement("span");
		cathotlink.innerHTML = "<a style='color: #0645AD; cursor: pointer'>categorize</a>";
		cathotlink.onclick = function() {
			var stubcatFormSpan = document.createElement("span");
			stubcatFormSpan.id = "stubcatForm";
			stubcatFormSpan.innerHTML = "<span id='stubcatForm'><input type='text' onkeyup='lookupStubcat()' id='stubcatInput' /><button id='stubcatOK' style='margin-left: .2em;' onclick='executeStubcat()'>OK</button><button style='margin-left: .2em;' onclick='cancelStubcat()'>Cancel</button></span>";
			document.getElementById("stubcatDiv").innerHTML = "";
			document.getElementById("stubcatDiv").style.width = "260px";
			document.getElementById("stubcatDiv").appendChild(stubcatFormSpan);
		}
		stubcatDiv.appendChild(cathotlink);
		stubtemp.parentNode.insertBefore(stubcatDiv, stubtemp.nextSibling);
	} else { // Already categorized
		var stubcatDiv = document.createElement("div");
		stubcatDiv.id = "stubcatDiv";
		stubcatDiv.style.backgroundColor = "#f9f9f9";
		stubcatDiv.style.borderLeft = "1px solid #aaa";
		stubcatDiv.style.borderRight = "1px solid #aaa";
		stubcatDiv.style.borderBottom = "1px solid #aaa";
		stubcatDiv.style.padding = ".3em";
		stubcatDiv.style.textAlign = "center";
		stubcatDiv.style.width = "100px";
		stubcatDiv.style.margin = "0em auto .4em auto";
		stubtemp.style.marginBottom = "0";
		var cathotlink = document.createElement("span");
		cathotlink.innerHTML = "<a style='color: #0645AD; cursor: pointer'>change category</a>";
		cathotlink.onclick = function() {
			var stubcatFormSpan = document.createElement("span");
			stubcatFormSpan.id = "stubcatForm";
			stubcatFormSpan.innerHTML = "<span id='stubcatForm'><input type='text' onkeyup='lookupStubcat()' id='stubcatInput' /><button id='stubcatOK' style='margin-left: .2em;' onclick='executeStubcatChange()'>OK</button><button style='margin-left: .2em;' onclick='cancelStubcat()'>Cancel</button></span>";
			document.getElementById("stubcatDiv").innerHTML = "";
			document.getElementById("stubcatDiv").style.width = "260px";
			document.getElementById("stubcatDiv").appendChild(stubcatFormSpan);
		}
		stubcatDiv.appendChild(cathotlink);
		stubtemp.parentNode.insertBefore(stubcatDiv, stubtemp.nextSibling);
	}
}
 
function lookupStubcat() {
	if (document.getElementById("stubcatInput").value != "") {
		if (window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
		} else {
			var req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		req.onreadystatechange = function() {
			if (req.readyState == 4 && req.status == 200) {
				var memberList = [];
				var jsonOutput = eval('(' + req.responseText + ')');
				var cmembers = jsonOutput.query.categorymembers;
				for (var memberObj in cmembers) {
					memberList.push(cmembers[memberObj].title);
				}
				var inputted = document.getElementById("stubcatInput").value;
				var checked = false;
				var preg = new RegExp("Category:" + inputted + "-related stubs", "i");
				for (var member in memberList) {
					if (memberList[member].match(preg) != null) {
						document.getElementById("stubcatInput").style.backgroundColor = "#ddeede";
						checked = true;
					}
				}
				if (!checked) {
					document.getElementById("stubcatInput").style.backgroundColor = "#f4d2cc";
				}
			}
		}
		req.open("GET", wgServer + wgScriptPath + "/api.php?action=query&format=json&list=categorymembers&cmlimit=500&cmtitle=Category:Articles%20needing%20expansion", true);
		req.send(null);
	}
}
 
function executeStubcat() {
	if (window.XMLHttpRequest) {
		var req = new XMLHttpRequest();
	} else {
		var req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200) {
			var inputted = document.getElementById("stubcatInput").value;
			var curText = req.responseText;
			curText = curText.replace(/{{stub}}/i, "{{stub|" + inputted + "}}");
			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;
				}
			if (window.XMLHttpRequest) {
				var exreq = new XMLHttpRequest();
			} else {
				var exreq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			function bottest() {
				if (wgUserGroups.indexOf("bot") == -1) {
					return "";
				} else {
					return "&bot";
				}
			}
			var editToken = encodeURIComponent(gettoken_api());
			exreq.open("POST", wgServer + wgScriptPath + "/api.php?action=edit&title=" + encodeURIComponent(wgPageName) + bottest() + "&text=" + encodeURIComponent(curText) + "&token=" + editToken + "&summary=" + encodeURIComponent("added stub type [[:Category:" + inputted + "-related stubs|" + inputted + "]] using [[User:Blue/scripts/hotstubcat.js|HotStubCat]]"), true);
			exreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			exreq.send(null);
			window.location.reload();
		}
	}
	req.open("GET", wgServer + wgScriptPath + "/index.php?title=" + wgPageName + "&action=raw", true);
	req.send(null);
}

function executeStubcatChange() {
	if (window.XMLHttpRequest) {
		var req = new XMLHttpRequest();
	} else {
		var req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = function() {
		if (req.readyState == 4 && req.status == 200) {
			var inputted = document.getElementById("stubcatInput").value;
			var curText = req.responseText;
			var stLoc = curText.search(/{{stub/i);
			for (var j = stLoc; j < curText.length; j++) {
				if (curText.slice(j, j + 2) == "}}") {
					var newText = curText.slice(stLoc, j + 2);
					var curCat = newText.slice(7, newText.length-2);
				}
			}
			curText = curText.replace(newText, "{{stub|" + inputted + "}}");
			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;
				}
			if (window.XMLHttpRequest) {
				var exreq = new XMLHttpRequest();
			} else {
				var exreq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			function bottest() {
				if (wgUserGroups.indexOf("bot") == -1) {
					return "";
				} else {
					return "&bot";
				}
			}
			var editToken = encodeURIComponent(gettoken_api());
			exreq.open("POST", wgServer + wgScriptPath + "/api.php?action=edit&title=" + encodeURIComponent(wgPageName) + bottest() + "&text=" + encodeURIComponent(curText) + "&token=" + editToken + "&summary=" + encodeURIComponent("changed stub type [[:Category:" + curCat + "-related stubs|" + curCat + "]] to [[:Category:" + inputted + "-related stubs|" + inputted + "]] using [[User:Blue/scripts/hotstubcat.js|HotStubCat]]"), true);
			exreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			exreq.send(null);
			window.location.reload();
		}
	}
	req.open("GET", wgServer + wgScriptPath + "/index.php?title=" + wgPageName + "&action=raw", true);
	req.send(null);
}

function cancelStubcat() {
	document.getElementById("stubcatDiv").parentNode.removeChild(document.getElementById("stubcatDiv"));
	var stubtemp = getElementsByClassName(document.getElementById('bodyContent'),"*","stubtemplate");
	if (stubtemp) stubtemp = stubtemp[0]; if (!stubtemp) return false;
	if (stubtemp.getElementsByTagName("i")[0].innerHTML == "This  article is a <b>stub.</b>") { // Uncategorized
		var stubcatDiv = document.createElement("div");
		stubcatDiv.id = "stubcatDiv";
		stubcatDiv.style.backgroundColor = "#f9f9f9";
		stubcatDiv.style.borderLeft = "1px solid #aaa";
		stubcatDiv.style.borderRight = "1px solid #aaa";
		stubcatDiv.style.borderBottom = "1px solid #aaa";
		stubcatDiv.style.padding = ".3em";
		stubcatDiv.style.margin = "0em auto .4em auto";
		stubcatDiv.style.textAlign = "center";
		stubcatDiv.style.width = "100px";
		stubtemp.style.marginBottom = "0";
		var cathotlink = document.createElement("span");
		cathotlink.innerHTML = "<a style='color: #0645AD; cursor: pointer'>categorize</a>";
		cathotlink.onclick = function() {
			var stubcatFormSpan = document.createElement("span");
			stubcatFormSpan.id = "stubcatForm";
			stubcatFormSpan.innerHTML = "<span id='stubcatForm'><input type='text' onkeyup='lookupStubcat()' id='stubcatInput' /><button id='stubcatOK' style='margin-left: .2em;' onclick='executeStubcat()'>OK</button><button style='margin-left: .2em;' onclick='cancelStubcat()'>Cancel</button></span>";
			document.getElementById("stubcatDiv").innerHTML = "";
			document.getElementById("stubcatDiv").style.width = "260px";
			document.getElementById("stubcatDiv").appendChild(stubcatFormSpan);
		}
		stubcatDiv.appendChild(cathotlink);
		stubtemp.parentNode.insertBefore(stubcatDiv, stubtemp.nextSibling);
	} else { // Already categorized
		var stubcatDiv = document.createElement("div");
		stubcatDiv.id = "stubcatDiv";
		stubcatDiv.style.backgroundColor = "#f9f9f9";
		stubcatDiv.style.borderLeft = "1px solid #aaa";
		stubcatDiv.style.borderRight = "1px solid #aaa";
		stubcatDiv.style.borderBottom = "1px solid #aaa";
		stubcatDiv.style.padding = ".3em";
		stubcatDiv.style.textAlign = "center";
		stubcatDiv.style.width = "100px";
		stubcatDiv.style.margin = "0em auto .4em auto";
		stubtemp.style.marginBottom = "0";
		var cathotlink = document.createElement("span");
		cathotlink.innerHTML = "<a style='color: #0645AD; cursor: pointer'>change category</a>";
		cathotlink.onclick = function() {
			var stubcatFormSpan = document.createElement("span");
			stubcatFormSpan.id = "stubcatForm";
			stubcatFormSpan.innerHTML = "<span id='stubcatForm'><input type='text' onkeyup='lookupStubcat()' id='stubcatInput' /><button id='stubcatOK' style='margin-left: .2em;' onclick='executeStubcatChange()'>OK</button><button style='margin-left: .2em;' onclick='cancelStubcat()'>Cancel</button></span>";
			document.getElementById("stubcatDiv").innerHTML = "";
			document.getElementById("stubcatDiv").style.width = "260px";
			document.getElementById("stubcatDiv").appendChild(stubcatFormSpan);
		}
		stubcatDiv.appendChild(cathotlink);
		stubtemp.parentNode.insertBefore(stubcatDiv, stubtemp.nextSibling);
	}
}
 
addOnloadHook(setupForStubcat);

//</nowiki>