MediaWiki:Gadget-quickvfd.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes.
- Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
- Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
- Opera, Konqueror and Safari users can just click the Reload button.
- Chrome: press Ctrl+F5 or Shift+F5
// * @name Quickvfd.js
// * @dependencies jquery.dialog, mediawiki
// * @source
// * @revision 2.0.1
// * @revision date Jun 3, 2015
// * @author: Bizzeebeever, http://en.uncyclopedia.co/wiki/User:Bizzeebeever
// * @license unknown
// * @comment Allow quick adding of pages to [[UN:QVFD]]
// * @comment Original by [[User:Spang]] and [[User:Lyrithya]] (FU SPANG!)
//<nowiki>
;( function( $ ) {
var boxId = '#qvfd-box',
comment = '',
pageToQvfd,
pageName = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ),
hasHistory = !!$( '#ca-history' ).length,
isRedirect = !!$( '#contentSub' ).text().match( /^redirect page$/i ),
isRcOrNew = !!pageName.match( /Special:(NewPages|RecentChanges)/i ),
scriptPath = mw.config.get( 'wgScriptPath' ) + '/api.php',
// fix for IE instances with no open console
console = window.console || { log: function() { } },
savePage = 'Uncyclopedia:QuickVFD';
// $( document ).ready() equivalent
// This can be done before the actual function declaration because of "hoisting"
$( init );
function init() {
if ( hasHistory ) {
addControl();
} else if ( isRcOrNew ) {
addLinks();
}
// add a handler to make the input-box execute on ENTER press
$( document ).on( 'keydown', '#qvfd-comment', function( e ) {
if ( e.which == 13 ) {
$( boxId ).trigger( 'qvfd:ok' );
}
} );
}
function addControl() {
$( mw.util.addPortletLink( 'p-cactions', '#', 'QVFD', 't-qvfd', 'Add page to QuickVFD' ) )
.click( function( e ) { run( e, pageName ); } );
}
function addLinks() {
$( '#mw-content-text li, #mw-content-text table' ).each( function() {
var $t = $( this ),
title = $t.find( 'a.mw-changeslist-title, a.mw-newpages-pagename' ).attr( 'title' ),
link = $( '<a/>', {
href: 'javascript:void(0)',
text: 'qvfd',
click: function( e ) { run( e, title ); }
} );
if ( pageName.match( /Special:NewPages/i ) ) {
$t.find( '.mw-newpages-history' ).after( ')' ).after( link ).after( ' (' );
} else {
$t.find( 'a[href*="action=history"]' ).after( link ).after( ' | ' );
}
} );
}
function run( e, title ) {
pageToQvfd = title;
e.preventDefault();
$( '#qvfd-box' ).dialog( 'close' );
$( '<div id="qvfd-box"></div>' )
.append( '<p>Add comment:</p><input type="text" id="qvfd-comment" placeholder="optional"/>' )
.dialog( {
buttons: [
{ text: 'Go', click: function() { $( this ).trigger( 'qvfd:ok' ); } },
{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }
],
closeOnEscape: false,
modal: true,
close: function() {
$( this ).dialog( 'destroy' ).remove();
},
title: 'Quick-deletion request (QVFD)'
} )
.bind( 'qvfd:ok', fetch );
}
function fetch() {
comment = $( '#qvfd-comment' ).val();
$( boxId ).empty()
.css( 'font-size', '80%' )
.dialog( 'option', 'title', 'QVFDing, please wait...' )
.dialog( 'option', 'buttons', [] );
output( 'info', 'Loading', 'QVFD page...' );
$.get( scriptPath, {
'action': 'query',
'titles': savePage,
'prop': 'revisions|info|links',
'pllimit': 500,
'rvprop': 'content',
'rvlimit': 1,
'rvsection': 1,
'intoken': 'edit',
'format': 'xml'
} )
.done( fetchDone )
.fail( fetchFail );
}
function isListed( $xml, title ) {
var listed = false;
$xml.find( 'pl' ).each( function() {
if ( $( this ).attr( 'title' ) == title ) {
listed = true;
}
} );
return listed;
}
function fetchDone( xml ) {
var $xml = $( xml ),
sectionText,
line = ' ' + ( isRedirect ? '{{redirect|' + pageToQvfd + '}}' : '[[:' + pageToQvfd + ']]' )
+ ( comment ? ' — ' + comment : '' );
output( 'info', 'Checking', 'QVFD for selected page...' );
if ( isListed( $xml, pageToQvfd ) ) {
output( 'fail', 'Page', 'already on QVFD!' );
addBackButtons();
return;
}
output( 'info', 'Page', 'not listed, adding it now...' );
sectionText = $xml.find( 'rev:first' ).text().replace( /==(\n|$)/, '==\n' + line + '\n' );
$.post( scriptPath, {
'action': 'edit',
'title': savePage,
'section': 1,
'token': $xml.find( 'page' ).attr( 'edittoken' ),
'summary': '+[[' + pageToQvfd + ']] - via [[MediaWiki:Gadget-quickvfd.js|QVFD gadget]]',
'text': sectionText,
'watchlist': 'nochange',
'format': 'xml'
} )
.done( editDone )
.fail( editFail );
}
function fetchFail( error ) {
output( 'fail', 'Couldn\'t', 'connect to QVFD!' );
addBackButtons();
console.log( error );
}
function editDone( xml ) {
var $xml = $( xml );
if ( $xml.find( 'edit[result="Success"]' ).length ) {
output( 'win', 'Saved!' );
} else {
var $error = $xml.find( 'error' );
output( 'fail', 'Saving failed',
'Error: ' + $error.attr( 'code' ) + ' - ' + $error.attr( 'info' ) );
}
addBackButtons();
}
function editFail() {
output( 'fail', 'Connection failed' );
addBackButtons();
}
function addBackButtons() {
$( boxId ).dialog( 'option', { buttons: [
{
text: 'Go to QVFD',
click: function() {
$( boxId ).dialog( 'close' );
document.location.href = mw.util.wikiUrlencode( savePage );
}
},
{ text: 'Close', click: function() { $( boxId ).dialog( 'close' ); } }
] } );
}
function output( type, label, text ) {
var indicator = type == 'fail' ? ' x ' : type == 'win' ? ' √ ' : ' ► ',
str = '<p>' + indicator + '<b>' + ( label || '' ) + '</b> ' + ( text || '' ) + '</p>';
$( boxId ).append( str )
.css( 'color', ( type == 'fail' ? 'red' : type == 'win' ? 'green' : 'black' ) );
}
})( jQuery );