User:Pythoncoder/Scripts/OneClickAFCH.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 code will be executed when previewing this page.
Documentation for this user script can be added at User:Pythoncoder/Scripts/OneClickAFCH.
/*
**One-Click AFCH**
Adapted by Pythoncoder from [[MediaWiki:Gadget-afchelper.js/submissions.js]]
Adds a button to decline LLM-generated drafts in just one click.
Submitters will be notified and invited to the Teahouse if needed.
If you need to add a comment or a second decline reason, don't click the button!
I may update this in the future to add other common decline reasons.
This script will not work unless you have the AFC Helper Script installed.
The standard user script disclaimer applies: USE THIS AT YOUR OWN RISK!
You are responsible for any actions you take with this script.
Please don't get yourself blocked because you misused this script —
it would make me really sad :(
*/
function oneClickDecline() {
let afchPage = new AFCH.Page( AFCH.consts.pagename );
let afchSubmissionUnparsed = new AFCH.Submission( afchPage );
$.when(
afchSubmissionUnparsed.parse(),
).then( ( afchSubmission ) => {
afchPage.getText( false ).done( ( text ) => {
let data = {
declineReason: ['ai'],
notifyUser: true,
inviteToTeahouse: true,
};
data.afchText = new AFCH.Text( text );
let isDecline = true; // true=decline, false=reject
let declineReason = data.declineReason[ 0 ];
let declineReason2 = null;
let newParams = {
decliner: mw.user.getName(),
declinets: '\x7B\x7Bsubst:REVISIONTIMESTAMP\x7D\x7D'
};
newParams[ '2' ] = declineReason;
// Update decline counts
let declineCounts = AFCH.userData.get( 'decline-counts', {} );
declineCounts[ declineReason ] = ( declineCounts[ declineReason ] || 1 ) + 1;
AFCH.userData.set( 'decline-counts', declineCounts );
// Now update the submission status
afchSubmission.setStatus( 'd', newParams );
data.afchText.updateAfcTemplates( afchSubmission.makeWikicode() );
data.afchText.cleanUp();
// Build edit summary. This is intentionally formatted a bit differently from the official AFCH script (spaced em dash lol) to make it easier to spot this script’s declines in case of bugs
let editSummary = 'Declining submission — ai (Submission appears to be a large language model output)';
afchPage.edit( {
contents: data.afchText.get(),
summary: editSummary
} );
if ( data.notifyUser ) {
afchSubmission.getSubmitter().done( ( submitter ) => {
const userTalk = new AFCH.Page( ( new mw.Title( submitter, 3 ) ).getPrefixedText() ),
shouldTeahouse = data.inviteToTeahouse ? $.Deferred() : false;
// Check categories on the page to ensure that if the user has already been
// invited to the Teahouse, we don't invite them again.
if ( data.inviteToTeahouse ) {
userTalk.getCategories( true ).done( ( categories ) => {
let hasTeahouseCat = false,
teahouseCategories = [
'Category:Wikipedians who have received a Teahouse invitation',
'Category:Wikipedians who have received a Teahouse invitation through AfC'
];
$.each( categories, ( _, cat ) => {
if ( teahouseCategories.indexOf( cat ) !== -1 ) {
hasTeahouseCat = true;
return false;
}
} );
shouldTeahouse.resolve( !hasTeahouseCat );
} );
}
$.when( shouldTeahouse ).then( ( teahouse ) => {
let message = AFCH.msg.get( 'declined-submission', {
$1: AFCH.consts.pagename,
$2: afchSubmission.shortTitle,
$3: 'no',
$4: declineReason,
$5: newParams[ '3' ] || '',
$6: declineReason2 || '',
$7: newParams.details2 || '',
$8: ''
} );
if ( teahouse ) {
message += '\n\n' + AFCH.msg.get( 'teahouse-invite' );
}
AFCH.actions.notifyUser( submitter, {
message: message,
summary: 'Notification: Your [[' + AFCH.consts.pagename + '|Articles for Creation submission]] has been declined'
} );
} );
} );
}
// Log AfC if enabled and CSD if necessary
afchSubmission.getSubmitter().done( ( submitter ) => {
AFCH.actions.logAfc( {
title: afchPage.rawTitle,
actionType: isDecline ? 'decline' : 'reject',
declineReason: declineReason,
declineReason2: declineReason2,
submitter: submitter
} );
} );
} );
});
}
function checkForDeclineButton(i) {
if (mw.config.get('wgNamespaceNumber') !== 2 && mw.config.get('wgNamespaceNumber') !== 118) {
// Only run in userspace and draftspace
return;
} else if (document.getElementById('1click_ai') === null && document.getElementById('afchDecline') !== null) {
htmlString = '<span id="1click_ai" style="display: inline-block; background-color: #f33; font-size: 24px; padding: 12px 24px; margin: 12px 0; border-radius: 8px; cursor: pointer">Decline AI</div>';
let elem = document.createElement('div');
elem.innerHTML = htmlString;
document.getElementById('bodyContent').insertBefore(elem, document.querySelector('#mw-content-text'));
document.getElementById('1click_ai').addEventListener('click', oneClickDecline);
} else {
setTimeout(function(i) {checkForDeclineButton(i+1)}, 1000*i*i);
}
}
checkForDeclineButton(1);