@blotoutio/edgetag-sdk-browser 0.2.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +1 -0
- package/index.js +1 -408
- package/package.json +16 -17
- package/index.min.js +0 -1
- package/index.min.js.gz +0 -0
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(e,t,n,o){return new(n||(n=Promise))((function(s,r){function a(e){try{i(o.next(e))}catch(e){r(e)}}function c(e){try{i(o.throw(e))}catch(e){r(e)}}function i(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,c)}i((o=o.apply(e,t||[])).next())}))}const t=(e,t,n,o)=>{const a=r(e);a.data||(a.data={}),a.data[t]||(a.data[t]={}),a.data[t][o]=n,s(e,a)},n=(e,t,n,o)=>{const a=r(e);a[t]||(a[t]={}),a[t][o]=n,s(e,a)},o=(e,t,n)=>{const o=r(e);if(o[t])return o[t][n]},s=(e,t,n="_workerStore")=>{"session"!==e?c(t,n):l(t,n)},r=(e,t="_workerStore")=>"session"===e?d(t):i(t),a=e=>{let t=r("session");t||(t={}),t.data||(t.data={}),t.data=Object.assign(Object.assign({},t.data),e),s("session",t)},c=(e,t)=>{localStorage.setItem(t,JSON.stringify(e))},i=e=>{const t=localStorage.getItem(e);return t&&JSON.parse(t)||{}},l=(e,t)=>{sessionStorage.setItem(t,JSON.stringify(e))},d=e=>{const t=sessionStorage.getItem(e);return t&&JSON.parse(t)||{}};let u="";const g=e=>{const t=f();return t?`${t}${e}`:(console.log("URL is not valid"),"")},f=()=>u;let h=!1;const y=e=>{return!!e&&(e.edgeURL?(h=!!e.disableConsentCheck,null!=(t=e.edgeURL)&&(u=t),!0):(console.error("Please provide URL for EdgeTag"),!1));var t},v=()=>{const e=navigator;let t=e.userAgent;return t+=e.brave?`${t} Brave`:"",t},p=e=>{const t=o("local","edgeTag","consent");if(h)return!0;if(!t)return!1;if(t.all)return!0;if(!e)return Object.values(t).find((e=>e))||!1;const n=o("local","edgeTag","providers")||[];for(const o of n){const n=e[o];if((n||!0===e.all&&void 0===n||!1===e.all&&!0===n)&&t[o])return!0}return!1},b=(e,t)=>{let n;return t&&(n=new Blob([JSON.stringify(t)],{type:"application/json"})),navigator.sendBeacon(e,n)},m=(t,n,o)=>e(void 0,void 0,void 0,(function*(){return yield fetch(n,{method:t,headers:{"Content-type":"application/json; charset=utf-8",Accept:"application/json; charset=utf-8"},body:JSON.stringify(o),credentials:"include"}).then((e=>e.json().then((t=>({status:e.status,body:t}))))).then((e=>e.status<200||e.status>=300?Promise.reject(new Error(JSON.stringify(e.body))):Promise.resolve(e.body))).catch((e=>Promise.reject(new Error(e))))}));function w(t,n,o){return e(this,void 0,void 0,(function*(){if(!t)return Promise.reject(new Error("URL is empty"));const e=(e=>{const t=Object.assign({pageUrl:window.location.href,userAgent:v()},e||{});let n={};const o=r("session");o&&(n=Object.assign(Object.assign({},n),o));const s=r("local");return s&&(n=Object.assign(Object.assign({},n),s)),t.storage=n,t})(n);return o&&"beacon"===o.method?Promise.resolve(b(t,e)):yield m("POST",t,e)}))}function j(t,n){return e(this,void 0,void 0,(function*(){return t?n&&"beacon"===n.method?{result:Promise.resolve(b(t))}:yield m("GET",t):Promise.reject(new Error("URL is empty"))}))}const k=e=>{},O=e=>{n("local","edgeTag",e,"consent")},S=e=>{const t={consentString:e};O(e),w(g("/consent"),t).catch(k)},P=(e,t,n)=>{const o={eventName:e};t&&(o.data=t),n&&(o.providers=n),p(n)?w(g("/tag"),o).catch(k):console.log("No consent")},I=(e,n,o,s)=>{let r;switch(s){case"cookie":r=(e=>{const t=`${e}=`,n=decodeURIComponent(document.cookie).split(";");for(let e=0;e<n.length;e++){let o=n[e];for(;" "===o.charAt(0);)o=o.substring(1);if(0===o.indexOf(t))return o.substring(t.length,o.length)}return""})(n);break;case"local":r=sessionStorage.getItem(n);break;case"session":r=localStorage.getItem(n)}r&&t(o,e,r,n)},E=(e,n)=>{n.forEach((n=>{switch(n.type){case"query":((e,n,o)=>{const s=new URLSearchParams(window.location.search);s&&s.get(n)&&t(o,e,s.get(n),n)})(e,n.key,n.persist);break;case"storage":I(e,n.key,n.persist,n.location)}}))},U=e=>{if(!y(e))return;let t=`${g("/init")}`;e.disableConsentCheck&&(t=`${t}?consentDisabled=true`,O({all:!0})),j(t).then((e=>{e?(e=>{const t=[];e.forEach((e=>{t.push(e.package),Object.entries(e.rules).forEach((([t,n])=>{"capture"!==t||E(e.package,n)}))})),n("local","edgeTag",t,"providers")})(e.result):console.log("init failed")})).catch(k)},A=(e,t)=>{e&&t?(a({[e]:t}),w(g("/user"),{key:e,value:t}).catch(k)):console.error("Key or Value is missing in user API.")},N=e=>{e&&0!==Object.keys(e).length?(a(e),w(g("/data"),{data:e}).catch(k)):console.error("Provide data for data API.")},R=(e,t)=>{e&&0!==e.length?j((e=>g(`/data?keys=${encodeURIComponent(e.join(","))}`))(e)).then((e=>{t((null==e?void 0:e.result)||{})})).catch(k):console.error("Provide keys for get data API.")},C=e=>{j(g("/keys")).then((t=>{e((null==t?void 0:t.result)||[])})).catch(k)};var J=new class{init(e){(e=>{U(e)})(e)}tag(e,t,n){((e,t,n)=>{P(e,t,n)})(e,t,n)}consent(e){(e=>{S(e)})(e)}user(e,t){((e,t)=>{A(e,t)})(e,t)}data(e){(e=>{N(e)})(e)}getData(e,t){((e,t)=>{R(e,t)})(e,t)}keys(e){(e=>{C(e)})(e)}};!function(){const e=e=>{const n=[].slice.call(e);if(Array.isArray(n))try{return t[n[0]](...n.slice(1))}catch(e){console.error(e)}},t=J;let n=[];window.edgetag&&(n=window.edgetag.stubs||[]),window.edgetag=function(){return e(arguments)},n.forEach(e)}();
|
package/index.js
CHANGED
|
@@ -1,408 +1 @@
|
|
|
1
|
-
(function (
|
|
2
|
-
typeof define === 'function' && define.amd ? define(factory) :
|
|
3
|
-
factory();
|
|
4
|
-
})((function () { 'use strict';
|
|
5
|
-
|
|
6
|
-
const tagStorage = 'edgeTag';
|
|
7
|
-
const consentKey = 'consent';
|
|
8
|
-
const providersKey = 'providers';
|
|
9
|
-
const keyPrefix = `_worker`;
|
|
10
|
-
|
|
11
|
-
const initKey = `${keyPrefix}Store`;
|
|
12
|
-
const saveDataPerKey = (persistType, provider, value, key) => {
|
|
13
|
-
const storage = getData(persistType);
|
|
14
|
-
if (!storage.data) {
|
|
15
|
-
storage.data = {};
|
|
16
|
-
}
|
|
17
|
-
if (!storage.data[provider]) {
|
|
18
|
-
storage.data[provider] = {};
|
|
19
|
-
}
|
|
20
|
-
// migration
|
|
21
|
-
if (storage[provider]) {
|
|
22
|
-
storage.data[provider] = storage[provider];
|
|
23
|
-
}
|
|
24
|
-
storage.data[provider][key] = value;
|
|
25
|
-
saveData(persistType, storage);
|
|
26
|
-
};
|
|
27
|
-
const savePerKey = (persistType, provider, value, key) => {
|
|
28
|
-
const storage = getData(persistType);
|
|
29
|
-
if (!storage[provider]) {
|
|
30
|
-
storage[provider] = {};
|
|
31
|
-
}
|
|
32
|
-
storage[provider][key] = value;
|
|
33
|
-
saveData(persistType, storage);
|
|
34
|
-
};
|
|
35
|
-
const getDataPerKey = (persistType, provider, key) => {
|
|
36
|
-
const storage = getData(persistType);
|
|
37
|
-
if (!storage[provider]) {
|
|
38
|
-
return undefined;
|
|
39
|
-
}
|
|
40
|
-
return storage[provider][key];
|
|
41
|
-
};
|
|
42
|
-
const saveData = (persistType, value, key = initKey) => {
|
|
43
|
-
if (persistType === 'session') {
|
|
44
|
-
saveSession(value, key);
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
saveLocal(value, key);
|
|
48
|
-
};
|
|
49
|
-
const getData = (persistType, key = initKey) => {
|
|
50
|
-
if (persistType === 'session') {
|
|
51
|
-
return getSession(key);
|
|
52
|
-
}
|
|
53
|
-
return getLocal(key);
|
|
54
|
-
};
|
|
55
|
-
const saveLocal = (value, key) => {
|
|
56
|
-
localStorage.setItem(key, JSON.stringify(value));
|
|
57
|
-
};
|
|
58
|
-
const getLocal = (key) => {
|
|
59
|
-
return JSON.parse(localStorage.getItem(key)) || {};
|
|
60
|
-
};
|
|
61
|
-
const saveSession = (value, key) => {
|
|
62
|
-
sessionStorage.setItem(key, JSON.stringify(value));
|
|
63
|
-
};
|
|
64
|
-
const getSession = (key) => {
|
|
65
|
-
return JSON.parse(sessionStorage.getItem(key)) || {};
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
let endpointUrl = '';
|
|
69
|
-
const generateUrl = (path) => {
|
|
70
|
-
const endpoint = getUrl();
|
|
71
|
-
if (!endpoint) {
|
|
72
|
-
console.log('URL is not valid');
|
|
73
|
-
return '';
|
|
74
|
-
}
|
|
75
|
-
return `${endpoint}${path}`;
|
|
76
|
-
};
|
|
77
|
-
const getUrl = () => {
|
|
78
|
-
return endpointUrl;
|
|
79
|
-
};
|
|
80
|
-
const setUrl = (url) => {
|
|
81
|
-
if (url == null) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
endpointUrl = url;
|
|
85
|
-
};
|
|
86
|
-
const getTagURL = () => {
|
|
87
|
-
return generateUrl('/tag');
|
|
88
|
-
};
|
|
89
|
-
const getInitURL = () => {
|
|
90
|
-
return generateUrl('/init');
|
|
91
|
-
};
|
|
92
|
-
const getConsentURL = () => {
|
|
93
|
-
return generateUrl('/consent');
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
let consentDisabled = false;
|
|
97
|
-
const setPreferences = (preferences) => {
|
|
98
|
-
if (!preferences) {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
if (!preferences.edgeURL) {
|
|
102
|
-
console.error('Please provide URL for EdgeTag');
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
consentDisabled = !!preferences.disableConsentCheck;
|
|
106
|
-
setUrl(preferences.edgeURL);
|
|
107
|
-
return true;
|
|
108
|
-
};
|
|
109
|
-
const isConsentDisabled = () => consentDisabled;
|
|
110
|
-
|
|
111
|
-
const getUserAgent = () => {
|
|
112
|
-
const nav = navigator;
|
|
113
|
-
let ua = nav.userAgent;
|
|
114
|
-
ua += nav.brave ? `${ua} Brave` : '';
|
|
115
|
-
return ua;
|
|
116
|
-
};
|
|
117
|
-
const allowTag = (providers) => {
|
|
118
|
-
const consent = getDataPerKey('local', tagStorage, consentKey);
|
|
119
|
-
if (isConsentDisabled()) {
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
if (!consent) {
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
if (consent.all === true) {
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
if (!providers) {
|
|
129
|
-
return Object.values(consent).find((isAllowed) => isAllowed);
|
|
130
|
-
}
|
|
131
|
-
const allProviders = getDataPerKey('local', tagStorage, providersKey) || [];
|
|
132
|
-
for (const provider of allProviders) {
|
|
133
|
-
const tagProviderSetting = providers[provider];
|
|
134
|
-
if (tagProviderSetting ||
|
|
135
|
-
(providers.all === true && tagProviderSetting == undefined) ||
|
|
136
|
-
(providers.all === false && tagProviderSetting === true)) {
|
|
137
|
-
if (consent[provider]) {
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return false;
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
const beacon = (url, payload) => {
|
|
146
|
-
let blob;
|
|
147
|
-
if (payload) {
|
|
148
|
-
blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
|
|
149
|
-
}
|
|
150
|
-
return navigator.sendBeacon(url, blob);
|
|
151
|
-
};
|
|
152
|
-
const ajax = async (method, url, payload) => {
|
|
153
|
-
return await fetch(url, {
|
|
154
|
-
method,
|
|
155
|
-
headers: {
|
|
156
|
-
'Content-type': 'application/json; charset=utf-8',
|
|
157
|
-
Accept: 'application/json; charset=utf-8',
|
|
158
|
-
},
|
|
159
|
-
body: JSON.stringify(payload),
|
|
160
|
-
credentials: 'include',
|
|
161
|
-
})
|
|
162
|
-
.then((response) => response.json().then((data) => ({ status: response.status, body: data })))
|
|
163
|
-
.then((data) => {
|
|
164
|
-
if (data.status < 200 || data.status >= 300) {
|
|
165
|
-
// Q: do we need to retry?
|
|
166
|
-
return Promise.reject(new Error(JSON.stringify(data.body)));
|
|
167
|
-
}
|
|
168
|
-
return Promise.resolve(data.body);
|
|
169
|
-
})
|
|
170
|
-
.catch((error) => {
|
|
171
|
-
// Q: do we need to retry?
|
|
172
|
-
return Promise.reject(new Error(error));
|
|
173
|
-
});
|
|
174
|
-
};
|
|
175
|
-
const getStandardPayload = (payload) => {
|
|
176
|
-
const data = {
|
|
177
|
-
pageUrl: window.location.href,
|
|
178
|
-
userAgent: getUserAgent(),
|
|
179
|
-
...payload,
|
|
180
|
-
};
|
|
181
|
-
let storage = {};
|
|
182
|
-
const session = getData('session');
|
|
183
|
-
if (session) {
|
|
184
|
-
storage = {
|
|
185
|
-
...storage,
|
|
186
|
-
...session,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
const local = getData('local');
|
|
190
|
-
if (local) {
|
|
191
|
-
storage = {
|
|
192
|
-
...storage,
|
|
193
|
-
...local,
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
data.storage = storage;
|
|
197
|
-
return data;
|
|
198
|
-
};
|
|
199
|
-
async function postRequest(url, data, options) {
|
|
200
|
-
if (!url) {
|
|
201
|
-
return Promise.reject(new Error('URL is empty'));
|
|
202
|
-
}
|
|
203
|
-
const payload = getStandardPayload(data);
|
|
204
|
-
if (options && options.method === 'beacon' && navigator.sendBeacon) {
|
|
205
|
-
return Promise.resolve(beacon(url, payload));
|
|
206
|
-
}
|
|
207
|
-
return await ajax('POST', url, payload);
|
|
208
|
-
}
|
|
209
|
-
async function getRequest(url, options) {
|
|
210
|
-
if (!url) {
|
|
211
|
-
return Promise.reject(new Error('URL is empty'));
|
|
212
|
-
}
|
|
213
|
-
if (options && options.method === 'beacon' && navigator.sendBeacon) {
|
|
214
|
-
return Promise.resolve(beacon(url));
|
|
215
|
-
}
|
|
216
|
-
return await ajax('GET', url);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const info = (data) => {
|
|
220
|
-
{
|
|
221
|
-
console.info(data);
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
const saveConsent = (consent) => {
|
|
226
|
-
savePerKey('local', tagStorage, consent, consentKey);
|
|
227
|
-
};
|
|
228
|
-
const handleConsent = (consent) => {
|
|
229
|
-
const payload = {
|
|
230
|
-
consentString: consent,
|
|
231
|
-
};
|
|
232
|
-
saveConsent(consent);
|
|
233
|
-
postRequest(getConsentURL(), payload).catch(info);
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
const handleTag = (name, data, providers) => {
|
|
237
|
-
let payload = {
|
|
238
|
-
eventName: name,
|
|
239
|
-
};
|
|
240
|
-
if (data) {
|
|
241
|
-
payload.data = data;
|
|
242
|
-
}
|
|
243
|
-
if (providers) {
|
|
244
|
-
payload.providers = providers;
|
|
245
|
-
}
|
|
246
|
-
if (!allowTag(providers)) {
|
|
247
|
-
console.log('No consent');
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
postRequest(getTagURL(), payload).catch(info);
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
const getCookieValue = (key) => {
|
|
254
|
-
let name = `${key}=`;
|
|
255
|
-
let decodedCookie = decodeURIComponent(document.cookie);
|
|
256
|
-
let ca = decodedCookie.split(';');
|
|
257
|
-
for (let i = 0; i < ca.length; i++) {
|
|
258
|
-
let c = ca[i];
|
|
259
|
-
while (c.charAt(0) == ' ') {
|
|
260
|
-
c = c.substring(1);
|
|
261
|
-
}
|
|
262
|
-
if (c.indexOf(name) == 0) {
|
|
263
|
-
return c.substring(name.length, c.length);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return '';
|
|
267
|
-
};
|
|
268
|
-
const handleCaptureQuery = (provider, key, persistType) => {
|
|
269
|
-
const params = new URLSearchParams(window.location.search);
|
|
270
|
-
if (!params || !params.get(key)) {
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
saveDataPerKey(persistType, provider, params.get(key), key);
|
|
274
|
-
};
|
|
275
|
-
const handleCaptureStorage = (provider, location, key, persistType) => {
|
|
276
|
-
let data;
|
|
277
|
-
switch (location) {
|
|
278
|
-
case 'cookie': {
|
|
279
|
-
data = getCookieValue(key);
|
|
280
|
-
break;
|
|
281
|
-
}
|
|
282
|
-
case 'local': {
|
|
283
|
-
data = sessionStorage.getItem(key);
|
|
284
|
-
break;
|
|
285
|
-
}
|
|
286
|
-
case 'session': {
|
|
287
|
-
data = localStorage.getItem(key);
|
|
288
|
-
break;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
if (!data) {
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
saveDataPerKey(persistType, provider, data, key);
|
|
295
|
-
};
|
|
296
|
-
const handleCapture = (provider, params) => {
|
|
297
|
-
params.forEach((param) => {
|
|
298
|
-
switch (param.type) {
|
|
299
|
-
case 'query': {
|
|
300
|
-
handleCaptureQuery(provider, param.key, param.persist);
|
|
301
|
-
break;
|
|
302
|
-
}
|
|
303
|
-
case 'storage': {
|
|
304
|
-
handleCaptureStorage(provider, param.location, param.key, param.persist);
|
|
305
|
-
break;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
const handleManifest = (manifest) => {
|
|
312
|
-
const providers = [];
|
|
313
|
-
manifest.forEach((provider) => {
|
|
314
|
-
providers.push(provider.package);
|
|
315
|
-
Object.entries(provider.rules).forEach(([name, recipe]) => {
|
|
316
|
-
switch (name) {
|
|
317
|
-
case 'capture': {
|
|
318
|
-
handleCapture(provider.package, recipe);
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
savePerKey('local', tagStorage, providers, providersKey);
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
const handleInit = (preferences) => {
|
|
328
|
-
const success = setPreferences(preferences);
|
|
329
|
-
if (!success) {
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
let url = `${getInitURL()}`;
|
|
333
|
-
if (preferences.disableConsentCheck) {
|
|
334
|
-
url = `${url}?consentDisabled=true`;
|
|
335
|
-
saveConsent({ all: true });
|
|
336
|
-
}
|
|
337
|
-
getRequest(url)
|
|
338
|
-
.then((result) => {
|
|
339
|
-
if (!result) {
|
|
340
|
-
console.log('init failed');
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
|
-
handleManifest(result.result);
|
|
344
|
-
})
|
|
345
|
-
.catch(info);
|
|
346
|
-
};
|
|
347
|
-
|
|
348
|
-
const init = (preferences) => {
|
|
349
|
-
handleInit(preferences);
|
|
350
|
-
};
|
|
351
|
-
const tag = (name, data, providers) => {
|
|
352
|
-
handleTag(name, data, providers);
|
|
353
|
-
};
|
|
354
|
-
const consent = (consent) => {
|
|
355
|
-
handleConsent(consent);
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
class API {
|
|
359
|
-
init(preferences) {
|
|
360
|
-
init(preferences);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
tag(name, data, providers) {
|
|
364
|
-
tag(name, data, providers);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
consent(consentString) {
|
|
368
|
-
consent(consentString);
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
var api = new API();
|
|
373
|
-
|
|
374
|
-
(function () {
|
|
375
|
-
const handleFunction = (arg) => {
|
|
376
|
-
const sliced = [].slice.call(arg);
|
|
377
|
-
if (!Array.isArray(sliced)) {
|
|
378
|
-
return
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
try {
|
|
382
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
383
|
-
// @ts-ignore
|
|
384
|
-
return library[sliced[0]](...sliced.slice(1))
|
|
385
|
-
} catch (e) {
|
|
386
|
-
console.error(e);
|
|
387
|
-
}
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
const library = api;
|
|
391
|
-
let stubs = [];
|
|
392
|
-
if (window.edgetag) {
|
|
393
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
394
|
-
// @ts-ignore
|
|
395
|
-
stubs = window.edgetag.stubs || [];
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
// this needs to be regular function for arguments to work
|
|
399
|
-
window.edgetag = function () {
|
|
400
|
-
// eslint-disable-next-line prefer-rest-params
|
|
401
|
-
return handleFunction(arguments)
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
// restore calls that were triggered before lib was ready
|
|
405
|
-
stubs.forEach(handleFunction);
|
|
406
|
-
})();
|
|
407
|
-
|
|
408
|
-
}));
|
|
1
|
+
function e(e,t,n,o){return new(n||(n=Promise))((function(r,s){function a(e){try{i(o.next(e))}catch(e){s(e)}}function c(e){try{i(o.throw(e))}catch(e){s(e)}}function i(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,c)}i((o=o.apply(e,t||[])).next())}))}const t=(e,t,n,o)=>{const a=s(e);a.data||(a.data={}),a.data[t]||(a.data[t]={}),a.data[t][o]=n,r(e,a)},n=(e,t,n,o)=>{const a=s(e);a[t]||(a[t]={}),a[t][o]=n,r(e,a)},o=(e,t,n)=>{const o=s(e);if(o[t])return o[t][n]},r=(e,t,n="_workerStore")=>{"session"!==e?c(t,n):l(t,n)},s=(e,t="_workerStore")=>"session"===e?d(t):i(t),a=e=>{let t=s("session");t||(t={}),t.data||(t.data={}),t.data=Object.assign(Object.assign({},t.data),e),r("session",t)},c=(e,t)=>{localStorage.setItem(t,JSON.stringify(e))},i=e=>{const t=localStorage.getItem(e);return t&&JSON.parse(t)||{}},l=(e,t)=>{sessionStorage.setItem(t,JSON.stringify(e))},d=e=>{const t=sessionStorage.getItem(e);return t&&JSON.parse(t)||{}};let u="";const g=e=>{const t=f();return t?`${t}${e}`:(console.log("URL is not valid"),"")},f=()=>u;let h=!1;const y=e=>{return!!e&&(e.edgeURL?(h=!!e.disableConsentCheck,null!=(t=e.edgeURL)&&(u=t),!0):(console.error("Please provide URL for EdgeTag"),!1));var t},v=()=>{const e=navigator;let t=e.userAgent;return t+=e.brave?`${t} Brave`:"",t},p=e=>{const t=o("local","edgeTag","consent");if(h)return!0;if(!t)return!1;if(t.all)return!0;if(!e)return Object.values(t).find((e=>e))||!1;const n=o("local","edgeTag","providers")||[];for(const o of n){const n=e[o];if((n||!0===e.all&&void 0===n||!1===e.all&&!0===n)&&t[o])return!0}return!1},b=(e,t)=>{let n;return t&&(n=new Blob([JSON.stringify(t)],{type:"application/json"})),navigator.sendBeacon(e,n)},m=(t,n,o)=>e(void 0,void 0,void 0,(function*(){return yield fetch(n,{method:t,headers:{"Content-type":"application/json; charset=utf-8",Accept:"application/json; charset=utf-8"},body:JSON.stringify(o),credentials:"include"}).then((e=>e.json().then((t=>({status:e.status,body:t}))))).then((e=>e.status<200||e.status>=300?Promise.reject(new Error(JSON.stringify(e.body))):Promise.resolve(e.body))).catch((e=>Promise.reject(new Error(e))))}));function w(t,n,o){return e(this,void 0,void 0,(function*(){if(!t)return Promise.reject(new Error("URL is empty"));const e=(e=>{const t=Object.assign({pageUrl:window.location.href,userAgent:v()},e||{});let n={};const o=s("session");o&&(n=Object.assign(Object.assign({},n),o));const r=s("local");return r&&(n=Object.assign(Object.assign({},n),r)),t.storage=n,t})(n);return o&&"beacon"===o.method?Promise.resolve(b(t,e)):yield m("POST",t,e)}))}function j(t,n){return e(this,void 0,void 0,(function*(){return t?n&&"beacon"===n.method?{result:Promise.resolve(b(t))}:yield m("GET",t):Promise.reject(new Error("URL is empty"))}))}const k=e=>{},O=e=>{n("local","edgeTag",e,"consent")},S=e=>{const t={consentString:e};O(e),w(g("/consent"),t).catch(k)},P=(e,t,n)=>{const o={eventName:e};t&&(o.data=t),n&&(o.providers=n),p(n)?w(g("/tag"),o).catch(k):console.log("No consent")},I=(e,n,o,r)=>{let s;switch(r){case"cookie":s=(e=>{const t=`${e}=`,n=decodeURIComponent(document.cookie).split(";");for(let e=0;e<n.length;e++){let o=n[e];for(;" "===o.charAt(0);)o=o.substring(1);if(0===o.indexOf(t))return o.substring(t.length,o.length)}return""})(n);break;case"local":s=sessionStorage.getItem(n);break;case"session":s=localStorage.getItem(n)}s&&t(o,e,s,n)},E=(e,n)=>{n.forEach((n=>{switch(n.type){case"query":((e,n,o)=>{const r=new URLSearchParams(window.location.search);r&&r.get(n)&&t(o,e,r.get(n),n)})(e,n.key,n.persist);break;case"storage":I(e,n.key,n.persist,n.location)}}))},U=e=>{if(!y(e))return;let t=`${g("/init")}`;e.disableConsentCheck&&(t=`${t}?consentDisabled=true`,O({all:!0})),j(t).then((e=>{e?(e=>{const t=[];e.forEach((e=>{t.push(e.package),Object.entries(e.rules).forEach((([t,n])=>{"capture"!==t||E(e.package,n)}))})),n("local","edgeTag",t,"providers")})(e.result):console.log("init failed")})).catch(k)},A=(e,t)=>{e&&t?(a({[e]:t}),w(g("/user"),{key:e,value:t}).catch(k)):console.error("Key or Value is missing in user API.")},N=e=>{e&&0!==Object.keys(e).length?(a(e),w(g("/data"),{data:e}).catch(k)):console.error("Provide data for data API.")},R=(e,t)=>{e&&0!==e.length?j((e=>g(`/data?keys=${encodeURIComponent(e.join(","))}`))(e)).then((e=>{t((null==e?void 0:e.result)||{})})).catch(k):console.error("Provide keys for get data API.")},C=e=>{j(g("/keys")).then((t=>{e((null==t?void 0:t.result)||[])})).catch(k)};var J=new class{init(e){(e=>{U(e)})(e)}tag(e,t,n){((e,t,n)=>{P(e,t,n)})(e,t,n)}consent(e){(e=>{S(e)})(e)}user(e,t){((e,t)=>{A(e,t)})(e,t)}data(e){(e=>{N(e)})(e)}getData(e,t){((e,t)=>{R(e,t)})(e,t)}keys(e){(e=>{C(e)})(e)}};!function(){const e=e=>{const n=[].slice.call(e);if(Array.isArray(n))try{return t[n[0]](...n.slice(1))}catch(e){console.error(e)}},t=J;let n=[];window.edgetag&&(n=window.edgetag.stubs||[]),window.edgetag=function(){return e(arguments)},n.forEach(e)}();
|
package/package.json
CHANGED
|
@@ -1,33 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blotoutio/edgetag-sdk-browser",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Browser
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Browser SDK for EdgeTag",
|
|
5
5
|
"author": "Blotout",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/blotoutio/edgetag-sdk",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
8
11
|
"repository": {
|
|
9
12
|
"type": "git",
|
|
10
13
|
"url": "git+https://github.com/blotoutio/edgetag-sdk.git"
|
|
11
14
|
},
|
|
12
15
|
"files": [
|
|
13
16
|
"index.js",
|
|
14
|
-
"index.
|
|
15
|
-
"index.min.js.gz",
|
|
17
|
+
"index.cjs",
|
|
16
18
|
"package.json",
|
|
17
19
|
"README.md"
|
|
18
20
|
],
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
"publishConfig": {
|
|
31
|
-
"access": "public"
|
|
21
|
+
"module": "./index.js",
|
|
22
|
+
"main": "./index.cjs",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "./index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./index.d.ts",
|
|
28
|
+
"import": "./index.js",
|
|
29
|
+
"require": "./index.cjs"
|
|
30
|
+
}
|
|
32
31
|
}
|
|
33
32
|
}
|
package/index.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";const e="edgeTag",t="consent",n="providers",o="_workerStore",r=(e,t,n,o)=>{const r=i(e);r.data||(r.data={}),r.data[t]||(r.data[t]={}),r[t]&&(r.data[t]=r[t]),r.data[t][o]=n,c(e,r)},s=(e,t,n,o)=>{const r=i(e);r[t]||(r[t]={}),r[t][o]=n,c(e,r)},a=(e,t,n)=>{const o=i(e);if(o[t])return o[t][n]},c=(e,t,n=o)=>{"session"!==e?l(t,n):u(t,n)},i=(e,t=o)=>"session"===e?d(t):g(t),l=(e,t)=>{localStorage.setItem(t,JSON.stringify(e))},g=e=>JSON.parse(localStorage.getItem(e))||{},u=(e,t)=>{sessionStorage.setItem(t,JSON.stringify(e))},d=e=>JSON.parse(sessionStorage.getItem(e))||{};let f="";const h=e=>{const t=p();return t?`${t}${e}`:(console.log("URL is not valid"),"")},p=()=>f;let y=!1;const m=e=>{return!!e&&(e.edgeURL?(y=!!e.disableConsentCheck,null!=(t=e.edgeURL)&&(f=t),!0):(console.error("Please provide URL for EdgeTag"),!1));var t},w=()=>{const e=navigator;let t=e.userAgent;return t+=e.brave?`${t} Brave`:"",t},b=o=>{const r=a("local",e,t);if(y)return!0;if(!r)return!1;if(!0===r.all)return!0;if(!o)return Object.values(r).find((e=>e));const s=a("local",e,n)||[];for(const e of s){const t=o[e];if((t||!0===o.all&&null==t||!1===o.all&&!0===t)&&r[e])return!0}return!1},v=(e,t)=>{let n;return t&&(n=new Blob([JSON.stringify(t)],{type:"application/json"})),navigator.sendBeacon(e,n)},S=async(e,t,n)=>await fetch(t,{method:e,headers:{"Content-type":"application/json; charset=utf-8",Accept:"application/json; charset=utf-8"},body:JSON.stringify(n),credentials:"include"}).then((e=>e.json().then((t=>({status:e.status,body:t}))))).then((e=>e.status<200||e.status>=300?Promise.reject(new Error(JSON.stringify(e.body))):Promise.resolve(e.body))).catch((e=>Promise.reject(new Error(e))));async function k(e,t,n){if(!e)return Promise.reject(new Error("URL is empty"));const o=(e=>{const t={pageUrl:window.location.href,userAgent:w(),...e};let n={};const o=i("session");o&&(n={...n,...o});const r=i("local");return r&&(n={...n,...r}),t.storage=n,t})(t);return n&&"beacon"===n.method&&navigator.sendBeacon?Promise.resolve(v(e,o)):await S("POST",e,o)}const O=e=>{},j=n=>{s("local",e,n,t)},E=e=>{const t={consentString:e};j(e),k(h("/consent"),t).catch(O)},P=(e,t,n)=>{let o={eventName:e};t&&(o.data=t),n&&(o.providers=n),b(n)?k(h("/tag"),o).catch(O):console.log("No consent")},N=(e,t,n,o)=>{let s;switch(t){case"cookie":s=(e=>{let t=`${e}=`,n=decodeURIComponent(document.cookie).split(";");for(let e=0;e<n.length;e++){let o=n[e];for(;" "==o.charAt(0);)o=o.substring(1);if(0==o.indexOf(t))return o.substring(t.length,o.length)}return""})(n);break;case"local":s=sessionStorage.getItem(n);break;case"session":s=localStorage.getItem(n)}s&&r(o,e,s,n)},U=(e,t)=>{t.forEach((t=>{switch(t.type){case"query":((e,t,n)=>{const o=new URLSearchParams(window.location.search);o&&o.get(t)&&r(n,e,o.get(t),t)})(e,t.key,t.persist);break;case"storage":N(e,t.location,t.key,t.persist)}}))},R=t=>{if(!m(t))return;let o=`${h("/init")}`;t.disableConsentCheck&&(o=`${o}?consentDisabled=true`,j({all:!0})),async function(e,t){return e?t&&"beacon"===t.method&&navigator.sendBeacon?Promise.resolve(v(e)):await S("GET",e):Promise.reject(new Error("URL is empty"))}(o).then((t=>{t?(t=>{const o=[];t.forEach((e=>{o.push(e.package),Object.entries(e.rules).forEach((([t,n])=>{"capture"!==t||U(e.package,n)}))})),s("local",e,o,n)})(t.result):console.log("init failed")})).catch(O)};var I=new class{init(e){(e=>{R(e)})(e)}tag(e,t,n){((e,t,n)=>{P(e,t,n)})(e,t,n)}consent(e){(e=>{E(e)})(e)}};!function(){const e=e=>{const n=[].slice.call(e);if(Array.isArray(n))try{return t[n[0]](...n.slice(1))}catch(e){console.error(e)}},t=I;let n=[];window.edgetag&&(n=window.edgetag.stubs||[]),window.edgetag=function(){return e(arguments)},n.forEach(e)}()}));
|
package/index.min.js.gz
DELETED
|
Binary file
|