qx
qx
12 小时以前 495bf3ca62536c52cf78895501205965cae05828
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const uaParser = require('ua-parser-js')
// https://afantasy.ninja/2017/05/08/user-tracking-iii/
// 用户停留时间
// addEvent() 是包装了 `window.addEventListener` 和 `window.attachEvent` 的事件监听函数
const r = window.requestAnimationFrame
const c = window.cancelAnimationFrame
let h
let lt = 0
let ltStart
let inActiveTime
const inActiveThreshold = 60 * 60 * 100
// 创建一个心跳闭包,负责向 lifetime 增加累计时间
h = (function () {
  let timer
 
  function beat() {
    const now = new Date()
    const diff = now - ltStart
    lt = lt + diff
    inActiveTime = inActiveTime + diff
    ltStart = now
    if (inActiveTime <= inActiveThreshold) {
      timer = r(beat)
    } else {
      timer = null
    }
    // document.getElementById('inActiveTime').innerText = inActiveTime
  }
 
  return {
    start: function () {
      if (!timer) {
        ltStart = new Date()
        timer = r(beat)
      }
    },
    stop: function () {
      if (timer) {
        c(timer)
        timer = null
      }
    }
  }
})()
 
function onFocus() {
  h.start()
}
 
function onBlur() {
  h.stop()
}
 
// 在 PC 端使用 focusin / focusout / focus / blur 事件
if ('onfocusin' in document) {
  document.onfocusin = onFocus
  document.onfocusout = onBlur
} else {
  window.onfocus = onFocus
  window.onblur = onBlur
}
// 在移动端使用 Page Visibility API 检查页面是否 active
const prefixes = ['', 'webkit', 'moz', 'ms', 'o']
let pf
let hiddenKey
let eventKey
if (isMobile) {
  for (let i = 0; i < prefixes.length; i++) {
    pf = prefixes[i]
    hiddenKey = pf ? pf + 'Hidden' : 'hidden'
    if (hiddenKey in document) {
      eventKey = pf + 'visibilitychange'
      break
    }
  }
  if (eventKey) {
    addEvent(document, eventKey, function () {
      document[hiddenKey] ? onBlur() : onFocus()
    })
  }
}
inActiveTime = 0
h.start() // 开始计算 lifetime
 
function isMobile() {
  const ua = uaParser(navigator.userAgent)
  if (ua.device) {
    let { type } = ua.device
    if (type && type == 'mobile') {
      return true
    }
  }
  return false
}
 
function addEvent(element, event, callback) {
  if (element.addEventListener) {
    // 支持使用 addEventListener()
    if (event.slice(0, 2) === 'on')
      // 以 "on" 开头,不需要,则去掉
      event = event.slice(2)
    element.addEventListener(event, callback)
  } else if (element.attachEvent) {
    // 支持使用 attachEvent()
    if (event.slice(0, 2) !== 'on')
      // 没有以 "on" 开头,需要,则加上
      event = 'on' + event
    element.attachEvent(event, callback)
  } else {
    event.slice(0, 2) !== 'on' ? (element['on' + event] = callback) : (element[event] = callback)
  }
}