MediaWiki:Gadget-sidebar-tracking.js

From RuneRealm Wiki
Jump to navigation Jump to search

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){
	function trackSidebarClick(event) {
		var $e = $(event.currentTarget),
		    pagename = mw.config.get('wgPageName'),
		    href,
		    click_type,
		    data;
		href = $e.attr('href');
		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,
				click: click_type,
				wiki: mw.config.get('wgDBname')
		};
		console.log('Sending sidebar click data:', data);
		var req = $.ajax('https://chisel.weirdgloop.org/gazproj/track/sidebar', {
			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(){
		$('#mw-panel a[href]:not([target])').on('mousedown', trackSidebarClick);
	}
	$(init);
})(jQuery, mw);