-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensync.js
71 lines (62 loc) · 2.2 KB
/
opensync.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
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
62
63
64
65
66
67
68
69
70
71
;(() => {
const glob = {};
let options = {};
const pubOpts = window.opensync;
const UID_COOKIE_NAME = 'openId';
options.startDelay = pubOpts.startDelay !== undefined ? pubOpts.startDelay : 1000;
options.syncLimit = pubOpts.syncLimit !== undefined ? pubOpts.syncLimit : 3;
const syncDefer = makeDefer();
window.addEventListener('message', (event) => {
let data = event.data ? event.data : {};
if (data.message === 'open-sync-update') {
if (data.userId) {
setCookie(UID_COOKIE_NAME, data.userId);
syncDefer.resolve(data.userId);
}
}
})
function setCookie(name, value) {
document.cookie = `${name}=${value}; expires=Sun, 1 Jan 2023 00:00:00 UTC;`;
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function makeDefer() {
let result = {}
result.promise = new Promise((resolve) => {
result.resolve = resolve;
})
return result;
}
function createSyncFrame(opts) {
const iframe = document.createElement('iframe');
iframe.src = opts.url
.replace('{uid}', opts.uid || '')
.replace('{limit}', opts.limit);
const style = iframe.style;
style.width = '0px';
style.height = '0px';
style.border = 'none';
document.body.appendChild(iframe);
}
function start() {
const uid = getCookie(UID_COOKIE_NAME);
createSyncFrame({ url: 'https://adtelligent.github.io/public-fp-sync/opensync.frame.html?uid={uid}&limit={limit}', uid, limit: options.syncLimit })
}
glob.getOpenId = () => {
if (getCookie(UID_COOKIE_NAME)) {
return Promise.resolve(getCookie(UID_COOKIE_NAME));
} else {
return syncDefer.promise;
}
}
window.opensync = glob;
setTimeout(start, options.startDelay);
})();