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

Content deleted Content added
pull request #152: Update link ___location, notification method, and namespace check (publish.php)
code cleanup (publish.php)
Line 18:
- clean external links out of the main article area (turn them into references)
- add ==References== section
- removesremove bold from headings
 
- Other uses:
Line 45:
- if article has headings but no lead, remove first heading
- replace unicode bullets with asterisks
 
Add one of the following to your User:yourName/common.js (at the top) to change the position where DraftCleaner puts its link:
window.draftCleanerPutInToolsMenu = true;
window.draftCleanerPutInMoreMenu = true;
 
This page was assembled from 3 files using my publish.php script. I have an offline test suite with around 100 unit tests for the DraftCleaner and StringFilter classes.
Line 50 ⟶ 54:
 
async function getWikicode(title) {
iflet (pageIsDeleted = ! mw.config.get('wgCurRevisionId') ) return ''; // if page is deleted, return blank
if ( pageIsDeleted ) {
return '';
}
 
var wikicode = '';
title = encodeURIComponent(title);
Line 61 ⟶ 69:
});
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
});
}
 
Line 94 ⟶ 84:
.append($('<input type="hidden" name="wpUltimateParam">').val('1'))
.appendTo($(document.body)) //it has to be added somewhere into the <body>
.submittrigger('submit');
}
 
Line 100 ⟶ 90:
function getArticleName() {
return mw.config.get('wgPageName');
 
/** 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');
iflet (isNotViewing = action != 'view' ) return;
if ( isNotViewing ) {
return;
 
// don't run when viewing diffs
let isDiff = mw.config.get('wgDiffNewId');
if ( isDiff ) {
return;
}
 
// Don't run in virtual namespaces
iflet isVirtualNamespace = (mw.config.get('wgNamespaceNumber') < 0) return;
if ( isVirtualNamespace ) {
return;
}
 
let menuID = 'p-navigation';
Line 138 ⟶ 130:
$('#DraftCleanerLink').on('click', async function() {
// prevent running the script while script is already in progress
if ( running ) {
return;
},
running = true;
 
// notify of script starting
mw.notify('Parsing page content...');
Line 152 ⟶ 145:
wikicode = dc.cleanDraft(wikicode, namespaceNumber, titleWithNamespaceAndSpaces);
 
let needsChanges = wikicode != originalWikicode;
// if changes to be made
if ( wikicode != originalWikicodeneedsChanges ) {
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 {
mw.notify('No changes needed!');