diff --git a/articles/static/vendor/goatcounter.js b/articles/static/vendor/goatcounter.js index 4dc7659..4f4b70b 100644 --- a/articles/static/vendor/goatcounter.js +++ b/articles/static/vendor/goatcounter.js @@ -1,14 +1,24 @@ // GoatCounter: https://www.goatcounter.com // This file (and *only* this file) is released under the ISC license: // https://opensource.org/licenses/ISC -(function() { +;(function() { 'use strict'; - if (window.goatcounter && window.goatcounter.vars) // Compatibility + if (window.goatcounter && window.goatcounter.vars) // Compatibility with very old version; do not use. window.goatcounter = window.goatcounter.vars else window.goatcounter = window.goatcounter || {} + // Load settings from data-goatcounter-settings. + var s = document.querySelector('script[data-goatcounter]') + if (s && s.dataset.goatcounterSettings) { + try { var set = JSON.parse(s.dataset.goatcounterSettings) } + catch (err) { console.error('invalid JSON in data-goatcounter-settings: ' + err) } + for (var k in set) + if (['no_onload', 'no_events', 'allow_local', 'allow_frame', 'path', 'title', 'referrer', 'event'].indexOf(k) > -1) + window.goatcounter[k] = set[k] + } + // Get all data we're going to send off to the counter endpoint. var get_data = function(vars) { var data = { @@ -72,7 +82,7 @@ // Get the endpoint to send requests to. var get_endpoint = function() { - var s = document.querySelector('script[data-goatcounter]'); + var s = document.querySelector('script[data-goatcounter]') if (s && s.dataset.goatcounter) return s.dataset.goatcounter return (goatcounter.endpoint || window.counter) // counter is for compat; don't use. @@ -91,6 +101,14 @@ return (loc.pathname + loc.search) || '/' } + // Run function after DOM is loaded. + var on_load = function(f) { + if (document.body === null) + document.addEventListener('DOMContentLoaded', function() { f() }, false) + else + f() + } + // Filter some requests that we (probably) don't want to count. goatcounter.filter = function() { if ('visibilityState' in document && (document.visibilityState === 'prerender' || document.visibilityState === 'hidden')) @@ -137,7 +155,7 @@ img.setAttribute('aria-hidden', 'true') var rm = function() { if (img && img.parentNode) img.parentNode.removeChild(img) } - setTimeout(rm, 3000) // In case the onload isn't triggered. + setTimeout(rm, 10000) // In case the onload isn't triggered. img.addEventListener('load', rm, false) document.body.appendChild(img) } @@ -178,37 +196,39 @@ // Add a "visitor counter" frame or image. window.goatcounter.visit_count = function(opt) { - opt = opt || {} - opt.type = opt.type || 'html' - opt.append = opt.append || 'body' - opt.path = opt.path || get_path() - opt.attr = opt.attr || {width: '200', height: (opt.no_branding ? '60' : '80')} + on_load(function() { + opt = opt || {} + opt.type = opt.type || 'html' + opt.append = opt.append || 'body' + opt.path = opt.path || get_path() + opt.attr = opt.attr || {width: '200', height: (opt.no_branding ? '60' : '80')} - opt.attr['src'] = get_endpoint() + 'er/' + encodeURIComponent(opt.path) + '.' + opt.type + '?' - if (opt.no_branding) opt.attr['src'] += '&no_branding=1' - if (opt.style) opt.attr['src'] += '&style=' + encodeURIComponent(opt.style) + opt.attr['src'] = get_endpoint() + 'er/' + encodeURIComponent(opt.path) + '.' + opt.type + '?' + if (opt.no_branding) opt.attr['src'] += '&no_branding=1' + if (opt.style) opt.attr['src'] += '&style=' + encodeURIComponent(opt.style) - var tag = {png: 'img', svg: 'img', html: 'iframe'}[opt.type] - if (!tag) - return warn('visit_count: unknown type: ' + opt.type) + var tag = {png: 'img', svg: 'img', html: 'iframe'}[opt.type] + if (!tag) + return warn('visit_count: unknown type: ' + opt.type) - if (opt.type === 'html') { - opt.attr['frameborder'] = '0' - opt.attr['scrolling'] = 'no' - } + if (opt.type === 'html') { + opt.attr['frameborder'] = '0' + opt.attr['scrolling'] = 'no' + } - var d = document.createElement(tag) - for (var k in opt.attr) - d.setAttribute(k, opt.attr[k]) + var d = document.createElement(tag) + for (var k in opt.attr) + d.setAttribute(k, opt.attr[k]) - var p = document.querySelector(opt.append) - if (!p) - return warn('visit_count: append not found: ' + opt.append) - p.appendChild(d) + var p = document.querySelector(opt.append) + if (!p) + return warn('visit_count: append not found: ' + opt.append) + p.appendChild(d) + }) } // Make it easy to skip your own views. - if (location.hash === '#toggle-goatcounter') + if (location.hash === '#toggle-goatcounter') { if (localStorage.getItem('skipgc') === 't') { localStorage.removeItem('skipgc', 't') alert('GoatCounter tracking is now ENABLED in this browser.') @@ -217,17 +237,12 @@ localStorage.setItem('skipgc', 't') alert('GoatCounter tracking is now DISABLED in this browser until ' + location + ' is loaded again.') } + } - if (!goatcounter.no_onload) { - var go = function() { + if (!goatcounter.no_onload) + on_load(function() { goatcounter.count() if (!goatcounter.no_events) goatcounter.bind_events() - } - - if (document.body === null) - document.addEventListener('DOMContentLoaded', function() { go() }, false) - else - go() - } + }) })();