User:Novem Linguae/Scripts/DraftCleaner.js: Difference between revisions

Content deleted Content added
switch to new version of publish.php (publish.php)
remove nowiki tags, remove $(async function() {, reduce indenting (publish.php)
Line 2:
 
$(async function() {
 
//
 
/* THIS SCRIPT IS STILL IN BETA AND IS BUGGY ABOUT 25% OF THE TIME. Be sure to check the diff that pops up before submitting.
Line 52 ⟶ 50:
*/
 
$(async function getWikicode(title) {
if ( ! mw.config.get('wgCurRevisionId') ) return ''; // if page is deleted, return blank
async function getWikicode(title) {
var wikicode = '';
if ( ! mw.config.get('wgCurRevisionId') ) return ''; // if page is deleted, return blank
title = encodeURIComponent(title);
var wikicode = '';
await $.ajax({
title = encodeURIComponent(title);
url: 'https://en.wikipedia.org/w/api.php?action=parse&page='+title+'&prop=wikitext&formatversion=2&format=json',
await $.ajax({
success: function (result) {
url: 'https://en.wikipedia.org/w/api.php?action=parse&page='+title+'&prop=wikitext&formatversion=2&format=json',
success:wikicode function= (result) {['parse']['wikitext'];
},
wikicode = result['parse']['wikitext'];
dataType: "json",
},
});
dataType: "json",
return wikicode;
});
}
return wikicode;
}
// borrowed from [[Wikipedia:User scripts/Guide#Edit a page and other common actions]]
function editPage(articleName, wikicode, summary) {
let debugInfo = $.ajax({
url: mw.util.wikiScript('api'),
type: 'POST',
dataType: 'json',
data: {
format: 'json',
action: 'edit',
title: articleName,
text: wikicode, // will replace entire page content
summary: summary,
token: mw.user.tokens.get('csrfToken')
},
async: false
});
}
 
// borrowed from [[Wikipedia:User scripts/Guide#Edit a page and other common actions]]
function goToShowChangesScreen(titleWithNamespaceAndUnderscores, wikicode, editSummary) {
function editPage(articleName, wikicode, summary) {
let titleEncoded = encodeURIComponent(titleWithNamespaceAndUnderscores);
let wgServerdebugInfo = mw$.config.getajax('wgServer');{
url: mw.util.wikiScript('api'),
let wgScriptPath = mw.config.get('wgScriptPath');
type: 'POST',
let baseURL = wgServer + wgScriptPath + '/';
dataType: 'json',
// https://stackoverflow.com/a/12464290/3480193
data: {
$(`<form action="${baseURL}index.php?title=${titleEncoded}&action=submit" method="POST"/>`)
format: 'json',
.append($('<input type="hidden" name="wpTextbox1">').val(wikicode))
action: 'edit',
.append($('<input type="hidden" name="wpSummary">').val(editSummary))
title: articleName,
.append($('<input type="hidden" name="mode">').val('preview'))
text: wikicode, // will replace entire page content
.append($('<input type="hidden" name="wpDiff">').val('Show changes'))
summary: summary,
.append($('<input type="hidden" name="wpUltimateParam">').val('1'))
token: mw.user.tokens.get('csrfToken')
.appendTo($(document.body)) //it has to be added somewhere into the <body>
},
.submit();
async: false
}
});
}
 
function goToShowChangesScreen(titleWithNamespaceAndUnderscores, wikicode, editSummary) {
/** returns the pagename, including the namespace name, but with spaces replaced by underscores */
let titleEncoded = encodeURIComponent(titleWithNamespaceAndUnderscores);
function getArticleName() {
return let wgServer = mw.config.get('wgPageNamewgServer');
let wgScriptPath = mw.config.get('wgScriptPath');
}
let baseURL = wgServer + wgScriptPath + '/';
// https://stackoverflow.com/a/12464290/3480193
$(`<form action="${baseURL}index.php?title=${titleEncoded}&action=submit" method="POST"/>`)
.append($('<input type="hidden" name="wpTextbox1">').val(wikicode))
.append($('<input type="hidden" name="wpSummary">').val(editSummary))
.append($('<input type="hidden" name="mode">').val('preview'))
.append($('<input type="hidden" name="wpDiff">').val('Show changes'))
.append($('<input type="hidden" name="wpUltimateParam">').val('1'))
.appendTo($(document.body)) //it has to be added somewhere into the <body>
.submit();
}
 
/** returns the pagename, including the namespace name, but with spaces replaced by underscores */
function getArticleName() {
return mw.config.get('wgPageName');
}
 
function showMessage(messageText) {
$('#DraftCleaner').hide();
$('#DraftCleanerNoClick').empty();
$('#DraftCleanerNoClick').prepend(messageText);
$('#DraftCleanerNoClick').show();
}
 
function showClickableButton() {
$('#DraftCleanerNoClick').hide();
$('#DraftCleaner').show();
}
 
/** refresh AND clear cache */
function hardRefresh() {
// window.___location.reload(true) is deprecated. use this instead
window.___location.href = window.___location.href;
}
 
// don't run when not viewing articles
let action = mw.config.get('wgAction');
if ( action != 'view' ) return;
 
// don't run when viewing diffs
let isDiff = mw.config.get('wgDiffNewId');
if ( isDiff ) return;
 
// Only run in mainspace, draftspace, and sandboxes
let titleWithNamespaceAndUnderscores = getArticleName();
let namespaceNumber = mw.config.get('wgNamespaceNumber');
let sandbox = titleWithNamespaceAndUnderscores.match(/sandbox/i);
if ( ! [0, 118].includes(namespaceNumber) && ! sandbox ) return;
 
// Add DraftCleaner to left sidebar
// Using two <li>s. One of the two is kept hidden at all times. This avoids having to delete #DraftCleanerLink, which would also delete the event listener.
$('#p-navigation .vector-menu-content-list').append(`
<li id="DraftCleaner">
<a id="DraftCleanerLink">Run DraftCleaner</a>
</li>
<li id="DraftCleanerNoClick" style="display:none">
function showMessage(messageText) {
$('#DraftCleaner').hide();
$('#DraftCleanerNoClick').empty();
$('#DraftCleanerNoClick').prepend(messageText);
$('#DraftCleanerNoClick').show();
}
function showClickableButton() {
$('#DraftCleanerNoClick').hide();
$('#DraftCleaner').show();
}
/** refresh AND clear cache */
function hardRefresh() {
// window.___location.reload(true) is deprecated. use this instead
window.___location.href = window.___location.href;
}
// don't run when not viewing articles
let action = mw.config.get('wgAction');
if ( action != 'view' ) return;
// don't run when viewing diffs
let isDiff = mw.config.get('wgDiffNewId');
if ( isDiff ) return;
// Only run in mainspace, draftspace, and sandboxes
let titleWithNamespaceAndUnderscores = getArticleName();
let namespaceNumber = mw.config.get('wgNamespaceNumber');
let sandbox = titleWithNamespaceAndUnderscores.match(/sandbox/i);
if ( ! [0, 118].includes(namespaceNumber) && ! sandbox ) return;
// Add DraftCleaner to left sidebar
// Using two <li>s. One of the two is kept hidden at all times. This avoids having to delete #DraftCleanerLink, which would also delete the event listener.
$('#p-navigation .vector-menu-content-list').append(`
<li id="DraftCleaner">
<a id="DraftCleanerLink">Run DraftCleaner</a>
</li>
</li>
<li id="DraftCleanerNoClick" style="display:none">
`);
 
</li>
$('#DraftCleanerLink').on('click', async function() {
`);
// prevent running the script while script is already in progress
showMessage('Editing. Please wait.');
// get page wikicode
$('#DraftCleanerLink').on('click', async function() {
let titleWithNamespaceAndSpaces = titleWithNamespaceAndUnderscores.replace(/_/g, ' ');
// prevent running the script while script is already in progress
let originalWikicode = await getWikicode(titleWithNamespaceAndUnderscores);
showMessage('Editing. Please wait.');
let wikicode = originalWikicode;
// get page wikicode
let titleWithNamespaceAndSpaces = titleWithNamespaceAndUnderscores.replace(/_/g, ' ');
let originalWikicode = await getWikicode(titleWithNamespaceAndUnderscores);
let wikicode = originalWikicode;
 
let dc = new DraftCleaner();
wikicode = dc.cleanDraft(wikicode, namespaceNumber, titleWithNamespaceAndSpaces);
 
// if changes to be made
if ( wikicode != originalWikicode ) {
let summary = 'clean up ([[User:Novem Linguae/Scripts/DraftCleaner.js|DraftCleaner]])';
// editPage(titleWithNamespaceAndUnderscores, wikicode);
// hardRefresh();
await goToShowChangesScreen(titleWithNamespaceAndUnderscores, wikicode, summary);
// else display "no changes needed", then reset
} else {
showMessage('No changes needed.');
setTimeout(function (){
showClickableButton();
}, 2000);
}
});
});
 
//