User:Thomas.tarak.mathew/common.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. |
![]() | The accompanying .css page for this skin is at User:Thomas.tarak.mathew/common.css. |
//mw.loader.load("//www.mediawiki.org/w/index.php?title=User:Thomas.tarak.mathew/common.js&action=raw&ctype=text/javascript");
mw.loader.load("//www.mediawiki.org/w/index.php?title=User:Thomas.tarak.mathew/common.css&action=raw&ctype=text/css", "text/css" );
/**
* Tutorial script: QuickRC ("Quick Recent Changes")
*
* A tutorial user script which adds a link to the toolbox to show a pop-up
* dialog with up to 25 recent edits.
*
* Demonstrates:
* - Use of the API
* - Use of jQuery
* - Use of ResourceLoader and some of the default modules that come with it
*
* (Be bold and improve it!)
*
* Authors:
* Erik Moeller, 2011, public domain
*
*/
// Import the jQuery dialog plugin before starting the rest of this script
mw.loader.using(['jquery.ui'], function() {
function renderQuickRCDialog( pageLinks ) {
var $dialog = $( '<div></div>' )
.html(
'<strong>Welcome, ' + mw.user.getName() +
'!</strong> The following pages have been recently modified:<br/><ul><li>' +
pageLinks.join( '<br /><li>' ) + '</ul>'
)
.dialog({
autoOpen: true,
title: 'Hello there!',
width: '70%',
modal: true
});
}
function quickRC() {
var myPageLinks = [];
var myTitles = [];
// Fetch recent changes from the API by one of jQuery's AJAX functions
jQuery.getJSON(
mw.util.wikiScript( 'api' ),
{
'format': 'json',
'action': 'query',
'list': 'recentchanges',
'rclimit' : 25
},
function( data ) {
// Build a unique array of links, using the mw.html library to format them.
$.each ( data.query.recentchanges , function( index , rc ) {
// Don't link to this title if we've seen this title already
if ( $.inArray( rc.title, myTitles ) === -1 ) {
myPageLinks.push(
mw.html.element(
'a', { href: mw.util.getUrl( rc.title ) }, rc.title
)
);
}
myTitles.push( rc.title );
} ) ;
renderQuickRCDialog( myPageLinks );
}
);
}
$(document).ready( function() {
//Adding lang links to the page
var langs;
var api = new mw.Api();
api.get( {
action: 'query',
prop: 'langlinks',
lllimit: '10',
titles: $('h1').text()
} )
.done( function ( data ) {
console.log( 'API result:', data );
$.each(data.query.pages, function(key, value) {
langs = "This article is also available as ";
$.each(value.langlinks, function(i, val) {
langs += " <a href='https://" + val["lang"] + ".wikipedia.org/wiki/"+ val["*"] +"'>" + val["*"] + "</a>, ";
});
if('query-continue' in data){
langs += "<a id='extralangs' > and many more</a>"; /*href='"+value.title+"_langs'*/
}
console.log( langs );
langs += "<br />";
$('#bodyContent').prepend(langs);
if($('#extralangs').length > 0){
$('#extralangs').click(function(){
$(document).scrollTop($('#p-lang').offset().top);
});
}
});
} )
.fail( function ( error ) {
console.log( 'API failed :(', error );
langs = " This article is only available in this language";
$('#bodyContent').prepend(langs);
} );
// Add a link to the toolbox
var link = mw.util.addPortletLink(
'p-tb',
'#',
'Quick changelog',
't-prettylinkwidget',
'Show a quick overview of changes',
null,
'#t-whatlinkshere'
);
// Create a jQuery object for this link so that we get
// to use jQuery awesomeness like .click() for binding functions to events
// and methods like e.preventDefault();
$(link).click( function( e ) {
// Avoid the browser going to '#'
e.preventDefault();
// Initiate quickRC!
quickRC();
});
});
});