Jump to content

User:Nirmos/global.js

From Meta, a Wikimedia project coordination wiki

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.loader.using( [ 'mediawiki.util', 'mediawiki.api', 'mediawiki.ForeignApi' ], function() {
	'use strict';
	mw.loader.load( 'https://sv.wikipedia.org/wiki/MediaWiki:Gadget-WikidataLabel.js?action=raw&ctype=text/javascript' );
	$( function() {
		var u = mw.util;
		var v = mw.config.get();
		if ( v.wgContentLanguage !== 'sv' ) {
			( new mw.ForeignApi( 'https://sv.wikipedia.org/w/api.php' ) ).get( {
				action: 'query',
				prop: 'revisions',
				titles: 'MediaWiki:Gadget-iwsort.json',
				rvprop: 'content',
				rvslots: 'main',
				formatversion: 2
			} ).done( function( data ) {
				var obj;
				if (
					data &&
					data.query &&
					data.query.pages &&
					data.query.pages[ 0 ] &&
					data.query.pages[ 0 ].revisions &&
					data.query.pages[ 0 ].revisions[ 0 ] &&
					data.query.pages[ 0 ].revisions[ 0 ].slots &&
					data.query.pages[ 0 ].revisions[ 0 ].slots.main &&
					data.query.pages[ 0 ].revisions[ 0 ].slots.main.content
				) {
					obj = JSON.parse( data.query.pages[ 0 ].revisions[ 0 ].slots.main.content );
					if ( obj ) {
						if ( obj[ v.wgContentLanguage ] ) {
							document.title = document.title + ' (' + v.wgContentLanguage + ', ' + obj[ v.wgContentLanguage ].toLowerCase() + ')';
						} else {
							document.title = document.title + ' (' + v.wgContentLanguage + ')';
						}
					}
				}
			} );
		}
		if ( v.wgIsArticle ) {
			u.addPortletLink(
				'p-cactions',
				'/api/rest_v1/page/html/' + u.rawurlencode( v.wgPageName ),
				'Parsoid',
				'ca-parsoid'
			);
			u.addPortletLink(
				'p-cactions',
				u.getUrl( v.wgPageName, {
					action: 'purge'
				} ),
				'Purge',
				'ca-purge'
			);
			$( '#ca-purge' ).click( function( e ) {
				e.preventDefault();
				( new mw.Api() ).post( {
					action: 'purge',
					titles: v.wgPageName
				} ).done( function() {
					location.reload( true );
				} );
			} );
		}
	} );
} );