MediaWiki:Gadget-navbox-tracking.js
Jump to navigation
Jump to search
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 if ($('.navbox').length <= 0) return;
3 var LOADING = false;
4 function trackNavboxClick(event) {
5 var $e = $(event.currentTarget),
6 pagename = mw.config.get('wgPageName'),
7 href,
8 navbox,
9 link_type = ['link'],
10 click_type,
11 data;
12 href = $e.attr('href');
13 navbox = $e.parents('.navbox[data-navbox-name]');
14 if (navbox.length<1) {
15 // missing name, template not propagated - skip
16 return;
17 }
18 navbox = navbox.attr('data-navbox-name');
19 if ($e.find('img').length>0) {
20 link_type.push('image');
21 if ($e.parent().is('span.inventory-image')) {
22 link_type.push('inventory');
23 }
24 if ($e.parent().is('span.chathead-link')) {
25 link_type.push('chathead');
26 }
27 }
28 if ($e.parents('th.navbox-title').length>0) {
29 link_type.push('navboxtitle');
30 }
31 if ($e.parent().is('td.navbox-group-title')) {
32 link_type.push('navboxgrouptitle');
33 link_type.push('navboxgroup-'+$e.parents('tr.navbox-group').length)
34 }
35 if ($e.parents('div.navbar').length>0) {
36 link_type.push('navbar');
37 }
38 if ($e.parents('sup').length>0) {
39 link_type.push('sup');
40 }
41 if ($e.parents('sub').length>0) {
42 link_type.push('sub');
43 }
44 switch (event.which) {
45 case 1:
46 click_type = 'left';
47 if (!(event.altKey || event.ctrlKey || event.altKey || event.metaKey)) {
48 $e.attr({'x-href': href, 'x-leftclicked':'1'}).removeAttr('href');
49 }
50 break;
51 case 2:
52 click_type = 'middle';
53 break;
54 case 3:
55 click_type = 'right';
56 break;
57 default:
58 click_type = 'other: '+event.which;
59 }
60 if (event.shiftKey) {
61 click_type += '-shift';
62 }
63 if (event.ctrlKey) {
64 click_type += '-control';
65 }
66 if (event.altKey) {
67 click_type += '-alt';
68 }
69 if (event.metaKey) {
70 click_type += '-meta';
71 }
72 data = {
73 page: pagename,
74 link: href,
75 navbox: navbox,
76 'type': link_type.join(' '),
77 click: click_type,
78 wiki: mw.config.get('wgDBname')
79 };
80 console.log('Sending navbox click data:', data);
81 var req = $.ajax('https://chisel.weirdgloop.org/gazproj/track/navbox', {
82 method: 'POST',
83 data: data
84 });
85 req.done(function(d, s, xhr){
86 console.log('Data (success): ', d, s, xhr);
87 if (click_type === 'left' && $e.attr('x-leftclicked') === '1') {
88 $e.attr({'href':$e.attr('x-href'), 'x-leftclicked':'0'});
89 $e.get(0).click();
90 }
91 });
92 req.fail(function(d, s, xhr){
93 console.log('Data (fail): ', d, s, xhr);
94 if (click_type === 'left' && $e.attr('x-leftclicked') === '1') {
95 $e.attr({'href':$e.attr('x-href'), 'x-leftclicked':'0'});
96 $e.get(0).click();
97 }
98 });
99 }
100 function init(){
101 $('.navbox a[href]').on('mousedown', trackNavboxClick);
102 }
103 $(init);
104 })(jQuery, mw);