aboutsummaryrefslogtreecommitdiff
path: root/counter/sws.js
blob: 5011b9a239d742514da3b4629bedf06cf94a78ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var d = document
var de = d.documentElement
var w = window
var l = d.location
var n = w.navigator
var esc = encodeURIComponent
var me = d.currentScript

var _sws = w._sws || {noauto: false, local: false}
_sws.d = _sws.d || me.src
_sws.id = _sws.id || me.dataset.id

function count (p, obj) {
  if (!_sws.local && l.hostname.match(/(localhost$|^127\.|^10\.|^172\.16\.|^192\.168\.)/))
    return
  if ('visibilityState' in d && d.visibilityState === 'prerender')
    return

  var qs = Object.keys(obj)
    .map(function (k) {
      return esc(k) + '=' + esc(obj[k])
    })
    .join('&')

  if (!_sws.noxhr) {
    var r = new w.XMLHttpRequest()
    r.open('GET', p + '?' + qs, true)
    r.send()
  } else {
    var image = new Image(1, 1)
    image.src = p + '?' + qs
  }
}

function ready (fn) {
  if (d.attachEvent ? d.readyState === 'complete' : d.readyState !== 'loading') {
    fn()
  } else {
    d.addEventListener('DOMContentLoaded', fn)
  }
}

var viewPort = (w.innerWidth || de.clientWidth || d.body.clientWidth) + 'x' +
  (w.innerHeight || de.clientHeight || d.body.clientHeight)

ready(function () {
  if (!_sws.noauto) {
    var ep = new URL(_sws.d)
    count('{{ .Endpoint }}', {
      id: _sws.id,
      s: l.protocol,
      h: l.host,
      p: l.pathname,
      q: l.search + l.hash,
      t: _sws.title || d.title,
      r: d.referrer,
      u: n.userAgent,
      v: viewPort
    })
  }
})