viewEvent.ts 2.07 KB
/*
 * @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()