-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
101 lines (83 loc) · 2.06 KB
/
background.js
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
const POLL_INTERVAL_SECONDS = 5
const SECONDS_UNTIL_IDLE = 15
let ACTIVE_TAB = {}
const pollActiveTab = () => {
console.log("polling...")
const idleState = getIdleState()
const currentTab = getCurrentTab()
console.log(currentTab.title)
console.log(ACTIVE_TAB.title)
const hasActiveTabChanged = currentTab.title == ACTIVE_TAB.title ? true : false
const isTabPlayingAudio = currentTab.audible
if (idleState == "active" && !hasActiveTabChanged) {
incrementActiveTab()
return
}
if (idleState == "active" && hasActiveTabChanged) {
submitActiveTab()
updateActiveTab(currentTab)
return
}
if (idleState == "idle" && !hasActiveTabChanged && isTabPlayingAudio) {
incrementActiveTab()
return
}
if (idleState == "idle" && !hasActiveTabChanged && !isTabPlayingAudio) {
submitActiveTab()
resetActiveTab()
return
}
if (idleState == "locked") {
submitActiveTab()
resetActiveTab()
return
}
console.log("uncaught condition")
}
const submitActiveTab = () => {
console.log("submitting")
console.log(ACTIVE_TAB)
}
const updateActiveTab = (tab) => {
ACTIVE_TAB.start = new Date()
ACTIVE_TAB.url = tab.url || tab.pendingUrl
ACTIVE_TAB.domain = new URL(ACTIVE_TAB.url).hostname
ACTIVE_TAB.title = tab.title
ACTIVE_TAB.seconds = 0
}
const incrementActiveTab = () => {
ACTIVE_TAB.seconds += POLL_INTERVAL_SECONDS
}
const resetActiveTab = () => {
ACTIVE_TAB = {
start: null,
url: null,
domain: null,
title: null,
seconds: null
}
}
const getIdleState = () => {
return chrome.idle.queryState(SECONDS_UNTIL_IDLE, (idleState) => {
return idleState
})
}
const getCurrentTab = () => {
return chrome.tabs.query({"active": true, "lastFocusedWindow": true}, (tabs) => {
return tabs[0]
})
}
console.log("starting")
ACTIVE_TAB = resetActiveTab()
setInterval(pollActiveTab,POLL_INTERVAL_SECONDS*100)
// if hasUrlChanged
// submit
// startNew
// return
// if (idle + isAudioPlaying) || (active)
// increment
// return
// if idle || locked
// submit
// reset
// return