@ohbug/extension-view 0.0.1 → 0.0.2

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/dist/index.umd.js DELETED
@@ -1,450 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.OhbugExtensionView = factory());
5
- }(this, (function () { 'use strict';
6
-
7
- function createExtension(extension) {
8
- return extension;
9
- }
10
-
11
- const DEFAULT_TIMEOUT = 30 * 60 * 1000 ;
12
- class Session {
13
- constructor(timeout = DEFAULT_TIMEOUT) {
14
- this.session = null;
15
- this.timeout = DEFAULT_TIMEOUT;
16
- this.session = new Date().getTime();
17
- this.timeout = timeout;
18
- }
19
- /**
20
- * 检查当前 session 是否过期
21
- *
22
- * @returns {boolean}
23
- */
24
- isExpired() {
25
- if (!this.session) {
26
- throw new Error('Session 没有初始化');
27
- }
28
- const now = new Date().getTime();
29
- return this.session + this.timeout < now;
30
- }
31
- update() {
32
- if (!this.session) {
33
- throw new Error('Session 没有初始化');
34
- }
35
- const now = new Date().getTime();
36
- this.session = now;
37
- return this.session;
38
- }
39
- }
40
-
41
- function error(condition, format, ...args) {
42
- if (format === undefined) {
43
- throw new Error('`Ohbug warning(condition, format, ...args)` requires a warning message argument');
44
- }
45
- if (!condition) {
46
- let argIndex = 0;
47
- const message = format.replace(/%s/g, () => args[argIndex++]);
48
- throw new Error(`Ohbug ${message}`);
49
- }
50
- }
51
-
52
- const fallbackGlobalObject = {};
53
- function getGlobal() {
54
- return (typeof window !== 'undefined'
55
- ? window
56
- : typeof global !== 'undefined'
57
- ? global
58
- : typeof self !== 'undefined'
59
- ? self
60
- : fallbackGlobalObject);
61
- }
62
- function getOhbugObject() {
63
- const _global = getGlobal();
64
- error(Boolean(_global.__OHBUG__), 'Failed to get `OhbugObject`, please confirm if `Ohbug.init`');
65
- return _global.__OHBUG__;
66
- }
67
-
68
- function replace(source, name, behavior) {
69
- if (!(name in source)) {
70
- return;
71
- }
72
- const original = source[name];
73
- const wrapped = behavior(original);
74
- source[name] = wrapped;
75
- return original;
76
- }
77
- function parseUrl(url) {
78
- if (typeof url !== 'string') {
79
- return {};
80
- }
81
- // eslint-disable-next-line
82
- const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
83
- if (!match) {
84
- return {};
85
- }
86
- const query = match[6] || '';
87
- const fragment = match[8] || '';
88
- return {
89
- host: match[4],
90
- path: match[5],
91
- protocol: match[2],
92
- relative: match[5] + query + fragment,
93
- };
94
- }
95
-
96
- function sendPageView(path, initial) {
97
- const { client } = getOhbugObject();
98
- const event = client.createEvent({
99
- category: 'view',
100
- type: 'pageView',
101
- detail: {
102
- initial,
103
- path,
104
- },
105
- });
106
- client.notify(event);
107
- }
108
- function sendUserView(path) {
109
- const { client } = getOhbugObject();
110
- const event = client.createEvent({
111
- category: 'view',
112
- type: 'userView',
113
- detail: {
114
- path,
115
- },
116
- });
117
- client.notify(event);
118
- }
119
-
120
- function createCommonjsModule(fn) {
121
- var module = { exports: {} };
122
- return fn(module, module.exports), module.exports;
123
- }
124
-
125
- /*!
126
- * JavaScript Cookie v2.2.1
127
- * https://github.com/js-cookie/js-cookie
128
- *
129
- * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
130
- * Released under the MIT license
131
- */
132
-
133
- var js_cookie = createCommonjsModule(function (module, exports) {
134
- (function (factory) {
135
- var registeredInModuleLoader;
136
- {
137
- module.exports = factory();
138
- registeredInModuleLoader = true;
139
- }
140
- if (!registeredInModuleLoader) {
141
- var OldCookies = window.Cookies;
142
- var api = window.Cookies = factory();
143
- api.noConflict = function () {
144
- window.Cookies = OldCookies;
145
- return api;
146
- };
147
- }
148
- }(function () {
149
- function extend () {
150
- var i = 0;
151
- var result = {};
152
- for (; i < arguments.length; i++) {
153
- var attributes = arguments[ i ];
154
- for (var key in attributes) {
155
- result[key] = attributes[key];
156
- }
157
- }
158
- return result;
159
- }
160
-
161
- function decode (s) {
162
- return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
163
- }
164
-
165
- function init (converter) {
166
- function api() {}
167
-
168
- function set (key, value, attributes) {
169
- if (typeof document === 'undefined') {
170
- return;
171
- }
172
-
173
- attributes = extend({
174
- path: '/'
175
- }, api.defaults, attributes);
176
-
177
- if (typeof attributes.expires === 'number') {
178
- attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
179
- }
180
-
181
- // We're using "expires" because "max-age" is not supported by IE
182
- attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';
183
-
184
- try {
185
- var result = JSON.stringify(value);
186
- if (/^[\{\[]/.test(result)) {
187
- value = result;
188
- }
189
- } catch (e) {}
190
-
191
- value = converter.write ?
192
- converter.write(value, key) :
193
- encodeURIComponent(String(value))
194
- .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
195
-
196
- key = encodeURIComponent(String(key))
197
- .replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
198
- .replace(/[\(\)]/g, escape);
199
-
200
- var stringifiedAttributes = '';
201
- for (var attributeName in attributes) {
202
- if (!attributes[attributeName]) {
203
- continue;
204
- }
205
- stringifiedAttributes += '; ' + attributeName;
206
- if (attributes[attributeName] === true) {
207
- continue;
208
- }
209
-
210
- // Considers RFC 6265 section 5.2:
211
- // ...
212
- // 3. If the remaining unparsed-attributes contains a %x3B (";")
213
- // character:
214
- // Consume the characters of the unparsed-attributes up to,
215
- // not including, the first %x3B (";") character.
216
- // ...
217
- stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
218
- }
219
-
220
- return (document.cookie = key + '=' + value + stringifiedAttributes);
221
- }
222
-
223
- function get (key, json) {
224
- if (typeof document === 'undefined') {
225
- return;
226
- }
227
-
228
- var jar = {};
229
- // To prevent the for loop in the first place assign an empty array
230
- // in case there are no cookies at all.
231
- var cookies = document.cookie ? document.cookie.split('; ') : [];
232
- var i = 0;
233
-
234
- for (; i < cookies.length; i++) {
235
- var parts = cookies[i].split('=');
236
- var cookie = parts.slice(1).join('=');
237
-
238
- if (!json && cookie.charAt(0) === '"') {
239
- cookie = cookie.slice(1, -1);
240
- }
241
-
242
- try {
243
- var name = decode(parts[0]);
244
- cookie = (converter.read || converter)(cookie, name) ||
245
- decode(cookie);
246
-
247
- if (json) {
248
- try {
249
- cookie = JSON.parse(cookie);
250
- } catch (e) {}
251
- }
252
-
253
- jar[name] = cookie;
254
-
255
- if (key === name) {
256
- break;
257
- }
258
- } catch (e) {}
259
- }
260
-
261
- return key ? jar[key] : jar;
262
- }
263
-
264
- api.set = set;
265
- api.get = function (key) {
266
- return get(key, false /* read as raw */);
267
- };
268
- api.getJSON = function (key) {
269
- return get(key, true /* read as json */);
270
- };
271
- api.remove = function (key, attributes) {
272
- set(key, '', extend(attributes, {
273
- expires: -1
274
- }));
275
- };
276
-
277
- api.defaults = {};
278
-
279
- api.withConverter = init;
280
-
281
- return api;
282
- }
283
-
284
- return init(function () {});
285
- }));
286
- });
287
-
288
- var dayjs_min = createCommonjsModule(function (module, exports) {
289
- !function(t,e){module.exports=e();}(this,function(){var t="millisecond",e="second",n="minute",r="hour",i="day",s="week",u="month",a="quarter",o="year",f="date",h=/^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/,c=/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,d={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_")},$=function(t,e,n){var r=String(t);return !r||r.length>=e?t:""+Array(e+1-r.length).join(n)+t},l={s:$,z:function(t){var e=-t.utcOffset(),n=Math.abs(e),r=Math.floor(n/60),i=n%60;return (e<=0?"+":"-")+$(r,2,"0")+":"+$(i,2,"0")},m:function t(e,n){if(e.date()<n.date())return -t(n,e);var r=12*(n.year()-e.year())+(n.month()-e.month()),i=e.clone().add(r,u),s=n-i<0,a=e.clone().add(r+(s?-1:1),u);return +(-(r+(n-i)/(s?i-a:a-i))||0)},a:function(t){return t<0?Math.ceil(t)||0:Math.floor(t)},p:function(h){return {M:u,y:o,w:s,d:i,D:f,h:r,m:n,s:e,ms:t,Q:a}[h]||String(h||"").toLowerCase().replace(/s$/,"")},u:function(t){return void 0===t}},y="en",M={};M[y]=d;var m=function(t){return t instanceof S},D=function(t,e,n){var r;if(!t)return y;if("string"==typeof t)M[t]&&(r=t),e&&(M[t]=e,r=t);else {var i=t.name;M[i]=t,r=i;}return !n&&r&&(y=r),r||!n&&y},v=function(t,e){if(m(t))return t.clone();var n="object"==typeof e?e:{};return n.date=t,n.args=arguments,new S(n)},g=l;g.l=D,g.i=m,g.w=function(t,e){return v(t,{locale:e.$L,utc:e.$u,x:e.$x,$offset:e.$offset})};var S=function(){function d(t){this.$L=D(t.locale,null,!0),this.parse(t);}var $=d.prototype;return $.parse=function(t){this.$d=function(t){var e=t.date,n=t.utc;if(null===e)return new Date(NaN);if(g.u(e))return new Date;if(e instanceof Date)return new Date(e);if("string"==typeof e&&!/Z$/i.test(e)){var r=e.match(h);if(r){var i=r[2]-1||0,s=(r[7]||"0").substring(0,3);return n?new Date(Date.UTC(r[1],i,r[3]||1,r[4]||0,r[5]||0,r[6]||0,s)):new Date(r[1],i,r[3]||1,r[4]||0,r[5]||0,r[6]||0,s)}}return new Date(e)}(t),this.$x=t.x||{},this.init();},$.init=function(){var t=this.$d;this.$y=t.getFullYear(),this.$M=t.getMonth(),this.$D=t.getDate(),this.$W=t.getDay(),this.$H=t.getHours(),this.$m=t.getMinutes(),this.$s=t.getSeconds(),this.$ms=t.getMilliseconds();},$.$utils=function(){return g},$.isValid=function(){return !("Invalid Date"===this.$d.toString())},$.isSame=function(t,e){var n=v(t);return this.startOf(e)<=n&&n<=this.endOf(e)},$.isAfter=function(t,e){return v(t)<this.startOf(e)},$.isBefore=function(t,e){return this.endOf(e)<v(t)},$.$g=function(t,e,n){return g.u(t)?this[e]:this.set(n,t)},$.unix=function(){return Math.floor(this.valueOf()/1e3)},$.valueOf=function(){return this.$d.getTime()},$.startOf=function(t,a){var h=this,c=!!g.u(a)||a,d=g.p(t),$=function(t,e){var n=g.w(h.$u?Date.UTC(h.$y,e,t):new Date(h.$y,e,t),h);return c?n:n.endOf(i)},l=function(t,e){return g.w(h.toDate()[t].apply(h.toDate("s"),(c?[0,0,0,0]:[23,59,59,999]).slice(e)),h)},y=this.$W,M=this.$M,m=this.$D,D="set"+(this.$u?"UTC":"");switch(d){case o:return c?$(1,0):$(31,11);case u:return c?$(1,M):$(0,M+1);case s:var v=this.$locale().weekStart||0,S=(y<v?y+7:y)-v;return $(c?m-S:m+(6-S),M);case i:case f:return l(D+"Hours",0);case r:return l(D+"Minutes",1);case n:return l(D+"Seconds",2);case e:return l(D+"Milliseconds",3);default:return this.clone()}},$.endOf=function(t){return this.startOf(t,!1)},$.$set=function(s,a){var h,c=g.p(s),d="set"+(this.$u?"UTC":""),$=(h={},h[i]=d+"Date",h[f]=d+"Date",h[u]=d+"Month",h[o]=d+"FullYear",h[r]=d+"Hours",h[n]=d+"Minutes",h[e]=d+"Seconds",h[t]=d+"Milliseconds",h)[c],l=c===i?this.$D+(a-this.$W):a;if(c===u||c===o){var y=this.clone().set(f,1);y.$d[$](l),y.init(),this.$d=y.set(f,Math.min(this.$D,y.daysInMonth())).$d;}else $&&this.$d[$](l);return this.init(),this},$.set=function(t,e){return this.clone().$set(t,e)},$.get=function(t){return this[g.p(t)]()},$.add=function(t,a){var f,h=this;t=Number(t);var c=g.p(a),d=function(e){var n=v(h);return g.w(n.date(n.date()+Math.round(e*t)),h)};if(c===u)return this.set(u,this.$M+t);if(c===o)return this.set(o,this.$y+t);if(c===i)return d(1);if(c===s)return d(7);var $=(f={},f[n]=6e4,f[r]=36e5,f[e]=1e3,f)[c]||1,l=this.$d.getTime()+t*$;return g.w(l,this)},$.subtract=function(t,e){return this.add(-1*t,e)},$.format=function(t){var e=this;if(!this.isValid())return "Invalid Date";var n=t||"YYYY-MM-DDTHH:mm:ssZ",r=g.z(this),i=this.$locale(),s=this.$H,u=this.$m,a=this.$M,o=i.weekdays,f=i.months,h=function(t,r,i,s){return t&&(t[r]||t(e,n))||i[r].substr(0,s)},d=function(t){return g.s(s%12||12,t,"0")},$=i.meridiem||function(t,e,n){var r=t<12?"AM":"PM";return n?r.toLowerCase():r},l={YY:String(this.$y).slice(-2),YYYY:this.$y,M:a+1,MM:g.s(a+1,2,"0"),MMM:h(i.monthsShort,a,f,3),MMMM:h(f,a),D:this.$D,DD:g.s(this.$D,2,"0"),d:String(this.$W),dd:h(i.weekdaysMin,this.$W,o,2),ddd:h(i.weekdaysShort,this.$W,o,3),dddd:o[this.$W],H:String(s),HH:g.s(s,2,"0"),h:d(1),hh:d(2),a:$(s,u,!0),A:$(s,u,!1),m:String(u),mm:g.s(u,2,"0"),s:String(this.$s),ss:g.s(this.$s,2,"0"),SSS:g.s(this.$ms,3,"0"),Z:r};return n.replace(c,function(t,e){return e||l[t]||r.replace(":","")})},$.utcOffset=function(){return 15*-Math.round(this.$d.getTimezoneOffset()/15)},$.diff=function(t,f,h){var c,d=g.p(f),$=v(t),l=6e4*($.utcOffset()-this.utcOffset()),y=this-$,M=g.m(this,$);return M=(c={},c[o]=M/12,c[u]=M,c[a]=M/3,c[s]=(y-l)/6048e5,c[i]=(y-l)/864e5,c[r]=y/36e5,c[n]=y/6e4,c[e]=y/1e3,c)[d]||y,h?M:g.a(M)},$.daysInMonth=function(){return this.endOf(u).$D},$.$locale=function(){return M[this.$L]},$.locale=function(t,e){if(!t)return this.$L;var n=this.clone(),r=D(t,e,!0);return r&&(n.$L=r),n},$.clone=function(){return g.w(this.$d,this)},$.toDate=function(){return new Date(this.valueOf())},$.toJSON=function(){return this.isValid()?this.toISOString():null},$.toISOString=function(){return this.$d.toISOString()},$.toString=function(){return this.$d.toUTCString()},d}(),p=S.prototype;return v.prototype=p,[["$ms",t],["$s",e],["$m",n],["$H",r],["$W",i],["$M",u],["$y",o],["$D",f]].forEach(function(t){p[t[1]]=function(e){return this.$g(e,t[0],t[1])};}),v.extend=function(t,e){return t.$i||(t(e,S,v),t.$i=!0),v},v.locale=D,v.isDayjs=m,v.unix=function(t){return v(1e3*t)},v.en=M[y],v.Ls=M,v.p={},v});
290
- });
291
-
292
- const NAME = 'OhbugExtensionViewUV';
293
- /**
294
- * initial page view 触发时同时触发
295
- * 先从 cookie 内取值
296
- * 没有值 => 创建 cookie 并记一次 uv
297
- * 有值(当天) => 不动
298
- * 有值(昨天) => 更新 cookie 并记一次 uv
299
- * 有值(明天) => 不动 (不应出现这个情况)
300
- */
301
- function createUserView(path) {
302
- const value = js_cookie.get(NAME);
303
- // 没有值 => 创建 cookie 并记一次 uv
304
- if (!value) {
305
- js_cookie.set(NAME, dayjs_min().toISOString(), { expires: 30 });
306
- sendUserView(path);
307
- }
308
- else {
309
- const parsedValue = dayjs_min(value);
310
- // 有值(昨天) => 更新 cookie 并记一次 uv
311
- if (parsedValue.isBefore(dayjs_min(), 'day')) {
312
- js_cookie.set(NAME, dayjs_min().toISOString(), { expires: 30 });
313
- sendUserView(path);
314
- }
315
- }
316
- }
317
-
318
- const HIDDEN = 'hidden';
319
- const VISIBLE = 'visible';
320
- /**
321
- * 1. 页面加载时,如果页面的 visibilityState 是可见的,发送 Page View 统计
322
- * 2. 如果页面的 visibilityState 是隐藏的,就监听 visibilitychange 事件,并在 visibilityState 变为可见时发送 Page View 统计
323
- * 3. 如果 visibilityState 由隐藏变为可见,并且自上次用户交互之后已经过了“足够长”的时间,就发送新的 Page View 统计
324
- * 4. 如果 URL 发生变化 发送新的 Page View 统计
325
- */
326
- class PageVisibility {
327
- constructor() {
328
- this.visible = null;
329
- this.lastVisible = null;
330
- this.initialVisible = null;
331
- this.sendPageLoad = false;
332
- this.visibleThresholdTimeout = null;
333
- this.handleVisibleChange = this.handleVisibleChange.bind(this);
334
- window.addEventListener('visibilitychange', this.handleVisibleChange, true);
335
- this.init();
336
- this.session = new Session();
337
- }
338
- static capturePageVisibility() {
339
- return new PageVisibility();
340
- }
341
- init() {
342
- // 页面加载时,如果页面的 visibilityState 是可见的,发送 Page View 统计
343
- if (!this.sendPageLoad) {
344
- if (document.visibilityState === VISIBLE) {
345
- this.initialVisible = true;
346
- sendPageView(window.location.href, true);
347
- createUserView(window.location.href);
348
- this.sendPageLoad = true;
349
- }
350
- else {
351
- this.initialVisible = false;
352
- }
353
- }
354
- }
355
- handleVisibleChange() {
356
- this.lastVisible = this.visible;
357
- if (document.visibilityState === VISIBLE) {
358
- this.visible = true;
359
- }
360
- else if (document.visibilityState === HIDDEN) {
361
- this.visible = false;
362
- }
363
- else {
364
- this.visible = null;
365
- }
366
- const { visible, lastVisible, initialVisible, session } = this;
367
- // 如果页面的 visibilityState 是隐藏的,就监听 visibilitychange 事件,并在 visibilityState 变为可见时发送 Page View 统计
368
- if (initialVisible === false && visible === true) {
369
- sendPageView(window.location.href, true);
370
- createUserView(window.location.href);
371
- // 发送后将 initialVisible 置空 防止重复发送
372
- this.initialVisible = null;
373
- }
374
- // 如果 visibilityState 由隐藏变为可见,并且自上次用户交互之后已经过了“足够长”的时间,就发送新的 Page View 统计
375
- if (lastVisible === false && visible === true) {
376
- if (session.isExpired()) {
377
- sendPageView(window.location.href);
378
- createUserView(window.location.href);
379
- }
380
- }
381
- if (visible === false) {
382
- session.update();
383
- }
384
- }
385
- }
386
-
387
- let lastHref;
388
- function handleUrlChange(from, to) {
389
- const parsedHref = parseUrl(window?.location?.href);
390
- let parsedFrom = parseUrl(from);
391
- const parsedTo = parseUrl(to);
392
- if (!parsedFrom.path) {
393
- parsedFrom = parsedHref;
394
- }
395
- lastHref = to;
396
- let targetFrom = from;
397
- let targetTo = to;
398
- if (parsedHref.protocol === parsedTo.protocol &&
399
- parsedHref.host === parsedTo.host) {
400
- targetTo = parsedTo.relative;
401
- }
402
- if (parsedHref.protocol === parsedFrom.protocol &&
403
- parsedHref.host === parsedFrom.host) {
404
- targetFrom = parsedFrom.relative;
405
- }
406
- if (targetFrom === targetTo)
407
- return;
408
- sendPageView(targetTo);
409
- }
410
- function historyReplacement(original) {
411
- return function call(data, title, url) {
412
- if (url) {
413
- handleUrlChange(lastHref, String(url));
414
- }
415
- return original.apply(this, [data, title, url]);
416
- };
417
- }
418
- function historyListener() {
419
- replace(window?.history, 'pushState', historyReplacement);
420
- replace(window?.history, 'replaceState', historyReplacement);
421
- replace(window, 'onpopstate', () => {
422
- const current = window?.location?.href;
423
- handleUrlChange(lastHref, current);
424
- });
425
- }
426
- function hashListener(e) {
427
- const { oldURL, newURL } = e;
428
- handleUrlChange(oldURL, newURL);
429
- }
430
- /**
431
- * 如果 URL 发生变化 发送新的 Page View 统计
432
- */
433
- function captureUrlChange() {
434
- // history
435
- historyListener();
436
- // hash
437
- window?.addEventListener?.('hashchange', hashListener, true);
438
- }
439
-
440
- const extension = createExtension({
441
- name: 'OhbugExtensionView',
442
- init: () => {
443
- PageVisibility.capturePageVisibility();
444
- captureUrlChange();
445
- },
446
- });
447
-
448
- return extension;
449
-
450
- })));
@@ -1,8 +0,0 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).OhbugExtensionView=e()}(this,(function(){"use strict";const t=18e5;class e{constructor(e=18e5){this.session=null,this.timeout=t,this.session=(new Date).getTime(),this.timeout=e}isExpired(){if(!this.session)throw new Error("Session 没有初始化");const t=(new Date).getTime();return this.session+this.timeout<t}update(){if(!this.session)throw new Error("Session 没有初始化");const t=(new Date).getTime();return this.session=t,this.session}}const n={};function i(){const t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:n;return function(t,e,...n){if(void 0===e)throw new Error("`Ohbug warning(condition, format, ...args)` requires a warning message argument");if(!t){let t=0;const i=e.replace(/%s/g,(()=>n[t++]));throw new Error(`Ohbug ${i}`)}}(Boolean(t.__OHBUG__),"Failed to get `OhbugObject`, please confirm if `Ohbug.init`"),t.__OHBUG__}function r(t,e,n){if(!(e in t))return;const i=t[e],r=n(i);return t[e]=r,i}function s(t){if("string"!=typeof t)return{};const e=t.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);if(!e)return{};const n=e[6]||"",i=e[8]||"";return{host:e[4],path:e[5],protocol:e[2],relative:e[5]+n+i}}function o(t,e){const{client:n}=i(),r=n.createEvent({category:"view",type:"pageView",detail:{initial:e,path:t}});n.notify(r)}function u(t){const{client:e}=i(),n=e.createEvent({category:"view",type:"userView",detail:{path:t}});e.notify(n)}function a(t){var e={exports:{}};return t(e,e.exports),e.exports
2
- /*!
3
- * JavaScript Cookie v2.2.1
4
- * https://github.com/js-cookie/js-cookie
5
- *
6
- * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
7
- * Released under the MIT license
8
- */}var c=a((function(t,e){var n;n=function(){function t(){for(var t=0,e={};t<arguments.length;t++){var n=arguments[t];for(var i in n)e[i]=n[i]}return e}function e(t){return t.replace(/(%[0-9A-Z]{2})+/g,decodeURIComponent)}return function n(i){function r(){}function s(e,n,s){if("undefined"!=typeof document){"number"==typeof(s=t({path:"/"},r.defaults,s)).expires&&(s.expires=new Date(1*new Date+864e5*s.expires)),s.expires=s.expires?s.expires.toUTCString():"";try{var o=JSON.stringify(n);/^[\{\[]/.test(o)&&(n=o)}catch(t){}n=i.write?i.write(n,e):encodeURIComponent(String(n)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),e=encodeURIComponent(String(e)).replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[\(\)]/g,escape);var u="";for(var a in s)s[a]&&(u+="; "+a,!0!==s[a]&&(u+="="+s[a].split(";")[0]));return document.cookie=e+"="+n+u}}function o(t,n){if("undefined"!=typeof document){for(var r={},s=document.cookie?document.cookie.split("; "):[],o=0;o<s.length;o++){var u=s[o].split("="),a=u.slice(1).join("=");n||'"'!==a.charAt(0)||(a=a.slice(1,-1));try{var c=e(u[0]);if(a=(i.read||i)(a,c)||e(a),n)try{a=JSON.parse(a)}catch(t){}if(r[c]=a,t===c)break}catch(t){}}return t?r[t]:r}}return r.set=s,r.get=function(t){return o(t,!1)},r.getJSON=function(t){return o(t,!0)},r.remove=function(e,n){s(e,"",t(n,{expires:-1}))},r.defaults={},r.withConverter=n,r}((function(){}))},t.exports=n()})),h=a((function(t,e){t.exports=function(){var t="millisecond",e="second",n="minute",i="hour",r="day",s="week",o="month",u="quarter",a="year",c="date",h=/^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/,f=/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,l={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_")},d=function(t,e,n){var i=String(t);return!i||i.length>=e?t:""+Array(e+1-i.length).join(n)+t},p={s:d,z:function(t){var e=-t.utcOffset(),n=Math.abs(e),i=Math.floor(n/60),r=n%60;return(e<=0?"+":"-")+d(i,2,"0")+":"+d(r,2,"0")},m:function t(e,n){if(e.date()<n.date())return-t(n,e);var i=12*(n.year()-e.year())+(n.month()-e.month()),r=e.clone().add(i,o),s=n-r<0,u=e.clone().add(i+(s?-1:1),o);return+(-(i+(n-r)/(s?r-u:u-r))||0)},a:function(t){return t<0?Math.ceil(t)||0:Math.floor(t)},p:function(h){return{M:o,y:a,w:s,d:r,D:c,h:i,m:n,s:e,ms:t,Q:u}[h]||String(h||"").toLowerCase().replace(/s$/,"")},u:function(t){return void 0===t}},g="en",w={};w[g]=l;var $=function(t){return t instanceof b},v=function(t,e,n){var i;if(!t)return g;if("string"==typeof t)w[t]&&(i=t),e&&(w[t]=e,i=t);else{var r=t.name;w[r]=t,i=r}return!n&&i&&(g=i),i||!n&&g},m=function(t,e){if($(t))return t.clone();var n="object"==typeof e?e:{};return n.date=t,n.args=arguments,new b(n)},y=p;y.l=v,y.i=$,y.w=function(t,e){return m(t,{locale:e.$L,utc:e.$u,x:e.$x,$offset:e.$offset})};var b=function(){function l(t){this.$L=v(t.locale,null,!0),this.parse(t)}var d=l.prototype;return d.parse=function(t){this.$d=function(t){var e=t.date,n=t.utc;if(null===e)return new Date(NaN);if(y.u(e))return new Date;if(e instanceof Date)return new Date(e);if("string"==typeof e&&!/Z$/i.test(e)){var i=e.match(h);if(i){var r=i[2]-1||0,s=(i[7]||"0").substring(0,3);return n?new Date(Date.UTC(i[1],r,i[3]||1,i[4]||0,i[5]||0,i[6]||0,s)):new Date(i[1],r,i[3]||1,i[4]||0,i[5]||0,i[6]||0,s)}}return new Date(e)}(t),this.$x=t.x||{},this.init()},d.init=function(){var t=this.$d;this.$y=t.getFullYear(),this.$M=t.getMonth(),this.$D=t.getDate(),this.$W=t.getDay(),this.$H=t.getHours(),this.$m=t.getMinutes(),this.$s=t.getSeconds(),this.$ms=t.getMilliseconds()},d.$utils=function(){return y},d.isValid=function(){return!("Invalid Date"===this.$d.toString())},d.isSame=function(t,e){var n=m(t);return this.startOf(e)<=n&&n<=this.endOf(e)},d.isAfter=function(t,e){return m(t)<this.startOf(e)},d.isBefore=function(t,e){return this.endOf(e)<m(t)},d.$g=function(t,e,n){return y.u(t)?this[e]:this.set(n,t)},d.unix=function(){return Math.floor(this.valueOf()/1e3)},d.valueOf=function(){return this.$d.getTime()},d.startOf=function(t,u){var h=this,f=!!y.u(u)||u,l=y.p(t),d=function(t,e){var n=y.w(h.$u?Date.UTC(h.$y,e,t):new Date(h.$y,e,t),h);return f?n:n.endOf(r)},p=function(t,e){return y.w(h.toDate()[t].apply(h.toDate("s"),(f?[0,0,0,0]:[23,59,59,999]).slice(e)),h)},g=this.$W,w=this.$M,$=this.$D,v="set"+(this.$u?"UTC":"");switch(l){case a:return f?d(1,0):d(31,11);case o:return f?d(1,w):d(0,w+1);case s:var m=this.$locale().weekStart||0,b=(g<m?g+7:g)-m;return d(f?$-b:$+(6-b),w);case r:case c:return p(v+"Hours",0);case i:return p(v+"Minutes",1);case n:return p(v+"Seconds",2);case e:return p(v+"Milliseconds",3);default:return this.clone()}},d.endOf=function(t){return this.startOf(t,!1)},d.$set=function(s,u){var h,f=y.p(s),l="set"+(this.$u?"UTC":""),d=(h={},h[r]=l+"Date",h[c]=l+"Date",h[o]=l+"Month",h[a]=l+"FullYear",h[i]=l+"Hours",h[n]=l+"Minutes",h[e]=l+"Seconds",h[t]=l+"Milliseconds",h)[f],p=f===r?this.$D+(u-this.$W):u;if(f===o||f===a){var g=this.clone().set(c,1);g.$d[d](p),g.init(),this.$d=g.set(c,Math.min(this.$D,g.daysInMonth())).$d}else d&&this.$d[d](p);return this.init(),this},d.set=function(t,e){return this.clone().$set(t,e)},d.get=function(t){return this[y.p(t)]()},d.add=function(t,u){var c,h=this;t=Number(t);var f=y.p(u),l=function(e){var n=m(h);return y.w(n.date(n.date()+Math.round(e*t)),h)};if(f===o)return this.set(o,this.$M+t);if(f===a)return this.set(a,this.$y+t);if(f===r)return l(1);if(f===s)return l(7);var d=(c={},c[n]=6e4,c[i]=36e5,c[e]=1e3,c)[f]||1,p=this.$d.getTime()+t*d;return y.w(p,this)},d.subtract=function(t,e){return this.add(-1*t,e)},d.format=function(t){var e=this;if(!this.isValid())return"Invalid Date";var n=t||"YYYY-MM-DDTHH:mm:ssZ",i=y.z(this),r=this.$locale(),s=this.$H,o=this.$m,u=this.$M,a=r.weekdays,c=r.months,h=function(t,i,r,s){return t&&(t[i]||t(e,n))||r[i].substr(0,s)},l=function(t){return y.s(s%12||12,t,"0")},d=r.meridiem||function(t,e,n){var i=t<12?"AM":"PM";return n?i.toLowerCase():i},p={YY:String(this.$y).slice(-2),YYYY:this.$y,M:u+1,MM:y.s(u+1,2,"0"),MMM:h(r.monthsShort,u,c,3),MMMM:h(c,u),D:this.$D,DD:y.s(this.$D,2,"0"),d:String(this.$W),dd:h(r.weekdaysMin,this.$W,a,2),ddd:h(r.weekdaysShort,this.$W,a,3),dddd:a[this.$W],H:String(s),HH:y.s(s,2,"0"),h:l(1),hh:l(2),a:d(s,o,!0),A:d(s,o,!1),m:String(o),mm:y.s(o,2,"0"),s:String(this.$s),ss:y.s(this.$s,2,"0"),SSS:y.s(this.$ms,3,"0"),Z:i};return n.replace(f,(function(t,e){return e||p[t]||i.replace(":","")}))},d.utcOffset=function(){return 15*-Math.round(this.$d.getTimezoneOffset()/15)},d.diff=function(t,c,h){var f,l=y.p(c),d=m(t),p=6e4*(d.utcOffset()-this.utcOffset()),g=this-d,w=y.m(this,d);return w=(f={},f[a]=w/12,f[o]=w,f[u]=w/3,f[s]=(g-p)/6048e5,f[r]=(g-p)/864e5,f[i]=g/36e5,f[n]=g/6e4,f[e]=g/1e3,f)[l]||g,h?w:y.a(w)},d.daysInMonth=function(){return this.endOf(o).$D},d.$locale=function(){return w[this.$L]},d.locale=function(t,e){if(!t)return this.$L;var n=this.clone(),i=v(t,e,!0);return i&&(n.$L=i),n},d.clone=function(){return y.w(this.$d,this)},d.toDate=function(){return new Date(this.valueOf())},d.toJSON=function(){return this.isValid()?this.toISOString():null},d.toISOString=function(){return this.$d.toISOString()},d.toString=function(){return this.$d.toUTCString()},l}(),S=b.prototype;return m.prototype=S,[["$ms",t],["$s",e],["$m",n],["$H",i],["$W",r],["$M",o],["$y",a],["$D",c]].forEach((function(t){S[t[1]]=function(e){return this.$g(e,t[0],t[1])}})),m.extend=function(t,e){return t.$i||(t(e,b,m),t.$i=!0),m},m.locale=v,m.isDayjs=$,m.unix=function(t){return m(1e3*t)},m.en=w[g],m.Ls=w,m.p={},m}()}));const f="OhbugExtensionViewUV";function l(t){const e=c.get(f);if(e){h(e).isBefore(h(),"day")&&(c.set(f,h().toISOString(),{expires:30}),u(t))}else c.set(f,h().toISOString(),{expires:30}),u(t)}const d="visible";class p{constructor(){this.visible=null,this.lastVisible=null,this.initialVisible=null,this.sendPageLoad=!1,this.visibleThresholdTimeout=null,this.handleVisibleChange=this.handleVisibleChange.bind(this),window.addEventListener("visibilitychange",this.handleVisibleChange,!0),this.init(),this.session=new e}static capturePageVisibility(){return new p}init(){this.sendPageLoad||(document.visibilityState===d?(this.initialVisible=!0,o(window.location.href,!0),l(window.location.href),this.sendPageLoad=!0):this.initialVisible=!1)}handleVisibleChange(){this.lastVisible=this.visible,document.visibilityState===d?this.visible=!0:"hidden"===document.visibilityState?this.visible=!1:this.visible=null;const{visible:t,lastVisible:e,initialVisible:n,session:i}=this;!1===n&&!0===t&&(o(window.location.href,!0),l(window.location.href),this.initialVisible=null),!1===e&&!0===t&&i.isExpired()&&(o(window.location.href),l(window.location.href)),!1===t&&i.update()}}let g;function w(t,e){const n=s(window?.location?.href);let i=s(t);const r=s(e);i.path||(i=n),g=e;let u=t,a=e;n.protocol===r.protocol&&n.host===r.host&&(a=r.relative),n.protocol===i.protocol&&n.host===i.host&&(u=i.relative),u!==a&&o(a)}function $(t){return function(e,n,i){return i&&w(g,String(i)),t.apply(this,[e,n,i])}}function v(t){const{oldURL:e,newURL:n}=t;w(e,n)}function m(){r(window?.history,"pushState",$),r(window?.history,"replaceState",$),r(window,"onpopstate",(()=>{const t=window?.location?.href;w(g,t)})),window?.addEventListener?.("hashchange",v,!0)}return{name:"OhbugExtensionView",init:()=>{p.capturePageVisibility(),m()}}}));
@@ -1,21 +0,0 @@
1
- import Session from './session';
2
- /**
3
- * 1. 页面加载时,如果页面的 visibilityState 是可见的,发送 Page View 统计
4
- * 2. 如果页面的 visibilityState 是隐藏的,就监听 visibilitychange 事件,并在 visibilityState 变为可见时发送 Page View 统计
5
- * 3. 如果 visibilityState 由隐藏变为可见,并且自上次用户交互之后已经过了“足够长”的时间,就发送新的 Page View 统计
6
- * 4. 如果 URL 发生变化 发送新的 Page View 统计
7
- */
8
- declare class PageVisibility {
9
- visible: boolean | null;
10
- lastVisible: boolean | null;
11
- initialVisible: boolean | null;
12
- sendPageLoad: boolean;
13
- visibleThresholdTimeout: null;
14
- session: Session;
15
- static capturePageVisibility(): PageVisibility;
16
- constructor();
17
- init(): void;
18
- handleVisibleChange(): void;
19
- }
20
- export default PageVisibility;
21
- //# sourceMappingURL=pageVisibility.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pageVisibility.d.ts","sourceRoot":"","sources":["../src/pageVisibility.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,WAAW,CAAA;AAO/B;;;;;GAKG;AACH,cAAM,cAAc;IAClB,OAAO,EAAE,OAAO,GAAG,IAAI,CAAO;IAE9B,WAAW,EAAE,OAAO,GAAG,IAAI,CAAO;IAElC,cAAc,EAAE,OAAO,GAAG,IAAI,CAAO;IAErC,YAAY,EAAE,OAAO,CAAQ;IAE7B,uBAAuB,OAAO;IAE9B,OAAO,EAAE,OAAO,CAAA;IAEhB,MAAM,CAAC,qBAAqB;;IAW5B,IAAI;IAcJ,mBAAmB;CAgCpB;AAED,eAAe,cAAc,CAAA"}
package/dist/session.d.ts DELETED
@@ -1,14 +0,0 @@
1
- declare class Session {
2
- session: number | null;
3
- timeout: number;
4
- constructor(timeout?: number);
5
- /**
6
- * 检查当前 session 是否过期
7
- *
8
- * @returns {boolean}
9
- */
10
- isExpired(): boolean;
11
- update(): number;
12
- }
13
- export default Session;
14
- //# sourceMappingURL=session.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAGA,cAAM,OAAO;IACX,OAAO,EAAE,MAAM,GAAG,IAAI,CAAO;IAE7B,OAAO,EAAE,MAAM,CAAkB;gBAErB,OAAO,GAAE,MAAwB;IAK7C;;;;OAIG;IACH,SAAS;IAQT,MAAM;CAQP;AAED,eAAe,OAAO,CAAA"}
@@ -1,6 +0,0 @@
1
- /**
2
- * 如果 URL 发生变化 发送新的 Page View 统计
3
- */
4
- declare function captureUrlChange(): void;
5
- export default captureUrlChange;
6
- //# sourceMappingURL=urlChange.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"urlChange.d.ts","sourceRoot":"","sources":["../src/urlChange.ts"],"names":[],"mappings":"AA2EA;;GAEG;AACH,iBAAS,gBAAgB,SAKxB;AAED,eAAe,gBAAgB,CAAA"}
package/dist/user.d.ts DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * initial page view 触发时同时触发
3
- * 先从 cookie 内取值
4
- * 没有值 => 创建 cookie 并记一次 uv
5
- * 有值(当天) => 不动
6
- * 有值(昨天) => 更新 cookie 并记一次 uv
7
- * 有值(明天) => 不动 (不应出现这个情况)
8
- */
9
- declare function createUserView(path: string): void;
10
- export default createUserView;
11
- //# sourceMappingURL=user.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../src/user.ts"],"names":[],"mappings":"AAOA;;;;;;;GAOG;AACH,iBAAS,cAAc,CAAC,IAAI,EAAE,MAAM,QAenC;AAED,eAAe,cAAc,CAAA"}