Content deleted Content added
fix linter errors (publish.php) |
fix linter errors. wrap in IIFE (publish.php) |
||
Line 53:
*/
( function () {
async function getWikicode( title ) { return '';
}
let 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 ) {
wikicode = result.parse.wikitext;
},
dataType: 'json'
} );
return wikicode;
}
function goToShowChangesScreen( titleWithNamespaceAndUnderscores, wikicode, editSummary ) {
const wgServer = mw.config.get( 'wgServer' );
const wgScriptPath = mw.config.get( 'wgScriptPath' );
const 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>
.trigger( 'submit' );
}
/** returns the pagename, including the namespace name, but with spaces replaced by underscores */
function getArticleName() {
}
// don't run when not viewing articles
const action = mw.config.get( 'wgAction' );
const isNotViewing = action != 'view';
if ( isNotViewing ) {
return;
}
// don't run when
if ( isDiff ) {
return;
}
//
if (
}
let menuID = 'p-navigation';
// @ts-ignore
if (
menuID = 'p-tb';
// @ts-ignore
} else if ( window.draftCleanerPutInMoreMenu ) {
menuID = 'p-cactions';
}
const titleWithNamespaceAndUnderscores = getArticleName();
const namespaceNumber = mw.config.get( 'wgNamespaceNumber' );
let running = false;
// Add DraftCleaner to the toolbar
mw.loader.using( [ 'mediawiki.util' ], () => {
mw.util.addPortletLink( menuID, '#', 'Run DraftCleaner', 'DraftCleanerLink' );
$( '#DraftCleanerLink' ).on( 'click', async () => {
// prevent running the script while script is already in progress
if ( running ) {
return;
}
running = true;
mw.notify( 'Parsing page content...' );
const titleWithNamespaceAndSpaces = titleWithNamespaceAndUnderscores.replace( /_/g, ' ' );
const originalWikicode = await getWikicode( titleWithNamespaceAndUnderscores );
let wikicode = originalWikicode;
wikicode = dc.cleanDraft( wikicode, namespaceNumber, titleWithNamespaceAndSpaces );
const needsChanges = wikicode != originalWikicode;
if ( needsChanges ) {
const summary = 'clean up ([[User:Novem Linguae/Scripts/DraftCleaner.js|DraftCleaner]])';
await goToShowChangesScreen( titleWithNamespaceAndUnderscores, wikicode, summary );
} else {
mw.notify( 'No changes needed!' );
}
} );
} );
}() );
// === modules/DraftCleaner.js ======================================================
Line 612 ⟶ 615:
fixExternalLinksToWikipediaArticles( wikicode ) {
// [https://en.wikipedia.org/wiki/Article] and [https://en.wikipedia.org/wiki/Article Article name]
return wikicode.replace( /(?<!\[)\[https?:\/\/en\.wikipedia\.org\/wiki\/([^ \]]*)( [^\]]*)?\]/gs,
p1 = decodeURIComponent( p1 );
p1 = p1.replace( /_/g, ' ' );
|