User:Guycn2/IPLookup.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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*

Adds a menu link to an external website displaying metadata
and geolocation info about the relevant IP address.

Written by: [[User:Guycn2]]

*/

( async () => {
	
	'use strict';
	
	const username = mw.config.get( 'wgRelevantUserName' );
	
	if ( !username ) {
		return;
	}
	
	await mw.loader.using( 'mediawiki.util' );
	
	if ( !mw.util.isIPAddress( username ) ) {
		return;
	}
	
	let messages;
	
	switch ( mw.config.get( 'wgUserLanguage' ) ) {
		case 'he':
			messages = {
				linkText: 'מידע על ה־IP',
				linkTooltip: 'הצגת מידע על כתובת IP זו'
			};
			break;
		default:
			messages = {
				linkText: 'IP lookup',
				linkTooltip: 'View information about this IP address'
			};
			break;
	}
	
	function i18n( key ) {
		return messages[ key ] || key;
	}
	
	function addLink() {
		
		mw.util.addPortletLink(
			'p-cactions',
			`https://whatismyipaddress.com/ip/${ username }`,
			i18n( 'linkText' ),
			'ca-ip-lookup',
			i18n( 'linkTooltip' )
		);
		
	}
	
	$( addLink );
	
} )();