User:Ingenuity/AbuseFilterContribs.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Ingenuity/AbuseFilterContribs. |
const afcAPI = new mw.Api();
if (mw.util.getUrl().includes("Special:Contributions")) {
const user = mw.util.getUrl().match(/Special:Contributions\/(.+)/)[1];
addToContribs(user);
}
async function loadAbuseFilterLog(user) {
return (await afcAPI.get({
action: "query",
list: "abuselog",
afllimit: 50,
afluser: user
})).query.abuselog.filter(e => e.result === "disallow").sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
}
async function addToContribs(user) {
const log = await loadAbuseFilterLog(user);
const cList = document.querySelector(".mw-contributions-list");
const children = Array.prototype.slice.call(cList.children);
const settings = await getSettings();
const offset = getOffset(settings);
const times = await getTimes(children.map(e => e.attributes["data-mw-revid"].value));
for (let i = 0; i < children.length; i++) {
const child = children[i];
const id = Array.prototype.slice.call(child.attributes).filter(a => a.nodeName === "data-mw-revid")[0].value;
const ts = times.filter(e => e.id === Number(id))[0].timestamp;
for (const hit of log) {
if (new Date(hit.timestamp) < new Date(ts)) {
const elem = document.createElement("li");
child.parentElement.insertBefore(elem, child.nextSibling);
const adj = new Date(new Date(hit.timestamp).getTime() + offset * 60 * 1000);
const details = hit.filter_id ? `(<a href="/wiki/Special:AbuseLog/${hit.id}">details</a> | <a href="/wiki/Special:AbuseLog/examine/log/${hit.id}">examine</a>)` : "";
elem.innerHTML = `
${getTimestampString(settings, adj.getUTCFullYear(), adj.getUTCMonth(), adj.getUTCDate(), adj.getUTCHours(), adj.getUTCMinutes())}:
triggered ${hit.filter_id ? `<a href="/wiki/Special:AbuseFilter/${hit.filter_id}">filter ${hit.filter_id}</a>` : "an edit filter"}
on <a href="/wiki/${encodeURIComponent(hit.title)}">${hit.title}</a>
${details}
`;
}
}
}
}
async function getTimes(ids) {
const data = [];
Object.values((await afcAPI.get({
action: "query",
revids: ids.join("|"),
prop: "revisions",
rvprop: "timestamp|ids"
})).query.pages).forEach(e => {
data.push({ id: e.revisions[0].revid, timestamp: e.revisions[0].timestamp });
});
return data.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
}
async function getSettings() {
return (await afcAPI.get({
action: "query",
meta: "userinfo",
uiprop: "options"
})).query.userinfo.options;
}
function getOffset(settings) {
return Number(settings.timecorrection.match(/([+-]\d+)/)[1]);
}
function getTimestampString(settings, year, month, day, hour, minute) {
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
switch (settings.date) {
case "dmy":
case "default":
return `${pad(hour)}:${pad(minute)}, ${day} ${monthNames[month]} ${year}`;
case "mdy":
return `${pad(hour)}:${pad(minute)}, ${monthNames[month]} ${day}, ${year}`;
case "ymd":
return `${pad(hour)}:${pad(minute)}, ${year} ${monthNames[month]} ${day}`;
default:
return `${year}-${pad(month + 1)}-${pad(day)}T${hour}:${minute}`;
}
}
function pad(num) {
return num < 10 ? "0" + num : num;
}