User:Terasail/Edit Request Tool.js: Difference between revisions

Content deleted Content added
Update
Account for Template:Sudo
 
(6 intermediate revisions by the same user not shown)
Line 1:
//*<nowiki>
Edit Request Tool
/*jshint esversion: 6*/
Created by: Terasail
var editRequestBoxes = document.getElementsByClassName("plainlinks tmbox tmbox-notice editrequest");
*/
var pageWatchers = "There are currently ";
var watchStatus;
var dataERT;
var userSignauteERT = "";
var pageNameERT = mw.config.get('wgPageName');
var editRequestBoxes = $('.editrequest');
var encodePageName = encodeURIComponent(pageNameERT);
var emptyTalkParamseditRequests = null[];
for (let i = 0; i < editRequestBoxes.length; i++) {
pageNameERT = pageNameERT.replace(/_/g, " ");
if (typeof(editRequestBoxes[i].attributes['data-origlevel']) != 'undefined') {
if (editRequestBoxes[i].id == "") {
$(editRequestBoxes[i].children[0].children[0].children[0]).append('<div class="response-cell-ert" style="text-align:center;"></div>');
} else {
$(editRequestBoxes[i].children[0]).append('<tr><td colspan="2" class="response-cell-ert" style="text-align:center;"></td></tr>');
}
editRequests.push(editRequestBoxes[i]);
}
}
 
