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

Content deleted Content added
Update to support new parser format
Account for {{COI edit}}
 
(5 intermediate revisions by the same user not shown)
Line 60:
if (expiry) {
watched = Math.ceil((new Date(expiry).getTime() - Date.now()) / 1000 / 60 / 60 / 24) + " days";
});
if (watched == undefined && typeof(autoWatchRequests) != "undefined" && autoWatchRequests == true) {
watched = '';
}
watchStatus.push(watchData.watchers || "less than 30", watchData.visitingwatchers || "<30", watched);
Line 79 ⟶ 82:
 
function loadCOIResponse(COIRequest, respondButton, watchStatus) {
let responseBoxHTML = '<table class="response-box" 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;">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 style="color:#808080">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 style="color:#808080">Preview:</div><div></div></td></tr><tr style="display: flex; justify-content: right;"><td class="response-controls"></td></tr></table>';
$(responseBoxHTML).insertAfter(COIRequest, respondButton);
let responseBox = COIRequest.nextElementSibling;
Line 125 ⟶ 128:
//Response dropdown
let responseDropdown = new OO.ui.DropdownWidget({
label: "Select reply option - Add additional text below",
menu: {items: []}
}).on("labelChange", function () {
submitButton.setDisabled(false);
previewCOI(responseDropdown.menu.findSelectedItem().getData(), responseText.value, responsePreview);
});
for (let i = 0; i < responseCOI.length; i++) {
let tempWidget = new OO.ui.MenuOptionWidget({
Line 148 ⟶ 151:
previewCOI(responseDropdown.menu.findSelectedItem().getData(), responseText.value, responsePreview);
}
});
responseText.$element[0].style = "margin:5px auto;";
$(responseCustom).append(responseText.$element);
Line 177 ⟶ 180:
}
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
Line 189 ⟶ 192:
watchlistDropdown.setDisabled(!newStatus);
});
let watchlistLabel = new OO.ui.LabelWidget({label: "Watch this page"}).on("change", function (newStatus) {;
 
});
//Submit Button
let submitButton = new OO.ui.ButtonWidget({
Line 213 ⟶ 214:
let nickname = " " + mw.user.options.values.nickname;
if (nickname == " ") {//Create default signature if no nickname
nickname = mw.user.getName();
nickname = " [[User:" + nickname + "|" + nickname + "]] ([[User talk:" + nickname + "|talk]])";
}
}
let dateObj = new Date();
let dateNow = dateObj.toLocaleDateString('en-GB', {
timeZone: 'UTC',
year: 'numeric',
month: 'long',
day: 'numeric'
});
let timeNow = dateObj.toLocaleTimeString('en-GB', {timeZone: 'UTC', hour: '2-digit', minute: '2-digit'});
responseText = responseText + nickname + " " + timeNow + ", " + dateNow + " (UTC)";
responseText = responseText.replaceAll(/{{subst:/gi, "{{");
responseText = responseText.replaceAll(/\s*~~~~\s*/g, "");
$.post(restTransform, 'wikitext=' + encodeURIComponent(responseText) + '&body_only=true',
function (html) {
tableCell.style = "padding:8px 1em 2px;";
tableCell.children[1].innerHTML = html;
}
}
);
);
} else {
tableCell.style = "display:none;";
}
}
}
 
Line 278 ⟶ 279:
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); //This is a string and might need to be a int
} else {
header =if (tempElement.getElementsByClassName("mw-headline")[0].id;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) {
Line 302 ⟶ 307:
if (responseOption.parameter != "") {
let template = "{{Edit COI|" + responseOption.parameter + "}}";
wikitext = wikitext.replace(/{{ *(Edit[ _])?COI(-protected|([ _](edit|request)){1,2})?( *\| *([=A-Z])*)* *}}/i, template);
}
if (responseOption.response != "") {
Line 324 ⟶ 329:
}
if (watchPage) {
if (watchValuewatchPage =!= "nochange") {
watchPage = "nochange";
} else {
watchPage = "watch";
}
Line 333 ⟶ 336:
}
let apiParams = {
action: 'edit',
title: mw.config.get("wgPageName"),
text: wikitext,
section: sectionIndex,
summary: editSummary,
watchlist: watchPage
};
if (watchPage == "watch") {
apiParams.watchlistexpiry = watchValue;
}
}
let reloadURL = "/w/index.php?title=" + encodeURI(mw.config.get("wgPageName")) + "&type=revision&diff=cur&oldid=prev";
new mw.Api().postWithEditToken(apiParams).done(function () {
window.___location = reloadURL;
});
}