MediaWiki:Gadget-navbox-tracking.js

After saving, you may need to bypass your browser's cache to see the changes. For further information, see Wikipedia:Bypass your cache.

  • In most Windows and Linux browsers: Hold down Ctrl and press F5.
  • In Safari: Hold down ⇧ Shift and click the Reload button.
  • In Chrome and Firefox for Mac: Hold down both ⌘ Cmd+⇧ Shift and press R.
;(function($, mw){
	if ($('.navbox').length <= 0) return;
	var LOADING = false;
	function trackNavboxClick(event) {
		var $e = $(event.currentTarget),
		    pagename = mw.config.get('wgPageName'),
		    href,
		    navbox,
		    link_type = ['link'],
		    click_type,
		    data;
		href = $e.attr('href');
		navbox = $e.parents('.navbox[data-navbox-name]');
		if (navbox.length<1) {
			// missing name, template not propagated - skip
			return;
		}
		navbox = navbox.attr('data-navbox-name');
		if ($e.find('img').length>0) {
			link_type.push('image');
			if ($e.parent().is('span.inventory-image')) {
				link_type.push('inventory');
			}
			if ($e.parent().is('span.chathead-link')) {
				link_type.push('chathead');
			}
		}
		if ($e.parents('th.navbox-title').length>0) {
			link_type.push('navboxtitle');
		}
		if ($e.parent().is('td.navbox-group-title')) {
			link_type.push('navboxgrouptitle');
			link_type.push('navboxgroup-'+$e.parents('tr.navbox-group').length)
		}
		if ($e.parents('div.navbar').length>0) {
			link_type.push('navbar');
		}
		if ($e.parents('sup').length>0) {
			link_type.push('sup');
		}
		if ($e.parents('sub').length>0) {
			link_type.push('sub');
		}
		switch (event.which) {
			case 1:
				click_type = 'left';
				if (!(event.altKey || event.ctrlKey || event.altKey || event.metaKey)) {
					$e.attr({'x-href': href, 'x-leftclicked':'1'}).removeAttr('href');
				}
				break;
			case 2:
				click_type = 'middle';
				break;
			case 3:
				click_type = 'right';
				break;
			default:
				click_type = 'other: '+event.which;
		}
		if (event.shiftKey) {
			click_type += '-shift';
		}
		if (event.ctrlKey) {
			click_type += '-control';
		}
		if (event.altKey) {
			click_type += '-alt';
		}
		if (event.metaKey) {
			click_type += '-meta';
		}
		data = {
				page: pagename,
				link: href,
				navbox: navbox,
				'type': link_type.join(' '),
				click: click_type,
				wiki: mw.config.get('wgDBname')
		};
		console.log('Sending navbox click data:', data);
		var req = $.ajax('https://chisel.weirdgloop.org/gazproj/track/navbox', {
			method: 'POST',
			data: data
		});
		req.done(function(d, s, xhr){
			console.log('Data (success): ', d, s, xhr);
			if (click_type === 'left' && $e.attr('x-leftclicked') === '1') {
				$e.attr({'href':$e.attr('x-href'), 'x-leftclicked':'0'});
				$e.get(0).click();
			}
		});
		req.fail(function(d, s, xhr){
			console.log('Data (fail): ', d, s, xhr);
			if (click_type === 'left' && $e.attr('x-leftclicked') === '1') {
				$e.attr({'href':$e.attr('x-href'), 'x-leftclicked':'0'});
				$e.get(0).click();
			}
    	});
	}
	function init(){
		$('.navbox a[href]').on('mousedown', trackNavboxClick);
	}
	$(init);
})(jQuery, mw);