User:DreamRimmer/QDA7endsore.js
Appearance
Note: After publishing, 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)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
//<nowiki>
(function() {
'use strict';
function getUserSignature() {
const username = mw.config.get('wgUserName');
const page = mw.config.get('wgPageName').replace(/_/g, ' ');
const now = new Date();
const months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
const hours = String(now.getUTCHours()).padStart(2, '0');
const minutes = String(now.getUTCMinutes()).padStart(2, '0');
const day = now.getUTCDate();
const month = months[now.getUTCMonth()];
const year = now.getUTCFullYear();
const timestamp = `${hours}:${minutes}, ${day} ${month} ${year} (UTC)`;
return `[[User:${username}|${username}]] ([[User talk:${username}|talk]]) ${timestamp} <small>([[Special:PageHistory/${page}|verify]])</small>`;
}
function isValidNamespace() {
const namespace = mw.config.get('wgNamespaceNumber');
return namespace === 0 || namespace === 2;
}
function hasA7QDTemplate() {
return new Promise((resolve) => {
const api = new mw.Api();
api.get({
action: 'query',
prop: 'revisions',
titles: mw.config.get('wgPageName'),
rvprop: 'content',
format: 'json'
}).done(function(data) {
const pages = data.query.pages;
const pageId = Object.keys(pages)[0];
if (pages[pageId].revisions && pages[pageId].revisions[0]) {
const content = pages[pageId].revisions[0]['*'];
const qdRegex = /\{\{\s*(?:qd|QD)\s*\|\s*(?:a7|A7)/i;
resolve(qdRegex.test(content));
} else {
resolve(false);
}
}).fail(function() {
resolve(false);
});
});
}
function checkEndorsementStatus() {
return new Promise((resolve) => {
const api = new mw.Api();
api.get({
action: 'query',
prop: 'revisions',
titles: mw.config.get('wgPageName'),
rvprop: 'content',
format: 'json'
}).done(function(data) {
const pages = data.query.pages;
const pageId = Object.keys(pages)[0];
const content = pages[pageId].revisions[0]['*'];
const currentUser = mw.config.get('wgUserName');
const endorsementRegex = /\|\s*endorsement\s*=\s*([^|}]*)/i;
const match = content.match(endorsementRegex);
let status = {
hasEndorsement: false,
isCurrentUser: false,
content: content
};
if (match) {
const endorsementValue = match[1].trim();
if (endorsementValue.length > 0) {
status.hasEndorsement = true;
const userLinkPattern = new RegExp(`\\[\\[User:${currentUser.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\|`, 'i');
const plainTextPattern = new RegExp(`\\b${currentUser}\\b`, 'i');
status.isCurrentUser = userLinkPattern.test(endorsementValue) || plainTextPattern.test(endorsementValue);
}
}
resolve(status);
});
});
}
function addEndorsement() {
const api = new mw.Api();
checkEndorsementStatus().then(function(status) {
if (status.hasEndorsement && !status.isCurrentUser) {
alert('This A7 deletion has already been endorsed by someone else.');
return;
}
if (!confirm('Are you sure you want to endorse this A7 deletion? Please confirm that you have read the page and believe it is AI-generated and meets the criteria.')) {
return;
}
const currentContent = status.content;
const signature = getUserSignature();
const qdRegex = /(\{\{\s*(?:qd|QD)\s*\|\s*(?:a7|A7)(?:\s*\|[^}]*?)??)(\}\})/i;
let newContent = currentContent.replace(qdRegex, function(match, templateContent, templateEnd) {
if (/\|\s*endorsement\s*=/i.test(templateContent)) {
return templateContent.replace(/(\|\s*endorsement\s*=\s*)([^|}]*)/i, '$1' + signature) + templateEnd;
} else {
const hasOtherParams = /\|\s*(?:editor|date)/i.test(templateContent);
if (hasOtherParams) {
return templateContent.replace(/(\|\s*(?:editor|date))/i, '|endorsement=' + signature + '$1') + templateEnd;
} else {
return templateContent + '|endorsement=' + signature + templateEnd;
}
}
});
api.postWithToken('csrf', {
action: 'edit',
title: mw.config.get('wgPageName'),
text: newContent,
summary: 'Endorsing A7 quick deletion',
minor: true
}).done(function(data) {
if (data.edit && data.edit.result === 'Success') {
location.reload();
}
}).fail(function(error) {
alert('Error saving page: ' + error);
});
});
}
function removeEndorsement() {
const api = new mw.Api();
checkEndorsementStatus().then(function(status) {
if (!status.isCurrentUser) {
alert('You cannot remove someone else’s endorsement.');
return;
}
if (!confirm('Are you sure you want to remove your endorsement of this A7 deletion?')) {
return;
}
const currentContent = status.content;
const qdRegex = /(\{\{\s*(?:qd|QD)\s*\|\s*(?:a7|A7)(?:\s*\|[^}]*?)??)(\}\})/i;
const endorsementParamRegex = /(\|\s*endorsement\s*=)([\s\S]*?)(?=(\|\s*(?:editor|date)\s*=|\}\}))/i;
let newContent = currentContent.replace(qdRegex, function(match, templateContent, templateEnd) {
return templateContent.replace(endorsementParamRegex, '$1') + templateEnd;
});
api.postWithToken('csrf', {
action: 'edit',
title: mw.config.get('wgPageName'),
text: newContent,
summary: 'Removing A7 endorsement',
minor: true
}).done(function() {
location.reload();
}).fail(function(error) {
alert('Error saving page: ' + error);
});
});
}
function addEndorsementButton() {
const targetElement = document.querySelector('div.ambox-speedy-text');
if (!targetElement) return;
checkEndorsementStatus().then(function(status) {
const button = document.createElement('button');
button.id = 'endorse-a7-button';
button.className = 'oo-ui-buttonElement oo-ui-buttonElement-progressive oo-ui-widget oo-ui-widget-enabled';
if (status.hasEndorsement && status.isCurrentUser) {
button.style.cssText = 'margin: 10px auto; background-color: rgb(204, 51, 51); color: white; border: 1px solid rgb(204, 51, 51); text-align: center; border-radius: 3px; font-size: 14px; padding: 6px 12px; display: block; font-weight: bold; cursor: pointer;';
button.textContent = 'Remove A7 endorsement';
button.addEventListener('click', removeEndorsement);
} else if (status.hasEndorsement && !status.isCurrentUser) {
button.style.cssText = 'margin: 10px auto; background-color: rgb(128, 128, 128); color: white; border: 1px solid rgb(128, 128, 128); text-align: center; border-radius: 3px; font-size: 14px; padding: 6px 12px; display: block; font-weight: bold; cursor: not-allowed;';
button.textContent = 'Already endorsed by another user';
button.disabled = true;
} else {
button.style.cssText = 'margin: 10px auto; background-color: rgb(51, 102, 204); color: white; border: 1px solid rgb(51, 102, 204); text-align: center; border-radius: 3px; font-size: 14px; padding: 6px 12px; display: block; font-weight: bold; cursor: pointer;';
button.textContent = 'Endorse A7 deletion';
button.addEventListener('click', addEndorsement);
}
targetElement.appendChild(button);
});
}
function init() {
if (!mw.config.get('wgUserName') || !isValidNamespace()) return;
hasA7QDTemplate().then(function(hasTemplate) {
if (!hasTemplate) return;
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', addEndorsementButton);
} else {
addEndorsementButton();
}
});
}
if (mw.config.get('wgServerName') === 'simple.wikipedia.org') {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
}
})();
//</nowiki>