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