User:Enterprisey/req-helper.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 user script seems to have a documentation page at User:Enterprisey/req-helper. |
//<nowiki>
( function ( $, mw ) {
//mw.loader.load( "mediawiki.ui.checkbox", "text/css" );
if ( mw.config.get( 'wgAction' ) === 'view' && mw.config.get( 'wgPageName' ).indexOf('Wikipedia:Requested_articles') !== -1 ) {
// Load and display form
$.getJSON(
mw.util.wikiScript('api'),
{
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'content',
rvlimit: 1,
titles: "User:APerson/req-helper.js/tpl-panel.js"
}
).done( function ( data ) {
var pageId = Object.keys(data.query.pages)[0];
var panelHtml = data.query.pages[pageId].revisions[0]['*'];
$( "#jump-to-nav" ).after( panelHtml );
$( "#req-helper-panel input[type='checkbox']" ).click( updateRequestActions );
$( "#req-helper-panel input[type='radio']" ).click( updateRequestDisplay );
updateRequestActions();
// Enable save button if there are requests marked for deletion
$( "#mw-content-text" ).on( "click", "a.delete-action", updateSaveChangesButton );
// Save handler
$( "#save-changes" ).click( function ( event ) {
$( "#save-changes" ).prop( "disabled", true );
$( "#req-helper-panel table").append( "<tr><td colspan=4><ul></ul></td></tr>" );
var status = function ( newStatus ) { return $( "<li> ").appendTo( "#req-helper-panel ul" ).text( newStatus ); };
status( $( ".marked-for-deletion" ).length + " item(s) to delete." );
var wikitext;
var getTextStatus = status( "Getting wikitext..." );
$.getJSON(
mw.util.wikiScript('api'),
{
format: 'json',
action: 'query',
prop: 'revisions',
rvprop: 'content',
rvlimit: 1,
titles: mw.config.get( 'wgPageName' )
}
).done( function ( data ) {
var pageId = Object.keys(data.query.pages)[0];
var wikitext = data.query.pages[pageId].revisions[0]['*'];
getTextStatus.text( "Got wikitext." );
$( ".marked-for-deletion" ).each( function ( index, element ) {
// Regex to sanitize link name from http://stackoverflow.com/a/3561711
var linkName = $( element ).children( "a" ).first().text(),
sanitizedLinkName = linkName.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'),
regexToRemove = new RegExp( "\\*\\s?\\[\\[" + sanitizedLinkName + "\\]\\].*?\n", "i" );
console.log( "Replacing " + linkName + " - regex: " + regexToRemove + " - search:" + wikitext.search( regexToRemove ) );
wikitext = wikitext.replace( regexToRemove, "" );
} );
status( "Processed wikitext." );
var saveStatus = status( "Saving page..." );
var plural = ( $( ".marked-for-deletion" ).length === 1 ) ? "" : "s";
$.ajax( {
url: mw.util.wikiScript( 'api' ),
type: 'POST',
dataType: 'json',
data: {
format: 'json',
action: 'edit',
title: mw.config.get( 'wgPageName' ),
summary: "Removing " + $( ".marked-for-deletion" ).length + " request" + plural + " ([[User:APerson/req-helper|req-helper]])",
token: mw.user.tokens.get( 'editToken' ),
text: wikitext
}
} ).done ( function ( data ) {
if( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
saveStatus.html( "<b>Saved page!</b> (" )
.append( $( "<a>" )
.text( "reload" )
.attr( "href", "#" )
.click( function () { document.location.reload( true ); } ) )
.append( ")" );
} else {
saveStatus.text( "There was an error saving the page." );
}
} );
} ); // end get-current-page-wikitext handler
} ); // end save handler
// Mark blue handler
$( "#mark-bluelinks" ).click( function ( event ) {
forEachRequest( function ( element ) {
// Bluelinks have no class :D
if( $( element ).attr( "class" ) ) return;
$( element ).parent().toggleClass( "marked-for-deletion" );
} );
updateSaveChangesButton();
} ); // end mark handler
} ); // end form-loaded handler
}
function updateSaveChangesButton () {
var numMarkedRequests = $( ".marked-for-deletion" ).length;
$( "#save-changes" ).prop( "disabled", numMarkedRequests === 0 );
$( "#save-changes" ).text( numMarkedRequests ? ( "Delete " + numMarkedRequests + " requests" ) : "Save changes" );
}
// Takes a callback that takes a link object
function forEachRequest ( callback ) {
$( "#mw-content-text a" ).each( function ( index, element ) {
// Test to make sure it's an actual request
if( $( element ).children( ".tocnumber" ).length ) return;
if( $( element ).parent().prop( "tagName" ) !== "LI" ) return;
if( $( element ).parent().children().first().text() !== $( element ).text() ) return;
// Run callback
callback( element );
} );
}
function updateRequestDisplay () {
var noSourcesAction = $( "input[name=no-sources]:checked" ).val(),
someSourcesAction = $( "input[name=some-sources]:checked" ).val();
forEachRequest( function ( link ) {
// Highlight articles based on # of provided refs
var listElement = $( link ).parent();
if( listElement.children( ".external" ).length === 0 ) {
if( noSourcesAction === "hide" ) {
listElement.hide();
} else {
listElement.show();
listElement.css( "background-color", ( $( "input[name=no-sources]:checked" ).val() === "highlight" ) ? "#FEE" : "#FFF" );
}
} else {
if( someSourcesAction === "hide" ) {
listElement.hide();
} else {
listElement.show();
listElement.css( "background-color", ( $( "input[name=some-sources]:checked" ).val() === "highlight" ) ? "#EFE" : "#FFF" );
}
}
} );
}
function updateRequestActions () {
var showSearch = $( "#show-search-option" ).is( ":checked" ),
showDelete = $( "#show-delete-option" ).is( ":checked" );
// Make sure we aren't destroying any deletion state
if( !showDelete && $( ".marked-for-deletion" ).length !== 0 ) {
if( window.confirm( "You've marked some requests for deletion. " +
"Are you sure you want to exit mark-for-deletion mode without deleting the requests?" ) ) {
// Wipe deletion state
$( "a.delete-action" ).text( "delete" );
$( "li.marked-for-deletion" ).toggleClass( "marked-for-deletion" );
$( "#save-changes" ).prop( "disabled", true );
} else {
// The user is indecisive; get out
$( "#show-delete-option" ).prop( "checked", true );
return;
}
}
forEachRequest( function ( link ) {
if( $( link ).parent().children( "span" ).length === 0 ) {
$( "<span>" )
.insertAfter( link )
.addClass( "request-actions" )
.css( "margin-left", "0.25em" );
}
// Now that there's definitely a span there, add action links
var span = $( link ).parent().children( "span.request-actions" );
span.empty();
if( showSearch || showDelete ) {
span.append( "( " );
if( showSearch ) {
span.append( $( "<a>" )
.text( "search" )
.attr( "href", "https://www.google.com/search?q=" + $( link ).text().replace( / /, "+" ) ) );
}
if( showSearch && showDelete ) {
span.append( " | " );
}
if( showDelete ) {
span.append( $( "<a>" )
.text( "delete" )
.addClass( "delete-action" )
.click( function ( e ) {
span.parent().toggleClass( "marked-for-deletion" );
if( $( this ).text() === "delete" ) {
$( this ).text( "undelete" );
} else {
$( this ).text( "delete" );
}
} ) );
}
span.append( " )" );
}
} );
}
}( jQuery, mediaWiki ) );
//</nowiki>