Jump to content

User:Terasail/COI Request Tool.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Terasail (talk | contribs) at 15:32, 30 January 2021 (Link change). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
var COIResponse = [
["ntmp", "speechBubbleAdd", "Comment", "Comment (No template)"],
["d", "checkAll", "Done", "Done"],
["pd", "check", "Partly done", "Partly done:"],
["a", "clock", "Already Done", "Already Done"],
["q", "helpNotice", "Question", "Question:"],
["g", "edit", "Go ahead", "Go ahead: I have reviewed these proposed changes and suggest that you go ahead and make the proposed changes to the page."],
["note", "ellipsis", "Note", "Note:"],
["n", "notice", "Not done", "Not done:"],
["nfn", "notice", "Not done for now", "Not done for now:"],
["c", "userGroup", "Not done – Needs consensus", "Not done for now: Please establish a consensus with editors engaged in the subject area before using the {{Request edit}} template for this proposed change."],
["udp","undo", "Partly undone", "Undone: This request has been partially undone."], 
["ud", "undo", "Undone", "Undone: This request has been undone."]];

function executeCOI(currentBox, selectedReply, inputText, answered) {
	if (confirm("Confirm in order to reply to this edit request.")) {
		currentBox.getElementsByTagName('tr')[2].remove();
		var infoBox = new OO.ui.MessageWidget( {
			icon: 'pageSettings',
			type: 'notice',
			label: 'Processing request — Edit request starting, getting section data to edit.'
		});
		var firstRow = currentBox.children[1].children[0];
		firstRow.innerHTML = "";
		progressBar = new OO.ui.ProgressBarWidget( {
			progress: false
		});
		$(firstRow).append(progressBar.$element);
		$(firstRow).append(infoBox.$element);
		progressBar.$element[0].style = "margin:auto";
		infoBox.$element[0].style = "margin:10px auto 0px; max-width:50em; text-align:left";
		var header = "";
		var curElement = currentBox.parentNode;
		do {
			curElement = curElement.previousElementSibling;
			if (curElement.getElementsByClassName("mw-headline").length == 1) {
				header = curElement.getElementsByClassName("mw-headline")[0].id;
			}
		}
		while (header == "");
		var api = new mw.Api();
		api.get( {
			action: "parse",
			page: mw.config.values.wgPageName,
			prop: "sections",
		}).done(function(data) {
			infoBox.setLabel("Processing request — Making changes to the edit request");
			var sections = data.parse.sections;
			var sectionNum = -1;
			var offset = 0;
			for (j = 0; j < sections.length; j++) {
				if (sections[j].anchor == header) {
					sectionNum = j + 1;
					sectionNum -= offset;
				}
				if (sections[j].fromtitle != mw.config.values.wgPageName) {
					offset++;
				}
			}
			infoBox.setLabel("Processing request — Getting information from section: " + sectionNum + ".");
			api.get( {
				action: "parse",
				page: mw.config.values.wgPageName,
				prop: "wikitext",
				section: sectionNum
			}).done( function(data) {
				infoBox.setLabel("Processing request — Making changes to the edit request.");
				var wikitext = data.parse.wikitext["*"];
				var tempStr = "";
				if (answered || selectedReply == "CLOSEREQ") {
					tempStr = "|A";
				}
				var templateStart = wikitext.search("{{request edit");
				if (templateStart == -1) {
					templateStart = wikitext.search("{{Request edit");
				} 
				if (templateStart == -1) {
					infoBox.setLabel('Failed to find {{Request edit}} template, please report to "User talk:Terasail/COI Request Tool"');
					infoBox.setType("error");
				} else {
					var beforeTemplate = wikitext.slice(0, templateStart);
					wikitext = wikitext.slice(templateStart);
					var afterTemplate = wikitext.slice(wikitext.search("}}") + 2);
					wikitext = beforeTemplate + "{{Requested edit" + tempStr + "}}" + afterTemplate;
					var editSummary = "/* " + header.replaceAll("_", " ") + " */ ";
					if (selectedReply == "REMOVE") {
						wikitext = "";
						editSummary = "Removed edit request";
						selectedReply = -1;
					} else if (selectedReply == "CLOSEREQ") {
						editSummary += "Closed edit request";
						selectedReply = -1;
					} else {
						if (selectedReply == 0) { //Do not add response template
							wikitext += "\n:" + inputText.value;
						} else {
							wikitext += "\n:{{subst:ECOI|" + COIResponse[selectedReply][0] + "}}";
							if (inputText != "") {
								wikitext += " " + inputText.value;
							}
						}
						wikitext += " ~~~~";
						editSummary += COIResponse[selectedReply][2];
					}
					editSummary += " ([[User:Terasail/COI_Request_Tool|COI Request Tool]])";
					if (selectedReply != -1 && infoBox.type != "error") {
						infoBox.setType("success");
						infoBox.setLabel("Processing '" + COIResponse[selectedReply][2] + "' request — Saving changes to the talk page.");
					}
					console.log(sectionNum);
					console.log(editSummary);
					console.log(wikitext);
					api.postWithEditToken({
						action: 'edit',
						title: mw.config.values.wgPageName,
						text: wikitext,
						section: sectionNum,
						summary: editSummary
					}).done(function(result) {
						window.location = "https" + "://en.wikipedia.org/w/index.php?title=" + mw.config.values.wgPageName + "&type=revision&diff=cur&oldid=prev";
					});
				}
			});
		});
	}
}

