This commit is contained in:
2021-11-23 01:45:19 +01:00
parent 57b9b875ba
commit 2fbdea00a6
15 changed files with 357 additions and 263 deletions

View File

@@ -3,7 +3,7 @@ import dayjs from 'dayjs'
import { dashboard } from '../shared/utils'
import { insertLog, normalizeTimestamp, DB } from '../shared/db'
import { getUsageForHost, percentagesToBool } from '../shared/lib'
import { getSettingsWithDefaults, getUsageForHost, percentagesToBool } from '../shared/lib'
browser.browserAction.onClicked.addListener(() => browser.tabs.create({ url: dashboard, active: true }))
@@ -18,22 +18,33 @@ async function log() {
const window = windows.find((window) => window.id === tab.windowId)
return tab.active && window.focused
})
.map(({ id, title, url }) => {
.map(({ url, audible, mutedInfo }) => {
const { host } = new URL(url)
return { id, title, host }
return { host, audio: audible && !mutedInfo.muted }
})
.filter((x) => x.host)
await Promise.all(
active.map(({ host }) => {
if (host)
return insertLog({
timestamp: normalizeTimestamp(new Date()),
host,
seconds: (frequency / 1000) | 0,
})
if (active.length === 0) return
const settings = await getSettingsWithDefaults()
let idle = false
if (settings.idleTimeout > 0) {
idle = dayjs(settings.lastActivity).add(settings.idleTimeout, 'minutes').isBefore(dayjs())
}
const inserted = active
.filter((tab) => !idle || tab.audio)
.map((tab) => {
return insertLog({
timestamp: normalizeTimestamp(new Date()),
host: tab.host,
seconds: (frequency / 1000) | 0,
})
})
)
} catch {}
await Promise.all(inserted)
} catch (e) {
console.error(e)
}
}
async function deleteOldLogs() {