|
@@ -7,22 +7,54 @@ export class History {
|
|
|
protected _sdk: SDK;
|
|
|
private currentRef: string;
|
|
|
private currentUrl: string;
|
|
|
+
|
|
|
constructor(sdk: SDK) {
|
|
|
this._sdk = sdk;
|
|
|
this.init();
|
|
|
}
|
|
|
|
|
|
init(): void {
|
|
|
- global.document.addEventListener(
|
|
|
- 'readystatechange',
|
|
|
- this.handleReadystatechange,
|
|
|
- );
|
|
|
+ console.log('history init');
|
|
|
+ global.document.addEventListener('readystatechange', this.handleReadystatechange, true);
|
|
|
+ global.history.pushState = hook(global.history, 'pushState', this.handlePushState);
|
|
|
+ global.history.replaceState = hook(global.history, 'replaceState', this.handlePushState);
|
|
|
+ const {
|
|
|
+ screen: { width, height },
|
|
|
+ navigator: { language },
|
|
|
+ location: { hostname, pathname, search },
|
|
|
+ localStorage,
|
|
|
+ document,
|
|
|
+ history,
|
|
|
+ } = global;
|
|
|
+ this.currentUrl = `${pathname}${search}`;
|
|
|
+ this.currentRef = global.document.referrer;
|
|
|
}
|
|
|
|
|
|
handleReadystatechange(): void {
|
|
|
-
|
|
|
+ console.log('handleReadystatechange');
|
|
|
}
|
|
|
|
|
|
+ handlePushState(data: any, unused: string, url?: string | URL): void {
|
|
|
+ console.log('handlePushState', data, unused, url);
|
|
|
+ if (!url) return;
|
|
|
+ this.currentRef = this.currentUrl;
|
|
|
+ const newUrl = url.toString();
|
|
|
+ if (newUrl.substring(0, 4) === 'http') {
|
|
|
+ this.currentUrl = '/' + newUrl.split('/').splice(3).join('/');
|
|
|
+ } else {
|
|
|
+ this.currentUrl = newUrl;
|
|
|
+ }
|
|
|
+ console.log('this.currentUrl', this.currentUrl);
|
|
|
+ console.log('this.currentRef', this.currentRef);
|
|
|
+
|
|
|
+ if (this.currentUrl !== this.currentRef) {
|
|
|
+ console.log('trackView');
|
|
|
+ // trackView();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ handleReplaceState(data: any, unused: string, url?: string | URL): void {
|
|
|
+ console.log('handleReplaceState', data, unused, url);
|
|
|
+ }
|
|
|
}
|
|
|
// const handlePush = (state, title, url) => {
|
|
|
// if (!url) return;
|