function loadButtons(currentBox) {
	$(currentBox).append('<tr><td colspan=2 style="padding-bottom:10px"><div style="display: flex; justify-content: center;"></div></td></tr><tr style="display:none"><td colspan=2 style="padding-bottom:10px; text-align:center"></td></tr>');
	var firstRow = currentBox.children[1].children[0];
	//Create dropdown menu
	var dropMenu = new OO.ui.DropdownWidget( {
		label: "Select reply option -  Add additional text below",
		menu: {items: []}
	});
	COIResponse.forEach(function(item) {
		var newOption = new OO.ui.MenuOptionWidget({
			label: item[3],
			icon: item[1]
		});
		dropMenu.menu.addItems(newOption);
	});
	dropMenu.$element[0].style = "text-align:left; margin:0px";
	$(currentBox.children[2].children[0]).append(dropMenu.$element);
	//Create input box
	var inputText = new OO.ui.MultilineTextInputWidget({autosize: true, label: "'~~" + "~~" + "' is automatically added"});
	inputText.$element[0].style = "margin:auto";
	$(currentBox.children[2].children[0]).append(inputText.$element);
	//Toggle answer button
	var answerB = new OO.ui.ToggleButtonWidget( { 
		icon: "check",
		label: "Close",
		value: true
	});
	answerB.on("click", function() {
		if (answerB.value == true) {
			answerB.setIcon("check");
		} else {
			answerB.setIcon("close");
		}
	});
	//Done button
	var doneB = new OO.ui.ButtonWidget( { 
		icon: "checkAll",
		flags: ["primary", "progressive"],
		label: "Done"
	});
	doneB.on("click", function() {
		if (doneB.getLabel() == "Submit") {
			dropMenu = dropMenu.getLabel();
			for (k = 0; k < COIResponse.length; k++) {
				if (COIResponse[k][3] == dropMenu) {
					executeCOI(currentBox, k, inputText, answerB.value);
				}
			}
		} else {
			executeCOI(currentBox, 1, inputText, answerB.value);
		}
	});
	$(firstRow.children[0]).append(doneB.$element);
	//Go ahead button
	var gaB = new OO.ui.ButtonWidget( { 
		icon: "edit",
		label: "Go ahead"
	});
	$(firstRow.children[0]).append(gaB.$element);
	gaB.on("click", function() {
		executeCOI(currentBox, 5, inputText, answerB.value);
	});
	//Consensus button
	var conB = new OO.ui.ButtonWidget( { 
		icon: "userGroup",
		label: "WP:Consensus"
	});
	$(firstRow.children[0]).append(conB.$element);
	conB.on("click", function() {
		executeCOI(currentBox, 9, inputText, answerB.value);
	});
	//Responses button
	var responseB = new OO.ui.ButtonWidget( { 
		icon: "add",
		label: "Other"
	});
	$(firstRow.children[0]).append(responseB.$element);
	responseB.on("click", function() {
		if (currentBox.children[2].style.display == "") {
			responseB.setIcon("add");
			currentBox.children[2].style = "display:none";
			doneB.setLabel("Done");
		} else {
			responseB.setIcon("subtract");
			currentBox.children[2].style = "";
			doneB.setLabel("Submit");
		}
	});
	$(firstRow.children[0]).append(answerB.$element);
	//Close request
	var closeB = new OO.ui.ButtonWidget( { 
		icon: "unFlag",
		invisibleLabel: true
	});
	closeB.$element[0].style = "margin:10px 10px 0px";
	$(currentBox.children[0].children[0]).append(closeB.$element);
	closeB.on("click", function() {
		executeCOI(currentBox, "CLOSEREQ", "", answerB.value)
	});
	//Remove request
	var removeB = new OO.ui.ButtonWidget( { 
		icon: "trash",
		flags: ["primary", "destructive"],
		invisibleLabel: true
	});
	removeB.$element[0].style = "margin:10px";
	$(currentBox.children[0].children[0]).append(removeB.$element);
	removeB.on("click", function() {
		executeCOI(currentBox, "REMOVE", "", answerB.value)
	});
}

var cats = mw.config.values.wgCategories;
var runCode = false;
cats.forEach(function(item) {
	if (item == "Requested edits") {
		runCode = true;
	}
});
if (runCode) {
	mw.loader.using(["oojs-ui-core", "oojs-ui-widgets"]).done(function() {
		mw.loader.load(["oojs-ui.styles.icons-alerts", "oojs-ui.styles.icons-interactions", "oojs-ui.styles.icons-moderation", "oojs-ui.styles.icons-user", "oojs-ui.styles.icons-editing-core"]);
		var banners = document.getElementsByClassName("plainlinks tmbox tmbox-notice");
		for (i = 0; i < banners.length; i++) {
			var edReqLink = banners[i].getElementsByTagName("A");
			if (edReqLink[0].innerHTML == "edit request"){
				var currentBox = banners[i].children[0];
				var replyB = new OO.ui.ButtonWidget( { 
					icon: "edit",
					flags: "progressive",
					invisibleLabel: true
				});
				replyB.$element[0].style = "margin:5px 0px";
				$(currentBox.children[0].children[0]).append(replyB.$element);
				replyB.on("click", function() {
					currentBox.parentElement.setAttribute("class", "plainlinks tmbox tmbox-notice");
					var img = currentBox.children[0].children[0].children[0]
					img.width = 53;
					img.height = 53;
					img.src = "/media/wikipedia/commons/f/f4/Info_talk.png"
					img.scrset = "";
					currentBox.children[0].children[1].innerHTML = '<div style="text-align:center; font-size:125%">Please read the instructions for the parameters used by this template for accepting and declining them, and review the request below and make the edit if it is well sourced, neutral, and follows other Wikipedia guidelines and policies.</div>'
					replyB.$element.remove();
					loadButtons(currentBox);
				});
			} else if (edReqLink.length >= 4) {
				if (edReqLink[3].innerHTML == "requested edits") {
					loadButtons(banners[i].children[0]);
				}
			}
		}
	});
}
//</nowiki>