if (editRequests.length > 0) {
function postEdit(wikitext, editSummary, secIndx, changeWatch, watchlist) {
mw.loader.using(["oojs-ui-core", "oojs-ui-widgets", "oojs-ui-windows"]).done(function() {
let apiParams = {
mw.loader.load(["oojs-ui.styles.icons-interactions", "oojs-ui.styles.icons-moderation", "oojs-ui.styles.icons-user", "oojs-ui.styles.icons-content", "oojs-ui.styles.icons-editing-core", "oojs-ui.styles.icons-editing-advanced"]);
action: 'edit',
loadERTool();
title: pageNameERT,
$.getJSON("https://en.wikipedia.org/w/index.php?title=User:Terasail/Edit_Request_Tool.json&action=raw&ctype=text/json", function (newData) {
text: wikitext,
dataERT = newData;
section: secIndx,
});
summary: editSummary,
ApiGetERT({
watchlist: changeWatch
action: "parse",
};
title: mw.config.get("wgPageName"),
let reloadURL = "/w/index.php?title=" + encodePageName + "&type=revision&diff=cur&oldid=prev";
text: "~~~~",
if (changeWatch == "watch") {
pst: "true",
apiParams.watchlistexpiry = watchlist;
disablelimitreport: "true",
}
disableeditsection: "true",
new mw.Api().postWithEditToken(apiParams).done(function () {
preview: "true"
if (emptyTalkParams != null) {
}).then(function(data){
new mw.Api().postWithEditToken(emptyTalkParams).done(function (result) {
userSignauteERT = data.parse.text["*"].replaceAll(/([^]+(?=<p>)<p>|<\/div>)/g, " ");
window.___location = reloadURL;
});
});
});
} else {
window.___location = reloadURL;
}
});
}
 
async function loadERTool() {
function execute(responseTable, replyOption, inputText, answered, boxType, targets, changeWatch, watchlist) {
// Get page watchers, visitors and user watch status.
OO.ui.confirm("Confirm in order to reply to this edit request.").done(function (confirmed) {
let watchStatus = [];
if (confirmed) {
let watchQuery = await ApiGetERT({
//Create label box & remove action buttons
action: "query",
let firstRow = responseTable.children[1].children[0];
prop: "info",
firstRow.innerHTML = "";
pageids: mw.config.get("wgArticleId"),
responseTable.children[5].children[0].remove();
inprop: "watchers|visitingwatchers|watched",
let infoBox = new OO.ui.MessageWidget({
format: "json"
icon: 'pageSettings',
});
type: 'notice',
let watchData = watchQuery.query.pages[mw.config.get("wgArticleId")];
label: 'Processing request — Edit request starting, getting section data to edit.'
let watched = watchData.watched;
});
let expiry = watchData.watchlistexpiry;
infoBox.$element[0].style = "margin:5px 0; max-width:50em";
if (expiry) {
$(firstRow).append(infoBox.$element);
watched = Math.ceil((new Date(expiry).getTime() - Date.now()) / 1000 / 60 / 60 / 24) + " days";
//Create loading bar
}
let progressBar = new OO.ui.ProgressBarWidget({
watchStatus.push(watchData.watchers || "less than 30", watchData.visitingwatchers || "<30", watched);
progress: false
//Increment through all edit requests & add respond button
});
for (let i = 0; i < editRequests.length; i++) {
$(firstRow).append(progressBar.$element);
let responseCell = $('.response-cell-ert')[i];
//Set preview for output
let smallButton = false;
if (replyOption[0] != "") {//Don't preview a non-response
if (responseCell.tagName == "DIV") {
showOutput(inputText, replyOption, responseTable.children[4].children[0], boxType);
smallButton = true;
}
}
//Find header
let respondButton = new OO.ui.ButtonWidget({
let header = "";
icon: "edit",
let curElement = responseTable.parentNode;
label: "Respond",
do {
flags: "progressive",
curElement = curElement.previousElementSibling;
title: "Open the response menu for this request",
if (curElement.getElementsByClassName("mw-headline").length == 1) {
invisibleLabel: smallButton,
header = curElement.getElementsByClassName("mw-headline")[0].id;
}).on("click", function() {
}
loadERTResponse(editRequests[i], respondButton, watchStatus);
}
respondButton.setDisabled(true);
while (header == "");
});
new mw.Api().get({
respondButton.$element[0].style = "margin:5px";
action: "parse",
$(responseCell).append(respondButton.$element);
page: pageNameERT,
}
prop: "sections"
}
}).done(function (data) {
 
infoBox.setLabel("Processing request — Making changes to the edit request");
function loadERTResponse(editRequest, respondButton, watchStatus) {
let editTemplate = "{{Edit " + boxType + "-protected";
let boxType = editRequest.dataset.origlevel;
if (answered) {
boxType = boxType.replace("full", "fully");
answered = "yes";
$('<table style="border:1px solid #A2A9B1; border-radius:2px; padding:10px 16px 0; margin:auto; max-width:55em; width:100%; clear:both;"><tr><td><div style="font-style:italic; margin-left:1em;">There are currently ' + watchStatus[0] + ' users watching this page (' + watchStatus[1] + ' have viewed recent edits).</div><div>Quick options:</div></td></tr><tr style="display: flex; justify-content: center;"><td class="response-quick"></td></tr><tr><td>Custom response:</td></tr><tr style="text-align:center;"><td class="response-custom"></td></tr><tr style="background:#F6F6F6;"><td class="response-preview" style="display:none;"><div>Preview:</div><div></div></td></tr><tr style="display: flex; justify-content: right;"><td class="response-controls"></td></tr></table>').insertAfter(editRequest);
} else {
let responseBox = editRequest.nextElementSibling;
answered = "no";
let responseQuick = $(responseBox).find('.response-quick')[0];
}
let responseCustom = $(responseBox).find('.response-custom')[0];
for (let c3 = 0; c3 < targets.length; c3++) {
let responsePreview = $(responseBox).find('.response-preview')[0];
editTemplate += "|" + targets[c3];
let responseControls = $(responseBox).find('.response-controls')[0];
}
let protections = Object.entries(dataERT.protections);
editTemplate += "|answered=" + answered;
let responses = Object.entries(dataERT.response);
let sections = data.parse.sections;
let quickResponses = Object.entries(dataERT.quickResponse);
let secIndx = sections[0];
let nonResponses = dataERT.nonResponse;
for (let j = 0; j < sections.length; j++) {
//Create type change dropdown
if (sections[j].anchor == header) {
let tcOptions = [];
secIndx = parseInt(sections[j].index);
for (let i = 0; i < protections.length; i++) {
}
tcOptions.push({data: protections[i][0], label: protections[i][1][0]});
}
}
new mw.Api().get({
let typeChange = new OO.ui.DropdownInputWidget({
action: "parse",
value: boxType,
page: pageNameERT,
options: tcOptions
section: secIndx,
});
prop: "wikitext|revid"
}) typeChange.doneon("change", function (data) {
submitB.setDisabled(false);
let wikitext = data.parse.wikitext["*"];
});
let newRev = data.parse.revid;
typeChange.$element[0].style = "text-align:left; margin:auto";
wikitext = wikitext.replace(/{{ *([SETFI]PER|Edit([ -]?[A-Z]+[ -]?|[- ])Protected)\s*[^}}]*/i, editTemplate);
$(responseCustom).append(typeChange.$element);
let editSummary = "/* " + header.replaceAll("_", " ") + " */ " + replyOption[2] + " ([[User:Terasail/Edit_Request_Tool|Edit Request Tool]])";
//Create target page list
if (replyOption[1] != "Close") {
let boxLinks = editRequest.getElementsByClassName("mbox-text")[0].getElementsByClassName("external text");
wikitext = wikitext.trim() + "\n:";
let pageTargets = [];
if (replyOption[0] != "") {
if (boxLinks.length > 0) {//Open request
wikitext += "{{subst:" + dataERT.protections[boxType][1] + replyOption[0] + "}} ";
for (let c1 = 0; c1 < boxLinks.length; c1++) {
}
if (boxLinks[c1].parentElement.tagName == "LI" || boxLinks[c1].parentElement.tagName == "B") {
if (inputText != "") {
pageTargets[pageTargets.length] = boxLinks[c1].innerHTML;
wikitext += inputText.replaceAll(/\s*~~~~\s*/g, "") + " ";
}
}
}
wikitext += "~~~~";
} else {//Closed request
if (replyOption[1] == "Remove") {
boxLinks = editRequest.getElementsByClassName("mbox-text")[0].getElementsByTagName("A");
wikitext = "";
for (let c2 = 1; c2 < boxLinks.length; c2++) {
/*if (inputText == "EmptyNotice") {
pageTargets[pageTargets.length] = boxLinks[c2].title;
let commentERT = document.getElementById(header).dataset.mwComment;
}
if (typeof (commentERT) != 'undefined' && commentERT.search(/-(?=[0-9]{14}","type)/) != -1) {
if (pageTargets.length == 0) {
let commenter = commentERT.slice(commentERT.indexOf('"name"') + 10, commentERT.search(/-(?=[0-9]{14}","type)/));
pageTargets = mw.config.get("wgPageName").replace(/(_talk|Talk:)/,"").replaceAll("_", " ");
emptyTalkParams = dataERT.emptyNotice;
}
emptyTalkParams.title = "User talk:" + commenter;
}
emptyTalkParams.sectiontitle += pageNameERT;
let targetPages = new OO.ui.TagMultiselectWidget({
emptyTalkParams.text = talkParams.text.replace("```", pageNameERT);
placeholder: 'Target Pages',
emptyTalkParams.summary = talkParams.summary.replace("```", pageNameERT);
allowArbitrary: true,
console.log(emptyTalkParams);
selected: pageTargets
}
});
}*/
targetPages.on("change", function () {
editSummary = editSummary.replace(/[^]+\*\/ /, "");
submitB.setDisabled(false);
}
});
}
targetPages.$element[0].style = "text-align:left; margin:5px auto";
infoBox.setType("success");
$(responseCustom).append(targetPages.$element);
infoBox.setLabel("Processing request — Saving changes to the talk page.");
//Create dropdown menu
if (newRev == mw.config.values.wgRevisionId) {
let dropMenu = new OO.ui.DropdownWidget({
postEdit(wikitext, editSummary, secIndx, changeWatch, watchlist);
label: "Select reply option - Add additional text below",
} else {
menu: {items: []}
OO.ui.confirm("There has been a new revision to the page, do you wish to continue?").done(function (revCon) {
});
if (revCon) {
for (let count = 0; count < responses.length; count++) {
postEdit(wikitext, editSummary, secIndx, changeWatch, watchlist);
let newOption = new OO.ui.MenuOptionWidget({
}
label: responses[count][0],
});
icon: responses[count][1][1]
}
});
});
dropMenu.menu.addItems([newOption]);
});
}
}
responses = dataERT.response;
});
dropMenu.$element[0].style = "text-align:left; margin:0px";
$(responseCustom).append(dropMenu.$element);
dropMenu.on("labelChange", function () {
submitB.setDisabled(false);
previewERT(inputText, responses[dropMenu.getLabel()], responsePreview, typeChange.value);
});
//Create input box
let inputText = new OO.ui.MultilineTextInputWidget({autosize: true, rows: 4, label: "Additional text"});
inputText.$element[0].style = "margin:5px auto";
$(responseCustom).append(inputText.$element);
inputText.on("change", function (newText) {
previewERT(inputText, responses[dropMenu.getLabel()], responsePreview, typeChange.value);
});
//Create top horizontal layout
let hzLayoutT = new OO.ui.HorizontalLayout();
//Create firstrow fieldset
let fieldsetT = new OO.ui.FieldsetLayout();
fieldsetT.addItems([new OO.ui.FieldLayout(new OO.ui.Widget({content: [hzLayoutT]}), {align: 'top'})]);
$(responseQuick).append(fieldsetT.$element);
//Remove button
let remove = new OO.ui.ButtonWidget({
icon: "trash",
flags: ["primary", "destructive"],
invisibleLabel: true,
title: "Remove the section!"
});
remove.on("click", function () {
$(responseBox).find("tr").each(function(_, row) {
if ($(row).find('.response-quick').length == 0) {
row.remove();
}
});
hzLayoutT.clearItems();
//Create deletion options
let remSec = new OO.ui.ButtonWidget({//RemoveSection
icon: "trash",
flags: ["primary", "destructive"],
label: "Remove section",
title: "Remove the entire section!"
});
remSec.on("click", function () {
saveResponseERT([editRequest, responseQuick, responsePreview, responseControls], nonResponses.Remove, "", null, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
});
hzLayoutT.addItems([remSec]);
hzLayoutT.addItems([cancelB]);
});
hzLayoutT.addItems([remove]);
//Open & Close button
if (editRequest.attributes[2].localName != "data-origlevel") {
let closeB = new OO.ui.ButtonWidget({
icon: "unFlag",
invisibleLabel: true,
title: "Mark as answered"
});
closeB.on("click", function () {
saveResponseERT([editRequest, responseQuick, responsePreview, responseControls], nonResponses.Close, "", true, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
});
hzLayoutT.addItems([closeB]);
} else {
let openB = new OO.ui.ButtonWidget({
icon: "flag",
invisibleLabel: true,
title: "Mark as unanswered"
});
openB.on("click", function () {
saveResponseERT([editRequest, responseQuick, responsePreview, responseControls], nonResponses.Open, "", false, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
});
hzLayoutT.addItems([openB]);
}
//Create quick response buttons
for (let i = 0; i < quickResponses.length; i++) {
let newButton = new OO.ui.ButtonWidget({
label: quickResponses[i][1][0],
flags: quickResponses[i][1][1],
title: quickResponses[i][1][2]
});
newButton.on("click", function () {
saveResponseERT([editRequest, responseQuick, responsePreview, responseControls], responses[quickResponses[i][0]], "", toggleAns.selected, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
});
hzLayoutT.addItems([newButton]);
}
//Toggle answer button
let toggleAns = new OO.ui.CheckboxInputWidget({selected: true});
hzLayoutT.addItems([toggleAns, new OO.ui.LabelWidget({label: "Answered"})]);
//Create lastrow horizontal layout
let hzLayoutB = new OO.ui.HorizontalLayout();
//Create lastrow fieldset
let fieldsetB = new OO.ui.FieldsetLayout();
fieldsetB.addItems([new OO.ui.FieldLayout(new OO.ui.Widget({content: [hzLayoutB]}), {align: 'top'})]);
$(responseControls).append(fieldsetB.$element);
//Cancel response button
let cancelB = new OO.ui.ButtonWidget({
icon: "cancel",
flags: ["destructive"],
label: "Cancel",
framed: false,
title: "Cancel the response & close menu"
});
cancelB.on("click", function () {
respondButton.setDisabled(false);
responseBox.remove();
});
hzLayoutB.addItems([cancelB]);
//Watchlist dropdown
let watchOptions = [{data: "infinite", label: "Permanent"}, {data: "1 day", label: "1 day"}, {data: "3 days", label: "3 days"}, {data: "1 week", label: "1 week"}, {data: "1 month", label: "1 month"}];
let watchValue = "infinite";
if (!!watchStatus[2]) {
watchOptions.unshift({data: "nochange", label: watchStatus[2]});
watchValue = "nochange";
}
let watchlistLayout = new OO.ui.HorizontalLayout();
let watchlistDropdown = new OO.ui.DropdownInputWidget({
value: watchValue,
options: watchOptions,
disabled: (watchStatus[2] == undefined)
});
watchlistLayout.addItems([watchlistDropdown]);
//Watchlist checkbox & label
let watchlistCheckbox = new OO.ui.CheckboxInputWidget({
selected: (watchStatus[2] != undefined)
}).on("change", function (newStatus) {
watchlistDropdown.setDisabled(!newStatus);
});
let watchlistLabel = new OO.ui.LabelWidget({label: "Watch this page"}).on("change", function (newStatus) {
 
});
hzLayoutB.addItems([watchlistCheckbox, watchlistLabel, watchlistLayout]);
//Submit response button
let submitB = new OO.ui.ButtonWidget({
icon: "checkAll",
flags: ["primary", "progressive"],
label: "Submit",
title: "Submit the response",
disabled: true
});
submitB.on("click", function () {
let newResponse = responses[dropMenu.getLabel()];
let newText = inputText.value;
if (typeof(newResponse) == "undefined") {
newText = "";
newResponse = nonResponses.ChangeLevel; //Assume that it is a template change
let newPageTargets = [];
targetPages.items.forEach(function(item) {
newPageTargets.push(item.label);
});
if (pageTargets.toString() != newPageTargets.toString()) { //Check if the page targets were changed instead
newResponse = nonResponses.ChangeTarget;
}
}
saveResponseERT([editRequest, responseQuick, responsePreview, responseControls], newResponse, newText, toggleAns.selected, typeChange.value, targetPages.getValue(), watchlistCheckbox.selected, watchlistDropdown.value);
});
hzLayoutB.addItems([submitB]);
}
 
function addButtonspreviewERT(currentBoxinputText, replyButtonreplyOption, tableRow, template) {
var restTransform = "https://en.wikipedia.org/api/rest_v1/transform/wikitext/to/html/" + encodeURIComponent(mw.config.get('wgPageName'));
let boxType = currentBox.parentElement.dataset.origlevel;
let preview = inputText.value;
boxType = boxType.replace("full", "fully");
template = dataERT.protections[template][1];
let tableElem = currentBox.parentElement;
if (typeof (replyOption) != "undefined") {
$('<table style="border:1px solid #A2A9B1; border-radius:2px; padding:10px 16px 0; margin:auto; max-width:55em; width:100%; clear:both;"><tr><td style="color:#808080"><div style="font-style:italic; margin-left:1em;">' + pageWatchers + '</div><div>Quick options:</div></td></tr><tr style="display: flex; justify-content: center;"><td></td></tr><tr><td style="color:#808080">Custom response:</td></tr><tr style="text-align:center;"><td></td></tr><tr style="background:#F6F6F6;"><td style="display:none;"><div style="color:#808080">Preview:</div><div></div></td></tr><tr style="display: flex; justify-content: right;"><td></td></tr></table>').insertAfter(tableElem);
preview = "{{" + template + replyOption[0] + "}} " + preview;
let responseTable = tableElem.nextElementSibling.children[0];
}
let firstRow = responseTable.children[1].children[0];
if (preview != "") {
let secondRow = responseTable.children[3].children[0];
preview = preview.replaceAll(/{{subst:/gi, "{{");
let thirdRow = responseTable.children[4].children[0];
$.post(restTransform, 'wikitext=' + encodeURIComponent(preview) + '&body_only=true',
let fourthRow = responseTable.children[5].children[0];
function (html) {
let protections = Object.entries(dataERT.protections);
if (inputText.value != "" || typeof (replyOption) != "undefined") {//Stops preview appearing with empty input box
let responses = Object.entries(dataERT.response);
tableRow.style = "padding:8px 1em 2px;";
let quickResponses = Object.entries(dataERT.quickResponse);
tableRow.children[1].innerHTML = html.replace("</p>", userSignauteERT);
let nonResponses = dataERT.nonResponse;
}
//Create type change dropdown
}
let tcOptions = [];
);
for (let i = 0; i < protections.length; i++) {
} else {
tcOptions.push({data: protections[i][0], label: protections[i][1][0]});
tableRow.style = "display:none;";
}
}
let typeChange = new OO.ui.DropdownInputWidget({
value: boxType,
options: tcOptions
});
typeChange.on("change", function () {
submitB.setDisabled(false);
});
typeChange.$element[0].style = "text-align:left; margin:auto";
$(secondRow).append(typeChange.$element);
//Create target page list
let boxLinks = currentBox.getElementsByClassName("mbox-text")[0].getElementsByClassName("external text");
let pageLinks = [];
if (boxLinks.length > 0) {//Open request
for (let c1 = 0; c1 < boxLinks.length; c1++) {
if (boxLinks[c1].parentElement.tagName == "LI" || boxLinks[c1].parentElement.tagName == "B") {
pageLinks[pageLinks.length] = boxLinks[c1].innerHTML;
}
}
} else {//Closed request
boxLinks = currentBox.getElementsByClassName("mbox-text")[0].getElementsByTagName("A");
for (let c2 = 1; c2 < boxLinks.length; c2++) {
pageLinks[pageLinks.length] = boxLinks[c2].title;
}
if (pageLinks.length == 0) {
pageLinks = mw.config.get("wgPageName").replace(/(_talk|Talk:)/,"");
}
}
let targetPages = new OO.ui.TagMultiselectWidget({
placeholder: 'Target Pages',
allowArbitrary: true,
selected: pageLinks
});
targetPages.on("change", function () {
submitB.setDisabled(false);
});
targetPages.$element[0].style = "text-align:left; margin:5px auto";
$(secondRow).append(targetPages.$element);
//Create dropdown menu
let dropMenu = new OO.ui.DropdownWidget({
label: "Select reply option - Add additional text below",
menu: {items: []}
});
for (let count = 0; count < responses.length; count++) {
let newOption = new OO.ui.MenuOptionWidget({
label: responses[count][0],
icon: responses[count][1][1]
});
dropMenu.menu.addItems([newOption]);
}
responses = dataERT.response;
dropMenu.$element[0].style = "text-align:left; margin:0px";
$(secondRow).append(dropMenu.$element);
dropMenu.on("labelChange", function () {
submitB.setDisabled(false);
showOutput(inputText, responses[dropMenu.getLabel()], thirdRow, typeChange.value);
});
//Create input box
let inputText = new OO.ui.MultilineTextInputWidget({autosize: true, rows: 4, label: "Additional text"});
inputText.$element[0].style = "margin:5px auto";
$(secondRow).append(inputText.$element);
inputText.on("change", function (newText) {
showOutput(inputText, responses[dropMenu.getLabel()], thirdRow, typeChange.value);
});
//Create top horizontal layout
let hzLayoutT = new OO.ui.HorizontalLayout();
//Create firstrow fieldset
let fieldsetT = new OO.ui.FieldsetLayout();
fieldsetT.addItems([new OO.ui.FieldLayout(new OO.ui.Widget({content: [hzLayoutT]}), {align: 'top'})]);
$(firstRow).append(fieldsetT.$element);
//Remove button
let remove = new OO.ui.ButtonWidget({
icon: "trash",
flags: ["primary", "destructive"],
invisibleLabel: true,
title: "Remove the section!"
});
remove.on("click", function () {
secondRow.style = "display:none";
thirdRow.style = "display:none";
fourthRow.style = "display:none";
responseTable.children[2].children[0].style = "display:none";
submitB.$element.remove();
hzLayoutT.clearItems();
//Create deletion options
let remSec = new OO.ui.ButtonWidget({//RemoveSection
icon: "trash",
flags: ["primary", "destructive"],
label: "Remove section",
title: "Remove the entire section!"
});
remSec.on("click", function () {
execute(responseTable, nonResponses.Remove, "", null, typeChange.defaultValue, targetPages.getValue(), "nochange", "");//temp
/*if (toggleESN.selected) {
execute(responseTable, nonResponses.Remove, "EmptyNotice", null, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
} else {
execute(responseTable, nonResponses.Remove, "", null, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
}*/
});
hzLayoutT.addItems([remSec]);
/*let toggleESN = new OO.ui.CheckboxInputWidget({selected: false});
hzLayoutT.addItems([toggleESN, new OO.ui.LabelWidget({label: "Give the user an empty section notice"})]);*/
hzLayoutT.addItems([cancelB]);
});
hzLayoutT.addItems([remove]);
//Open & Close button
if (currentBox.parentElement.attributes[2].localName != "data-origlevel") {
let closeB = new OO.ui.ButtonWidget({
icon: "unFlag",
invisibleLabel: true,
title: "Mark as answered"
});
closeB.on("click", function () {
execute(responseTable, nonResponses.Close, "", true, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
});
hzLayoutT.addItems([closeB]);
} else {
let openB = new OO.ui.ButtonWidget({
icon: "flag",
invisibleLabel: true,
title: "Mark as unanswered"
});
openB.on("click", function () {
execute(responseTable, nonResponses.Open, "", false, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
});
hzLayoutT.addItems([openB]);
}
//Create quick response buttons
for (let i = 0; i < quickResponses.length; i++) {
let newButton = new OO.ui.ButtonWidget({
label: quickResponses[i][1][0],
flags: quickResponses[i][1][1],
title: quickResponses[i][1][2]
});
newButton.on("click", function () {
execute(responseTable, responses[quickResponses[i][0]], "", toggleAns.selected, typeChange.defaultValue, targetPages.getValue(), "nochange", "");
});
hzLayoutT.addItems([newButton]);
}
//Toggle answer button
let toggleAns = new OO.ui.CheckboxInputWidget({selected: true});
hzLayoutT.addItems([toggleAns, new OO.ui.LabelWidget({label: "Answered"})]);
//Create lastrow horizontal layout
let hzLayoutB = new OO.ui.HorizontalLayout();
//Create lastrow fieldset
let fieldsetB = new OO.ui.FieldsetLayout();
fieldsetB.addItems([new OO.ui.FieldLayout(new OO.ui.Widget({content: [hzLayoutB]}), {align: 'top'})]);
$(fourthRow).append(fieldsetB.$element);
//Cancel response button
let cancelB = new OO.ui.ButtonWidget({
icon: "cancel",
flags: ["destructive"],
label: "Cancel",
framed: false,
title: "Cancel the response & close menu"
});
cancelB.on("click", function () {
replyButton.setDisabled(false);
responseTable.parentElement.remove();
});
hzLayoutB.addItems([cancelB]);
let isWatched = (typeof (watchStatus[0]) != "undefined");
//Watch list//Toggle answer button
let toggleWL = new OO.ui.CheckboxInputWidget({selected: isWatched});
let toggleWLLable = new OO.ui.LabelWidget({label: "Watch this page"});
toggleWL.on("change", function (newStatus) {
watchDropdown.setDisabled(!newStatus);
});
toggleWLLable.$element[0].style = "white-space: nowrap;";
hzLayoutB.addItems([toggleWL, toggleWLLable]);
let watchValue = "never";
let watchOptions = [{data: "never", label: "Permanent"}, {data: "1 day", label: "1 day"}, {
data: "3 days",
label: "3 days"
}, {data: "1 week", label: "1 week"}, {data: "1 month", label: "1 month"}];
let wlExpiry = watchStatus[1];
if (typeof (wlExpiry) != "undefined") {
let daysDif = Math.ceil((new Date(wlExpiry).getTime() - Date.now()) / 1000 / 60 / 60 / 24);
watchOptions.unshift({data: "nochange", label: daysDif + " days"});
watchValue = wlExpiry;
}
//Create WLDropdown horizontal layout
let hzLayoutWLD = new OO.ui.HorizontalLayout();
hzLayoutB.addItems([hzLayoutWLD]);
//Watchlist dropdown
let watchDropdown = new OO.ui.DropdownInputWidget({
value: watchValue,
options: watchOptions,
disabled: !isWatched
});
hzLayoutWLD.addItems([watchDropdown]);
//Submit response button
let submitB = new OO.ui.ButtonWidget({
icon: "checkAll",
flags: ["primary", "progressive"],
label: "Submit",
title: "Submit the response",
disabled: true
});
submitB.on("click", function () {
let newResponse = responses[dropMenu.getLabel()];
let newText = inputText.value;
let isAns = toggleAns.selected;
let newType = typeChange.value;
let newTargets = targetPages.getValue();
let targets = targetPages.items;
let targetChange = false;
let wlChange = toggleWL.selected;
let wlVals = watchDropdown.value;
if (wlChange) {
wlChange = "watch";
if (wlVals == "nochange") {
wlChange = "nochange";
}
} else {
wlChange = "unwatch";
}
if (typeof (newResponse) != "undefined") {
execute(responseTable, newResponse, newText, isAns, newType, newTargets, wlChange, wlVals);
} else if (typeChange.value != typeChange.defaultValue) {
execute(responseTable, nonResponses.ChangeLevel, "", isAns, newType, newTargets, wlChange, wlVals);
}
if (targets.length == pageLinks.length) {
for (let item = 0; item < targets.length; item++) {
if (targets[item].data != pageLinks[item]) {
targetChange = true;
}
}
} else {
targetChange = true;
}
if (targetChange) {
execute(responseTable, nonResponses.ChangeTarget, "", isAns, newType, newTargets, wlChange, wlVals);
}
});
hzLayoutB.addItems([submitB]);
}
 
async function saveResponseERT(requestBox, responseOption, responseText, answered, requestType, targets, watchPage, watchValue) {
function showOutput(inputText, replyOption, tableRow, template) {
await new Promise(function(resolve) {
var restTransform = "https://en.wikipedia.org/api/rest_v1/transform/wikitext/to/html/" + encodePageName;
OO.ui.confirm("Confirm in order to reply to this edit request.").done(function(confirmed) { if (confirmed) {
let preview = "";
resolve();
let newText = inputText.value;
} else {
if (typeof (inputText) == 'string') {
return;
newText = inputText;
}});
}
});
template = dataERT.protections[template][1];
//Create label box & remove action buttons
if (typeof (replyOption) != "undefined") {
requestBox[1].innerHTML = "";
preview += "{{" + template + replyOption[0] + "}} ";
requestBox[3].remove();
}
let infoBox = new OO.ui.MessageWidget({
if (newText != "" && typeof (newText) != "undefined") {
icon: 'pageSettings',
preview += newText + " ";
type: 'notice',
}
label: 'Processing request — Edit request starting, getting section data to edit.'
if (preview != "") {
});
let nickname = " " + mw.user.options.values.nickname;
infoBox.$element[0].style = "margin:5px 0; max-width:50em";
if (nickname == " ") {//Create default signature if no nickname
$(requestBox[1]).append(infoBox.$element);
nickname = mw.user.getName();
//Create loading bar
nickname = " [[User:" + nickname + "|" + nickname + "]] ([[User talk:" + nickname + "|talk]])";
let progressBar = new OO.ui.ProgressBarWidget({
}
progress: false
let dateObj = new Date();
});
let dateNow = dateObj.toLocaleDateString('en-GB', {
$(requestBox[1]).append(progressBar.$element);
timeZone: 'UTC',
//Set preview for output
year: 'numeric',
if (responseOption[0] != "") {//Don't preview a non-response
month: 'long',
let tempValue = {value:responseText};
day: 'numeric'
previewERT(tempValue, responseOption, requestBox[2], requestType);
});
}
let timeNow = dateObj.toLocaleTimeString('en-GB', {timeZone: 'UTC', hour: '2-digit', minute: '2-digit'});
//Find header
preview += nickname + " " + timeNow + ", " + dateNow + " (UTC)";
let header = "";
preview = preview.replaceAll(/{{subst:/gi, "{{");
let sectionIndex = 0;
$.post(restTransform, 'wikitext=' + encodeURIComponent(preview) + '&body_only=true',
let tempElement = requestBox[0];
function (html) {
let sectionQuery = await ApiGetERT({
if (inputText.value != "" || typeof (replyOption) != "undefined") {//Stops preview appearing with empty input box
action: "parse",
tableRow.style = "padding:8px 1em 2px;";
page: mw.config.get("wgPageName"),
tableRow.children[1].innerHTML = html;
prop: "sections"
}
});
}
let sections = sectionQuery.parse.sections;
);
do {
} else {
tempElement = tempElement.previousElementSibling;
tableRow.style = "display:none;";
if (tempElement.classList.contains("mw-heading")) {
}
if (tempElement.parentElement.tagName == "SECTION") { //Need to support both while new parser is being implemented
header = $(tempElement).find("h1,h2,h3,h4,h5,h6")[0].id;
sectionIndex = parseInt(tempElement.parentElement.dataset.mwSectionId);
} else {
if (tempElement.getElementsByClassName("mw-headline").length > 0) { //Vector 2022
header = tempElement.getElementsByClassName("mw-headline")[0].id;
} else { //Vector Legacy
header = $(tempElement).find("h1,h2,h3,h4,h5,h6")[0].id;
}
for (let i = 0; i < sections.length; i++) {
if (sections[i].anchor == header) {
sectionIndex = parseInt(sections[i].index);
}
}
}
}
}
while (header == "");
infoBox.setLabel("Processing request — Making changes to the edit request");
let editSummary = "/* " + header.replaceAll("_", " ") + " */ " + responseOption[2] + " ([[User:Terasail/Edit_Request_Tool|Edit Request Tool]])";
let wikitextQuery = await ApiGetERT({
action: "parse",
page: mw.config.get("wgPageName"),
section: sectionIndex,
prop: "wikitext|revid"
});
let wikitext = wikitextQuery.parse.wikitext["*"];
let latestRevision = wikitextQuery.parse.revid;
if (responseOption[1] != "Remove") {
let editTemplate = "{{Edit " + requestType + "-protected";
for (let c3 = 0; c3 < targets.length; c3++) {
editTemplate += "|" + targets[c3];
}
if (answered) {
editTemplate += "|answered=yes";
} else {
editTemplate += "|answered=no";
}
wikitext = wikitext.replace(/{{ *([SETFI]PER|Edit([ -]?[A-Z]+[ -]?|[- ])Protected|Sudo)\s*[^}}]*/i, editTemplate);
if (responseOption[1] != "Close") {
wikitext = wikitext.trim() + "\n:";
if (responseOption[0] != "") {
wikitext += "{{subst:" + dataERT.protections[requestType][1] + responseOption[0] + "}} ";
}
if (responseText != "") {
wikitext += responseText.replaceAll(/\s*~~~~\s*/g, "") + " ";
}
wikitext += "~~~~";
}
} else {
wikitext = "";
editSummary = editSummary.replace(/[^]+\*\/ /, "");
}
infoBox.setType("success");
infoBox.setLabel("Processing request — Saving changes to the talk page.");
if (latestRevision != mw.config.values.wgRevisionId) {
await new Promise(function(resolve) {
OO.ui.confirm("There has been a new revision to the page, do you wish to continue?").done(function(confirmed) { if (confirmed) {
resolve();
} else {
return;
}});
});
}
if (watchPage) {
if (watchPage != "nochange") {
watchPage = "watch";
}
} else {
watchPage = "unwatch";
}
let apiParams = {
action: 'edit',
title: mw.config.get("wgPageName"),
text: wikitext,
section: sectionIndex,
summary: editSummary,
watchlist: watchPage
};
if (watchPage == "watch") {
apiParams.watchlistexpiry = watchValue;
}
new mw.Api().postWithEditToken(apiParams).done(function () {
window.___location = "/w/index.php?title=" + encodeURI(mw.config.get("wgPageName")) + "&type=revision&diff=cur&oldid=prev";
});
}
 
function ApiGetERT(params) {
if (editRequestBoxes.length != 0) {
return new Promise(function(resolve) {
let pageID = mw.config.values.wgArticleId;
new mw.Api().get({params)
.done(function (data) {resolve(data);})
action: "query",
.fail(function (data) {console.error(data);});
prop: "info",
});
pageids: pageID,
inprop: "watchers|visitingwatchers|watched",
format: "json"
}).done(function (data) {
data = data.query.pages[pageID];
let watchers = data.watchers;
let visiting = data.visitingwatchers;
watchStatus = [data.watched, data.watchlistexpiry];
if (typeof (watchers) == "undefined") {
pageWatchers += "less than 30";
} else {
pageWatchers += watchers;
}
pageWatchers += " users watching this page (";
if (typeof (visiting) == "undefined") {
pageWatchers += "0";
} else {
pageWatchers += visiting;
}
pageWatchers += " have viewed recent edits).";
});
var jsonERTURL = "https://en.wikipedia.org/w/index.php?title=User:Terasail/Edit_Request_Tool.json&action=raw&ctype=text/json";
$.getJSON(jsonERTURL, function (newData) {
dataERT = newData;
mw.loader.load(["oojs-ui-core", "oojs-ui-widgets", "oojs-ui-windows"]);
mw.loader.load(["oojs-ui.styles.icons-interactions", "oojs-ui.styles.icons-moderation", "oojs-ui.styles.icons-user", "oojs-ui.styles.icons-content", "oojs-ui.styles.icons-editing-core", "oojs-ui.styles.icons-editing-advanced"]);
});
for (let i = 0; i < editRequestBoxes.length; i++) {
let currentBox = editRequestBoxes[i].children[0]; //The tbody tag for the box
if (typeof (currentBox.parentElement.dataset.origlevel) != "undefined") {
let isSmall = false;
if (editRequestBoxes[i].id == "") {
isSmall = true;
}
let replyButton = new OO.ui.ButtonWidget({
icon: "edit",
flags: ["progressive"],
label: "Respond",
invisibleLabel: isSmall,
title: "Respond to the edit request."
});
replyButton.on("click", function () {
addButtons(currentBox, replyButton);
replyButton.setDisabled(true);
});
replyButton.$element[0].style = "margin:2px 0";
if (isSmall) {
$(currentBox.children[0].children[0]).append(replyButton.$element);
} else {
$(currentBox).append('<tr><td colspan=2><div style="display: flex; justify-content: center;"></div></td></tr>');
$(currentBox.children[1].children[0].children[0]).append(replyButton.$element);
}
}
}
}
//</nowiki>[[Category:Wikipedia scripts]]