viewEvent.ts
2.07 KB
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
72
73
74
75
76
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-30 14:45:53
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-13 17:57:01
* @FilePath: \glsx-tracker-javascript\src\modules\viewEvent.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import type { viewBase, routerOption } from '../types/view'
import Events from './events'
class ViewEvent {
routerModel: routerOption | undefined = 'hash'
/**
* 初始化页面信息
*$page_title: 页面标题
$page_url: 页面地址
$page_path: 页面地址
$page_referrer: 页面来源
*/
initPage(): viewBase {
return {
$page_title: document.title,
$page_url: location.href,
$page_path: location.pathname,
$page_referrer: ''
}
}
/**
* 初始化
*/
init(routerModel?: routerOption) {
this.routerModel = routerModel
this.initPage()
this.initEvent()
}
/**
* 重写pushState/replaceState事件
*/
initEvent() {
window.history['pushState'] = this.createHistoryEvnent('pushState')
window.history['replaceState'] = this.createHistoryEvnent('replaceState')
if (this.routerModel == 'history') {
this.captureEvents(['pushState', 'replaceState', 'popstate'])
}
if (this.routerModel == 'hash') {
this.captureEvents(['hashchange'])
}
}
captureEvents<T>(MouseEventList: string[]) {
MouseEventList.forEach(event => {
Events.addEvent(window, event, (e: MouseEvent) => {
console.log(555)
})
})
}
createHistoryEvnent = <T extends keyof History>(type: T): (() => any) => {
const origin = history[type]
return function (this: any) {
const res = origin.apply(this, arguments)
var e = new Event(type)
window.dispatchEvent(e)
return res
}
}
/**
* 获取页面信息
* @param target 接收的数据
*/
getPresetProperties() {}
/**
* 绑定浏览事件
*/
trigger() {}
}
export default new ViewEvent()