Skip to content

Commit 533d161

Browse files
committed
Inline code for visit counter.
1 parent 2513c98 commit 533d161

File tree

1 file changed

+271
-2
lines changed

1 file changed

+271
-2
lines changed

assets/traffic-counter.html

Lines changed: 271 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,271 @@
1-
<script data-goatcounter="https://nvim-mini.goatcounter.com/count"
2-
async src="//gc.zgo.at/count.js"></script>
1+
<!-- Original source: https://gc.zgo.at/count.js -->
2+
<!-- Inlined for security reasons (less supply chain attack possibility) -->
3+
<script data-goatcounter="https://nvim-mini.goatcounter.com/count">
4+
// GoatCounter: https://www.goatcounter.com
5+
// This file is released under the ISC license: https://opensource.org/licenses/ISC
6+
;(function() {
7+
'use strict';
8+
9+
window.goatcounter = window.goatcounter || {}
10+
11+
// Load settings from data-goatcounter-settings.
12+
var s = document.querySelector('script[data-goatcounter]')
13+
if (s && s.dataset.goatcounterSettings) {
14+
try { var set = JSON.parse(s.dataset.goatcounterSettings) }
15+
catch (err) { console.error('invalid JSON in data-goatcounter-settings: ' + err) }
16+
for (var k in set)
17+
if (['no_onload', 'no_events', 'allow_local', 'allow_frame', 'path', 'title', 'referrer', 'event'].indexOf(k) > -1)
18+
window.goatcounter[k] = set[k]
19+
}
20+
21+
var enc = encodeURIComponent
22+
23+
// Get all data we're going to send off to the counter endpoint.
24+
window.goatcounter.get_data = function(vars) {
25+
vars = 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,
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+
return (s && s.dataset.goatcounter) ? s.dataset.goatcounter : goatcounter.endpoint
89+
}
90+
91+
// Get current path.
92+
var get_path = function() {
93+
var loc = location,
94+
c = document.querySelector('link[rel="canonical"][href]')
95+
if (c) { // May be relative or point to different domain.
96+
var a = document.createElement('a')
97+
a.href = c.href
98+
if (a.hostname.replace(/^www\./, '') === location.hostname.replace(/^www\./, ''))
99+
loc = a
100+
}
101+
return (loc.pathname + loc.search) || '/'
102+
}
103+
104+
// Run function after DOM is loaded.
105+
var on_load = function(f) {
106+
if (document.body === null)
107+
document.addEventListener('DOMContentLoaded', function() { f() }, false)
108+
else
109+
f()
110+
}
111+
112+
// Filter some requests that we (probably) don't want to count.
113+
window.goatcounter.filter = function() {
114+
if ('visibilityState' in document && document.visibilityState === 'prerender')
115+
return 'visibilityState'
116+
if (!goatcounter.allow_frame && location !== parent.location)
117+
return 'frame'
118+
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$)/))
119+
return 'localhost'
120+
if (!goatcounter.allow_local && location.protocol === 'file:')
121+
return 'localfile'
122+
if (localStorage && localStorage.getItem('skipgc') === 't')
123+
return 'disabled with #toggle-goatcounter'
124+
return false
125+
}
126+
127+
// Get URL to send to GoatCounter.
128+
window.goatcounter.url = function(vars) {
129+
var data = window.goatcounter.get_data(vars || {})
130+
if (data.p === null) // null from user callback.
131+
return
132+
data.rnd = Math.random().toString(36).substr(2, 5) // Browsers don't always listen to Cache-Control.
133+
134+
var endpoint = get_endpoint()
135+
if (!endpoint)
136+
return warn('no endpoint found')
137+
138+
return endpoint + urlencode(data)
139+
}
140+
141+
// Count a hit.
142+
window.goatcounter.count = function(vars) {
143+
var f = goatcounter.filter()
144+
if (f)
145+
return warn('not counting because of: ' + f)
146+
var url = goatcounter.url(vars)
147+
if (!url)
148+
return warn('not counting because path callback returned null')
149+
150+
if (!navigator.sendBeacon(url)) {
151+
// This mostly fails due to being blocked by CSP; try again with an
152+
// image-based fallback.
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+
169+
// Get a query parameter.
170+
window.goatcounter.get_query = function(name) {
171+
var s = location.search.substr(1).split('&')
172+
for (var i = 0; i < s.length; i++)
173+
if (s[i].toLowerCase().indexOf(name.toLowerCase() + '=') === 0)
174+
return s[i].substr(name.length + 1)
175+
}
176+
177+
// Track click events.
178+
window.goatcounter.bind_events = function() {
179+
if (!document.querySelectorAll) // Just in case someone uses an ancient browser.
180+
return
181+
182+
var send = function(elem) {
183+
return function() {
184+
goatcounter.count({
185+
event: true,
186+
path: (elem.dataset.goatcounterClick || elem.name || elem.id || ''),
187+
title: (elem.dataset.goatcounterTitle || elem.title || (elem.innerHTML || '').substr(0, 200) || ''),
188+
referrer: (elem.dataset.goatcounterReferrer || elem.dataset.goatcounterReferral || ''),
189+
})
190+
}
191+
}
192+
193+
Array.prototype.slice.call(document.querySelectorAll("*[data-goatcounter-click]")).forEach(function(elem) {
194+
if (elem.dataset.goatcounterBound)
195+
return
196+
var f = send(elem)
197+
elem.addEventListener('click', f, false)
198+
elem.addEventListener('auxclick', f, false) // Middle click.
199+
elem.dataset.goatcounterBound = 'true'
200+
})
201+
}
202+
203+
// Add a "visitor counter" frame or image.
204+
window.goatcounter.visit_count = function(opt) {
205+
on_load(function() {
206+
opt = opt || {}
207+
opt.type = opt.type || 'html'
208+
opt.append = opt.append || 'body'
209+
opt.path = opt.path || get_path()
210+
opt.attr = opt.attr || {width: '200', height: (opt.no_branding ? '60' : '80')}
211+
212+
opt.attr['src'] = get_endpoint() + 'er/' + enc(opt.path) + '.' + enc(opt.type) + '?'
213+
if (opt.no_branding) opt.attr['src'] += '&no_branding=1'
214+
if (opt.style) opt.attr['src'] += '&style=' + enc(opt.style)
215+
if (opt.start) opt.attr['src'] += '&start=' + enc(opt.start)
216+
if (opt.end) opt.attr['src'] += '&end=' + enc(opt.end)
217+
218+
var tag = {png: 'img', svg: 'img', html: 'iframe'}[opt.type]
219+
if (!tag)
220+
return warn('visit_count: unknown type: ' + opt.type)
221+
222+
if (opt.type === 'html') {
223+
opt.attr['frameborder'] = '0'
224+
opt.attr['scrolling'] = 'no'
225+
}
226+
227+
var d = document.createElement(tag)
228+
for (var k in opt.attr)
229+
d.setAttribute(k, opt.attr[k])
230+
231+
var p = document.querySelector(opt.append)
232+
if (!p)
233+
return warn('visit_count: element to append to not found: ' + opt.append)
234+
p.appendChild(d)
235+
})
236+
}
237+
238+
// Make it easy to skip your own views.
239+
if (location.hash === '#toggle-goatcounter') {
240+
if (localStorage.getItem('skipgc') === 't') {
241+
localStorage.removeItem('skipgc', 't')
242+
alert('GoatCounter tracking is now ENABLED in this browser.')
243+
}
244+
else {
245+
localStorage.setItem('skipgc', 't')
246+
alert('GoatCounter tracking is now DISABLED in this browser until ' + location + ' is loaded again.')
247+
}
248+
}
249+
250+
if (!goatcounter.no_onload)
251+
on_load(function() {
252+
// 1. Page is visible, count request.
253+
// 2. Page is not yet visible; wait until it switches to 'visible' and count.
254+
// See #487
255+
if (!('visibilityState' in document) || document.visibilityState === 'visible')
256+
goatcounter.count()
257+
else {
258+
var f = function(e) {
259+
if (document.visibilityState !== 'visible')
260+
return
261+
document.removeEventListener('visibilitychange', f)
262+
goatcounter.count()
263+
}
264+
document.addEventListener('visibilitychange', f)
265+
}
266+
267+
if (!goatcounter.no_events)
268+
goatcounter.bind_events()
269+
})
270+
})();
271+
</script>

0 commit comments

Comments
 (0)