MediaWiki:Gadget-sidebar-tracking.js

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.
 1 ;(function($, mw){
 2 	function trackSidebarClick(event) {
 3 		var $e = $(event.currentTarget),
 4 		    pagename = mw.config.get('wgPageName'),
 5 		    href,
 6 		    click_type,
 7 		    data;
 8 		href = $e.attr('href');
 9 		switch (event.which) {
10 			case 1:
11 				click_type = 'left';
12 				if (!(event.altKey || event.ctrlKey || event.altKey || event.metaKey)) {
13 					$e.attr({'x-href': href, 'x-leftclicked':'1'}).removeAttr('href');
14 				}
15 				break;
16 			case 2:
17 				click_type = 'middle';
18 				break;
19 			case 3:
20 				click_type = 'right';
21 				break;
22 			default:
23 				click_type = 'other: '+event.which;
24 		}
25 		if (event.shiftKey) {
26 			click_type += '-shift';
27 		}
28 		if (event.ctrlKey) {
29 			click_type += '-control';
30 		}
31 		if (event.altKey) {
32 			click_type += '-alt';
33 		}
34 		if (event.metaKey) {
35 			click_type += '-meta';
36 		}
37 		data = {
38 				page: pagename,
39 				link: href,
40 				click: click_type,
41 				wiki: mw.config.get('wgDBname')
42 		};
43 		console.log('Sending sidebar click data:', data);
44 		var req = $.ajax('https://chisel.weirdgloop.org/gazproj/track/sidebar', {
45 			method: 'POST',
46 			data: data
47 		});
48 		req.done(function(d, s, xhr){
49 			console.log('Data (success): ', d, s, xhr);
50 			if (click_type === 'left' && $e.attr('x-leftclicked') === '1') {
51 				$e.attr({'href':$e.attr('x-href'), 'x-leftclicked':'0'});
52 				$e.get(0).click();
53 			}
54 		});
55 		req.fail(function(d, s, xhr){
56 			console.log('Data (fail): ', d, s, xhr);
57 			if (click_type === 'left' && $e.attr('x-leftclicked') === '1') {
58 				$e.attr({'href':$e.attr('x-href'), 'x-leftclicked':'0'});
59 				$e.get(0).click();
60 			}
61     	});
62 	}
63 	function init(){
64 		$('#mw-panel a[href]').on('mousedown', trackSidebarClick);
65 	}
66 	$(init);
67 })(jQuery, mw);