1// GoatCounter: https://www.goatcounter.com 2// This file is released under the ISC license: https://opensource.org/licenses/ISC 3;(function() { 4 'use strict'; 5 6 window.goatcounter = window.goatcounter || {} 7 8 // Load settings from data-goatcounter-settings. 9 var s = document.querySelector('script[data-goatcounter]') 10 if (s && s.dataset.goatcounterSettings) { 11 try { var set = JSON.parse(s.dataset.goatcounterSettings) } 12 catch (err) { console.error('invalid JSON in data-goatcounter-settings: ' + err) } 13 for (var k in set) 14 if (['no_onload', 'no_events', 'allow_local', 'allow_frame', 'path', 'title', 'referrer', 'event'].indexOf(k) > -1) 15 window.goatcounter[k] = set[k] 16 } 17 18 var enc = encodeURIComponent 19 20 // Get all data we're going to send off to the counter endpoint. 21 window.goatcounter.get_data = function(vars) { 22 vars = vars || {} 23 var data = { 24 p: (vars.path === undefined ? goatcounter.path : vars.path), 25 r: (vars.referrer === undefined ? goatcounter.referrer : vars.referrer), 26 t: (vars.title === undefined ? goatcounter.title : vars.title), 27 e: !!(vars.event || goatcounter.event), 28 s: window.screen.width, 29 b: is_bot(), 30 q: location.search, 31 } 32 33 var rcb, pcb, tcb // Save callbacks to apply later. 34 if (typeof(data.r) === 'function') rcb = data.r 35 if (typeof(data.t) === 'function') tcb = data.t 36 if (typeof(data.p) === 'function') pcb = data.p 37 38 if (is_empty(data.r)) data.r = document.referrer 39 if (is_empty(data.t)) data.t = document.title 40 if (is_empty(data.p)) data.p = get_path() 41 if (vars.no_session) data.ns = (typeof(vars.no_session) === 'function' ? vars.no_session(false) : vars.no_session) 42
43 if (rcb) data.r = rcb(data.r) 44 if (tcb) data.t = tcb(data.t) 45 if (pcb) data.p = pcb(data.p) 46 return data 47 } 48 49 // Check if a value is "empty" for the purpose of get_data(). 50 var is_empty = function(v) { return v === null || v === undefined || typeof(v) === 'function' } 51 52 // See if this looks like a bot; there is some additional filtering on the 53 // backend, but these properties can't be fetched from there. 54 var is_bot = function() { 55 // Headless browsers are probably a bot. 56 var w = window, d = document 57 if (w.callPhantom || w._phantom || w.phantom) 58 return 150 59 if (w.__nightmare) 60 return 151 61 if (d.__selenium_unwrapped || d.__webdriver_evaluate || d.__driver_evaluate) 62 return 152 63 if (navigator.webdriver) 64 return 153 65 return 0 66 } 67 68 // Object to urlencoded string, starting with a ?. 69 var urlencode = function(obj) { 70 var p = [] 71 for (var k in obj) 72 if (obj[k] !== '' && obj[k] !== null && obj[k] !== undefined && obj[k] !== false) 73 p.push(enc(k) + '=' + enc(obj[k])) 74 return '?' + p.join('&') 75 } 76 77 // Show a warning in the console. 78 var warn = function(msg) { 79 if (console && 'warn' in console) 80 console.warn('goatcounter: ' + msg) 81 } 82 83 // Get the endpoint to send requests to. 84 var get_endpoint = function() { 85 var s = document.querySelector('script[data-goatcounter]') 86 return (s && s.dataset.goatcounter) ? s.dataset.goatcounter : goatcounter.endpoint 87 } 88 89 // Get current path. 90 var get_path = function() { 91 var loc = location, 92 c = document.querySelector('link[rel="canonical"][href]') 93 if (c) { // May be relative or point to different domain. 94 var a = document.createElement('a') 95 a.href = c.href 96 if (a.hostname.replace(/^www\./, '') === location.hostname.replace(/^www\./, '')) 97 loc = a 98 } 99 return (loc.pathname + loc.search) || '/' 100 } 101 102 // Run function after DOM is loaded. 103 var on_load = function(f) { 104 if (document.body === null) 105 document.addEventListener('DOMContentLoaded', function() { f() }, false) 106 else 107 f() 108 } 109 110 // Filter some requests that we (probably) don't want to count. 111 window.goatcounter.filter = function() { 112 if ('visibilityState' in document && document.visibilityState === 'prerender') 113 return 'visibilityState' 114 if (!goatcounter.allow_frame && location !== parent.location) 115 return 'frame' 116 if (!goatcounter.allow_local && location.hostname.match(/(localhost$|^127\.|^10\.|^172\.(1[6-9]|2[0-9]|3[0-1])\.|^192\.168\.|^0\.0\.0\.0$)/)) 117 return 'localhost' 118 if (!goatcounter.allow_local && location.protocol === 'file:') 119 return 'localfile' 120 if (localStorage && localStorage.getItem('skipgc') === 't') 121 return 'disabled with #toggle-goatcounter' 122 return false 123 } 124 125 // Get URL to send to GoatCounter. 126 window.goatcounter.url = function(vars) { 127 var data = window.goatcounter.get_data(vars || {}) 128 if (data.p === null) // null from user callback. 129 return 130 data.rnd = Math.random().toString(36).substr(2, 5) // Browsers don't always listen to Cache-Control. 131 132 var endpoint = get_endpoint() 133 if (!endpoint) 134 return warn('no endpoint found') 135 136 return endpoint + urlencode(data) 137 } 138 139 // Count a hit. 140 window.goatcounter.count = function(vars) { 141 var f = goatcounter.filter() 142 if (f) 143 return warn('not counting because of: ' + f) 144 var url = goatcounter.url(vars) 145 if (!url) 146 return warn('not counting because path callback returned null') 147 148 if (!navigator || !navigator.sendBeacon || !navigator.sendBeacon(url)) { 149 // This mostly fails due to being blocked by CSP; try again with an 150 // image-based fallback. 151 var img = document.createElement('img') 152 img.src = url 153 img.style.position = 'absolute' // Affect layout less. 154 img.style.bottom = '0px' 155 img.style.width = '1px' 156 img.style.height = '1px' 157 img.loading = 'eager' 158 img.setAttribute('alt', '') 159 img.setAttribute('aria-hidden', 'true') 160 161 var rm = function() { if (img && img.parentNode) img.parentNode.removeChild(img) } 162 img.addEventListener('load', rm, false) 163 document.body.appendChild(img) 164 } 165 } 166 167 // Get a query parameter. 168 window.goatcounter.get_query = function(name) { 169 var s = location.search.substr(1).split('&') 170 for (var i = 0; i < s.length; i++) 171 if (s[i].toLowerCase().indexOf(name.toLowerCase() + '=') === 0) 172 return s[i].substr(name.length + 1) 173 } 174 175 // Track click events. 176 window.goatcounter.bind_events = function() { 177 if (!document.querySelectorAll) // Just in case someone uses an ancient browser. 178 return 179 180 var send = function(elem) { 181 return function() { 182 goatcounter.count({ 183 event: true, 184 path: (elem.dataset.goatcounterClick || elem.name || elem.id || ''), 185 title: (elem.dataset.goatcounterTitle || elem.title || (elem.innerHTML || '').substr(0, 200) || ''), 186 referrer: (elem.dataset.goatcounterReferrer || elem.dataset.goatcounterReferral || ''), 187 no_session: ['1', 't', 'true'].indexOf((elem.dataset.goatcounterNoSession || '').toLowerCase()) !== -1, 188 }) 189 } 190 } 191 192 Array.prototype.slice.call(document.querySelectorAll("*[data-goatcounter-click]")).forEac
192h(function(elem) { 193 if (elem.dataset.goatcounterBound) 194 return 195 var f = send(elem) 196 elem.addEventListener('click', f, false) 197 elem.addEventListener('auxclick', f, false) // Middle click. 198 elem.dataset.goatcounterBound = 'true' 199 }) 200 } 201 202 // Add a "visitor counter" frame or image. 203 window.goatcounter.visit_count = function(opt) { 204 on_load(function() { 205 opt = opt || {} 206 opt.type = opt.type || 'html' 207 opt.append = opt.append || 'body' 208 opt.path = opt.path || get_path() 209 opt.attr = opt.attr || {width: '200', height: (opt.no_branding ? '60' : '80')} 210 211 opt.attr['src'] = get_endpoint() + 'er/' + enc(opt.path) + '.' + enc(opt.type) + '?' 212 if (opt.no_branding) opt.attr['src'] += '&no_branding=1' 213 if (opt.style) opt.attr['src'] += '&style=' + enc(opt.style) 214 if (opt.start) opt.attr['src'] += '&start=' + enc(opt.start) 215 if (opt.end) opt.attr['src'] += '&end=' + enc(opt.end) 216 217 var tag = {png: 'img', svg: 'img', html: 'iframe'}[opt.type] 218 if (!tag) 219 return warn('visit_count: unknown type: ' + opt.type) 220 221 if (opt.type === 'html') { 222 opt.attr['frameborder'] = '0' 223 opt.attr['scrolling'] = 'no' 224 } 225 226 var d = document.createElement(tag) 227 for (var k in opt.attr) 228 d.setAttribute(k, opt.attr[k]) 229 230 var p = document.querySelector(opt.append) 231 if (!p) 232 return warn('visit_count: element to append to not found: ' + opt.append) 233 p.appendChild(d) 234 }) 235 } 236 237 // Make it easy to skip your own views. 238 if (location.hash === '#toggle-goatcounter') { 239 if (localStorage.getItem('skipgc') === 't') { 240 localStorage.removeItem('skipgc', 't') 241 alert('GoatCounter tracking is now ENABLED in this browser.') 242 } 243 else { 244 localStorage.setItem('skipgc', 't') 245 alert('GoatCounter tracking is now DISABLED in this browser until ' + location + ' is loaded again.') 246 } 247 } 248 249 if (!goatcounter.no_onload) 250 on_load(function() { 251 // 1. Page is visible, count request. 252 // 2. Page is not yet visible; wait until it switches to 'visible' and count. 253 // See #487 254 if (!('visibilityState' in document) || document.visibilityState === 'visible') 255 goatcounter.count() 256 else { 257 var f = function(e) { 258 if (document.visibilityState !== 'visible') 259 return 260 document.removeEventListener('visibilitychange', f) 261 goatcounter.count() 262 } 263 document.addEventListener('visibilitychange', f) 264 } 265 266 if (!goatcounter.no_events) 267 goatcounter.bind_events() 268 }) 269})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.