User:AntiCompositeNumber/quicktag.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. |
Documentation for this user script can be added at User:AntiCompositeNumber/quicktag. |
///////////////////////////////////////////////////////////////////////////////
// This code is cobbled together from various examples and libraries. //
//It is not designed for use by any other user, as some things are hardcoded.//
// There's probably a better way of doing this. //
///////////////////////////////////////////////////////////////////////////////
/* This section adds a toolbox link to file pages. */
mw.loader.using( 'mediawiki.util', function () {
// Wait for the page to be parsed
$(document).ready( function () {
if ( mw.config.get( 'wgCanonicalNamespace' ) === 'File') {
var acmtcLink = mw.util.addPortletLink( 'p-tb', '#', 'Commons Ready', 'ca-acmtc', 'Mark for Commons');
$( acmtcLink ).click( function ( event ) {
event.preventDefault();
editPage({
title: mw.config.get( 'wgPageName' ),
text: '{'+"{Copy to Wikimedia Commons|human=AntiCompositeNumber}} \n",
summary: 'Preparing to move to Commons using [[User:AntiCompositeNumber/quicktag.js]]'
});
});
}
});
});
/* This section uses AJAX to add <nowiki>{{Copy to Wikimedia Commons|human=AntiCompositeNumber}}</nowiki> to the page when the link is clicked */
function editPage( info ) {
$.ajax({
url: mw.util.wikiScript( 'api' ),
type: 'POST',
dataType: 'json',
data: {
format: 'json',
action: 'edit',
title: info.title,
prependtext: info.text,
summary: info.summary,
token: mw.user.tokens.get( 'editToken' )
}
})
.done (function( data ) {
if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
patrolPage();
setTimeout(function(){ location.reload(); }, 3000);
} else {
alert( 'The edit query returned an error. =(' );
}
})
.fail ( function() {
alert( 'The ajax request failed.' );
});
}
/* The page is patrolled for good measure.
This section taken from Twinkle's library. */
var patrolPage = function() {
// There's no patrol link on page, so we can't patrol
if ( !$( '.patrollink' ).length ) {
return;
}
// Extract the rcid token from the "Mark page as patrolled" link on page
var patrolhref = $( '.patrollink a' ).attr( 'href' ),
rcid = mw.util.getParamValue( 'rcid', patrolhref );
if ( rcid ) {
var patrolstat = new Morebits.status( 'Marking page as patrolled' );
var wikipedia_api = new Morebits.wiki.api( 'doing...', {
action: 'patrol',
rcid: rcid,
token: mw.user.tokens.get( 'patrolToken' )
}, null, patrolstat );
// We don't really care about the response
wikipedia_api.post();
}
};