-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
27 lines (19 loc) · 980 Bytes
/
index.js
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
var assert = require('assert')
var MAX_SIZE = 65536
module.exports = nanobeacon
function nanobeacon (url, data) {
data = data || {}
assert.equal(typeof url, 'string', 'nanobeacon: url should be type string')
assert.equal(typeof data, 'object', 'nanobeacon: data should be type object')
assert.equal(typeof window, 'object', 'nanobeacon: no window object found')
assert.ok(window.navigator && window.navigator.sendBeacon, 'nanobeacon: window.navigator.beacon not found. Consider using a polyfill')
try {
var json = JSON.stringify(data)
} catch (e) {
throw new Error('nanobeacon: could not stringify data')
}
assert.ok(json.length < MAX_SIZE, 'nanobeacon: data should be smaller than ' + MAX_SIZE + ' bytes. Was ' + json.length + ' bytes')
var blob = new window.Blob([ json ], { type: 'text/plain; charset=UTF-8' })
if (window.navigator.doNotTrack || !window.navigator.sendBeacon) return false
return window.navigator.sendBeacon(url, blob)
}