1// GoatCounter: https://www.goatcounter.com 2// This file (and *only* this file) is released under the ISC license: 3// https://opensource.org/licenses/ISC 4;(function() { 5 'use strict'; 6 7 if (window.goatcounter && window.goatcounter.vars) // Compatibility with very old version; do not use. 8 window.goatcounter = window.goatcounter.vars 9 else 10 window.goatcounter = window.goatcounter || {} 11 12 // Load settings from data-goatcounter-settings. 13 var s = document.querySelector('script[data-goatcounter]') 14 if (s && s.dataset.goatcounterSettings) { 15 try { var set = JSON.parse(s.dataset.goatcounterSettings) } 16 catch (err) { console.error('invalid JSON in data-goatcounter-settings: ' + err) } 17 for (var k in set) 18 if (['no_onload', 'no_events', 'allow_local', 'allow_frame', 'path', 'title', 'referrer', 'event'].indexOf(k) > -1) 19 window.goatcounter[k] = set[k] 20 } 21 22 var enc = encodeURIComponent 23 24 // Get all data we're going to send off to the counter endpoint. 25 var get_data = function(vars) { 26 var data = { 27 p: (vars.path === undefined ? goatcounter.path : vars.path), 28 r: (vars.referrer === undefined ? goatcounter.referrer : vars.referrer), 29 t: (vars.title === undefined ? goatcounter.title : vars.title), 30 e: !!(vars.event || goatcounter.event), 31 s: [window.screen.width, window.screen.height, (window.devicePixelRatio || 1)], 32 b: is_bot(), 33 q: location.search, 34 } 35 36 var rcb, pcb, tcb // Save callbacks to apply later. 37 if (typeof(data.r) === 'function') rcb = data.r 38 if (typeof(data.t) === 'function') tcb = data.t 39 if (typeof(data.p) === 'function') pcb = data.p 40 41 if (is_empty(data.r)) data.r = document.referrer 42 if (is_empty(data.t)) data.t = document.title 43 if (is_empty(data.p)) data.p = get_path() 44
45 if (rcb) data.r = rcb(data.r) 46 if (tcb) data.t = tcb(data.t) 47 if (pcb) data.p = pcb(data.p) 48 return data 49 } 50 51 // Check if a value is "empty" for the purpose of get_data(). 52 var is_empty = function(v) { return v === null || v === undefined || typeof(v) === 'function' } 53 54 // See if this looks like a bot; there is some additional filtering on the 55 // backend, but these properties can't be fetched from there. 56 var is_bot = function() { 57 // Headless browsers are probably a bot. 58 var w = window, d = document 59 if (w.callPhantom || w._phantom || w.phantom) 60 return 150 61 if (w.__nightmare) 62 return 151 63 if (d.__selenium_unwrapped || d.__webdriver_evaluate || d.__driver_evaluate) 64 return 152 65 if (navigator.webdriver) 66 return 153 67 return 0 68 } 69 70 // Object to urlencoded string, starting with a ?. 71 var urlencode = function(obj) { 72 var p = [] 73 for (var k in obj) 74 if (obj[k] !== '' && obj[k] !== null && obj[k] !== undefined && obj[k] !== false) 75 p.push(enc(k) + '=' + enc(obj[k])) 76 return '?' + p.join('&') 77 } 78 79 // Show a warning in the console. 80 var warn = function(msg) { 81 if (console && 'warn' in console) 82 console.warn('goatcounter: ' + msg) 83 } 84 85 // Get the endpoint to send requests to. 86 var get_endpoint = function() { 87 var s = document.querySelector('script[data-goatcounter]') 88 if (s && s.dataset.goatcounter) 89 return s.dataset.goatcounter 90 return (goatcounter.endpoint || window.counter) // counter is for compat; don't use. 91 } 92 93 // Get current path. 94 var get_path = function() { 95 var loc = location, 96 c = document.querySelector('link[rel="canonical"][href]') 97 if (c) { // May be relative or point to different domain. 98 var a = document.createElement('a') 99 a.href = c.href 100 if (a.hostname.replace(/^www\./, '') === location.hostname.replace(/^www\./, '')) 101 loc = a 102 } 103 return (loc.pathname + loc.search) || '/' 104 } 105 106 // Run function after DOM is loaded. 107 var on_load = function(f) { 108 if (document.body === null) 109 document.addEventListener('DOMContentLoaded', function() { f() }, false) 110 else 111 f() 112 } 113 114 // Filter some requests that we (probably) don't want to count. 115 goatcounter.filter = function() { 116 if ('visibilityState' in document && document.visibilityState === 'prerender') 117 return 'visibilityState' 118 if (!goatcounter.allow_frame && location !== parent.location) 119 return 'frame' 120 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$)/)) 121 return 'localhost' 122 if (!goatcounter.allow_local && location.protocol === 'file:') 123 return 'localfile' 124 if (localStorage && localStorage.getItem('skipgc') === 't') 125 return 'disabled with #toggle-goatcounter' 126 return false 127 } 128 129 // Get URL to send to GoatCounter. 130 window.goatcounter.url = function(vars) { 131 var data = get_data(vars || {}) 132 if (data.p === null) // null from user callback. 133 return 134 data.rnd = Math.random().toString(36).substr(2, 5) // Browsers don't always listen to Cache-Control. 135 136 var endpoint = get_endpoint() 137 if (!endpoint) 138 return warn('no endpoint found') 139 140 return endpoint + urlencode(data) 141 } 142 143 // Count a hit. 144 window.goatcounter.count = function(vars) { 145 var f = goatcounter.filter() 146 if (f) 147 return warn('not counting because of: ' + f) 148 149 var url = goatcounter.url(vars) 150 if (!url) 151 return warn('not counting because path callback returned null') 152 153 var img = document.createElement('img') 154 img.src = url 155 img.style.position = 'absolute' // Affect layout less. 156 img.style.bottom = '0px' 157 img.style.width = '1px' 158 img.style.height = '1px' 159 img.loading = 'eager' 160 img.setAttribute('alt', '') 161 img.setAttribute('aria-hidden', 'true') 162 163 var rm = function() { if (img && img.parentNode) img.parentNode.removeChild(img) } 164 img.addEventListener('load', rm, false) 165 document.body.appendChild(img) 166 } 167 168 // Get a query parameter. 169 window.goatcounter.get_query = function(name) { 170 var s = location.search.substr(1).split('&') 171 for (var i = 0; i < s.length; i++) 172 if (s[i].toLowerCase().indexOf(name.toLowerCase() + '=') === 0) 173 return s[i].substr(name.length + 1) 174 } 175 176 // Track click events. 177 window.goatcounter.bind_events = function() { 178 if (!document.querySelectorAll) // Just in case someone uses an ancient browser. 179 return 180 181 var send = function(elem) { 182 return function() { 183 goatcounter.count({ 184 event: true, 185 path: (elem.dataset.goatcounterClick || elem.name || elem.id || ''), 186 title: (elem.dataset.goatcounterTitle || elem.title || (elem.innerHTML || '').substr(0, 200) || ''), 187 referrer: (elem.dataset.goatcounterReferrer || elem.dataset.goatcounterReferral || ''), 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: append 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.