Difference between revisions of "User:BecaBot/common.js"
(Restore from local copy (December 26th, 2020). This will take care of Rush Duel card numbers and handle cards with hashes in their names.) |
(Add a link to Special:PrefiIndex to the sidebar.) |
||
Line 159: | Line 159: | ||
} )( window, window.jQuery, window.mediaWiki, window.console ); | } )( window, window.jQuery, window.mediaWiki, window.console ); | ||
+ | |||
+ | /* global mw */ | ||
+ | /** | ||
+ | * Personal JS | ||
+ | */ | ||
+ | (function() { | ||
+ | "use strict"; | ||
+ | |||
+ | var LAST_LOG = '13:27, 9 December 2023 (UTC)'; // <pre> | ||
+ | |||
+ | var config = mw.config.get([ | ||
+ | 'wgPageName' | ||
+ | ]); | ||
+ | |||
+ | /** | ||
+ | * Add a link to Special:PrefiIndex to the sidebar. | ||
+ | */ | ||
+ | $( '#p-tb' ).find( 'ul' ).append( $( '<li>', { | ||
+ | id: 't-prefixindex', | ||
+ | html: $( '<a>', { | ||
+ | href: '/wiki/Special:PrefixIndex/' + config.wgPageName, | ||
+ | text: 'Prefix index', | ||
+ | } ), | ||
+ | } ) ); | ||
+ | |||
+ | console.log( 'User common.js last updated at', LAST_LOG ); | ||
+ | |||
+ | // </pre> | ||
+ | })(); |
Latest revision as of 13:28, 9 December 2023
/**
* Redirects card numbers to their respective pages.
* @author [[User:Becasita]]
*/
( function _gadgetCardNumberRedirect( window, $, mw, console ) {
"use strict";
var LAST_LOG = '10:30, 6 May 2023 (UTC)'; // <pre>
var config = mw.config.get( [
'wgNamespaceNumber',
'wgPageName'
] );
var api;
function requestContent() {
return api.get( {
action: 'query',
redirects: true,
prop: 'revisions',
rvprop: 'content',
format: 'json',
formatversion: 2,
titles: config.wgPageName
} );
}
function getRedirectMap( data ) {
var content = ( function() {
try {
return data.query.pages[ 0 ].revisions[ 0 ].content;
} catch ( e ) {
console.warn( '[Gadget][CardNumberRedirect] - Could not fetch content.' );
throw e;
}
} )();
var regex = /^[ \t]*([\d\-\w\/]{5,13})[ \t]*;[ \t]*(.*?)[ \t]*(;|\/\/|$)/gm;
var redirectsMap = {};
content.replace( regex, function( match, $1, $2 ) {
redirectsMap[ $1 ] = $2;
} );
return redirectsMap;
}
function redirect( number, name ) {
var nameWithoutHash = name.replace( /#/g, '' );
return api
.postWithToken( 'csrf', {
action: 'edit',
title: number,
createonly: true,
text: '#REDIRECT [[' + nameWithoutHash + ']] {{R from card number}}',
summary: 'Creating redirects: Redirected page to [[' + nameWithoutHash + ']].',
bot: true,
} )
.done( function() {
console.log( '[Gadget][CardNumberRedirect] - redirected', number, 'to', name );
} )
[ 'catch' ](
console.error.bind(
console,
'[Gadget][CardNumberRedirect] - Error creating redirect to',
name
)
)
;
}
function redirectAll( data ) {
var redirectMap = getRedirectMap( data );
var chain = Promise.resolve();
for ( var cardNumber in redirectMap ) {
if ( redirectMap.hasOwnProperty( cardNumber ) ) {
( function( number, name ) {
chain = chain.then( function() {
return redirect( number, name );
} );
chain = chain.then( function() {
return new Promise( function( resolve ) {
window.setTimeout( resolve, window.CARD_NUMBER_REDIRECT_DELAY || 500 );
} );
} );
} )( cardNumber, redirectMap[ cardNumber ] );
}
}
chain.then( function() {
console.log( '[Gadget][CardNumberRedirect] - All done!' );
return mw.notify( 'All done!', {
title: 'Card number redirect',
tag: 'card_number_redirect',
} );
} );
}
function onRedirectCardNumbersClick( e ) {
e.preventDefault();
requestContent()
.done( redirectAll )
.fail(
console.error.bind(
console,
'[Gadget][CardNumberRedirect] - Error:'
)
)
;
}
function init() {
if ( config.wgNamespaceNumber === 3006 ) {
var $button = $( '<li>', {
id: 'ca-redirect',
'class': 'collapsible',
html: $( '<a>', {
href: '#',
title: 'Create card number redirects.',
text: 'Redirect'
} ),
click: function( e ) {
e.preventDefault();
console.error( '[Gadget][CardNumberRedirect] - Waiting for mediawiki.api and mediawiki.notify modules to load.' );
},
} );
$( '#p-cactions' )
.removeClass( 'emptyPortlet' )
.find( '.menu' )
.find( 'ul' )
.append( $button )
;
mw.loader.using( [ 'mediawiki.api', 'mediawiki.notify' ] ).done( function() {
api = new mw.Api();
$button
.off( 'click' ) // Remove fallback click.
.click( onRedirectCardNumbersClick )
;
} );
}
}
$( init ); // </pre>
console.log( '[Gadget] CardNumberRedirect last updated at', LAST_LOG );
} )( window, window.jQuery, window.mediaWiki, window.console );
/* global mw */
/**
* Personal JS
*/
(function() {
"use strict";
var LAST_LOG = '13:27, 9 December 2023 (UTC)'; // <pre>
var config = mw.config.get([
'wgPageName'
]);
/**
* Add a link to Special:PrefiIndex to the sidebar.
*/
$( '#p-tb' ).find( 'ul' ).append( $( '<li>', {
id: 't-prefixindex',
html: $( '<a>', {
href: '/wiki/Special:PrefixIndex/' + config.wgPageName,
text: 'Prefix index',
} ),
} ) );
console.log( 'User common.js last updated at', LAST_LOG );
// </pre>
})();