User:Technical 13/SandBox/getPageViews.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:Technical 13/SandBox/getPageViews. |
// Licensed under the MIT license; theopolismewiki@gmail.com
( function ( $, mw ) {
function sum ( obj ) {
var total;
for (var prop in obj ) {
if ( obj.hasOwnProperty( prop ) ) {
total += parseInt( obj[prop], 10 );
}
}
return total;
}
function getPageData( pagename, language ) {
var deferred = $.Deferred(),
request3 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest30/' + pagename ),
request6 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest60/' + pagename ),
request9 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest90/' + pagename );
$.when( request3, request6, request9 ).done( function ( data3, data6, data9 ) {
deferred.resolve( {
title: data3.title,
rank: data3.rank,
hits30: sum( data3.daily_views ),
hits60: sum( data6.daily_views ),
hits90: sum( data9.daily_views )
} );
} );
return deferred;
}
function getPagesTable( pages, language ) {
var table = $( '<div>' ), requests = [];
$.each( pages, function ( i, page ) {
requests.push( getPageData( page, language ) );
} );
$.when.apply( $, requests ).done( function () {
$.each( requests, function ( i, request ) {
request.done( function ( data ) {
table.append( $( '<div>' ).text( JSON.stringify( data ) ) );
} );
} );
} );
}
var pages = prompt( 'Enter page names, separated by pipes', mw.config.get( 'wgTitle' ) ).split( '|' ),
language = mw.config.get( 'wgPageContentLanguage' );
$( '#mw-content-text' ).prepend( getPagesTable( pages, language ) );
} )( jQuery, mediaWiki );