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:
*/
if ( ! mw.config.get('wgCurRevisionId') ) return ''; // if page is deleted, return blank
var wikicode = '';
title = encodeURIComponent(title);
await $.ajax({
url: 'https://en.wikipedia.org/w/api.php?action=parse&page='+title+'&prop=wikitext&formatversion=2&format=json',
success: function (result) {
},
dataType: "json",
});
return wikicode;
}
// borrowed from [[Wikipedia:User scripts/Guide#Edit a page and other common actions]]
function editPage(articleName, wikicode, summary) {
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
});
}
function goToShowChangesScreen(titleWithNamespaceAndUnderscores, wikicode, editSummary) {
let titleEncoded = encodeURIComponent(titleWithNamespaceAndUnderscores);
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">
</li>
`);
$('#DraftCleanerLink').on('click', async function() {
// prevent running the script while script is already in progress
showMessage('Editing. Please wait.');
// get page wikicode
let titleWithNamespaceAndSpaces = titleWithNamespaceAndUnderscores.replace(/_/g, ' ');
let originalWikicode = await getWikicode(titleWithNamespaceAndUnderscores);
let wikicode = originalWikicode;
});
|