User:Gauransh Dingwani/API-Demo.js

From mediawiki.org

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.loader.using( 'mw.Api' ).then( function () {
	// Call to the function that uses mw.Api
	var api = new mw.Api();
	api.get( {
		action: 'query',
		prop: 'pageviews',
		titles: [ mw.config.get( 'wgPageName' ) ]
	} ).done( function ( data ) {
		const object = data.query.pages[ mw.config.get( 'wgArticleId' ) ].pageviews;
		var sum = 0;
		for ( const property in object ) {
			sum += object[ property ];
		}
		var label = ' ( ' + sum + ' views in last 2 months )';
		$( '#firstHeading' ).append( label );
	} );
} );