ora/src/background/index.js

40 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-09-17 14:27:08 +02:00
import browser from 'webextension-polyfill'
2020-09-18 21:00:59 +02:00
import { dashboard } from '../shared/utils'
2020-09-19 01:16:43 +02:00
import { insertLog, normalizeTimestamp } from '../shared/db'
2020-09-17 14:27:08 +02:00
2020-09-18 21:00:59 +02:00
browser.browserAction.onClicked.addListener(() => browser.tabs.create({ url: dashboard, active: true }))
2020-09-17 14:27:08 +02:00
2020-09-19 02:05:33 +02:00
const frequency = 1000
2020-09-17 14:27:08 +02:00
async function getAllTabs() {
2020-09-19 02:05:33 +02:00
try {
const tabs = await browser.tabs.query({})
const windows = await browser.windows.getAll()
const active = tabs
.filter((tab) => {
const window = windows.find((window) => window.id === tab.windowId)
return tab.active && window.focused
})
.map(({ id, title, url }) => {
const { host } = new URL(url)
return { id, title, host }
})
2020-09-18 21:00:59 +02:00
2020-09-19 02:05:33 +02:00
await Promise.all(
active.map(({ host }) => {
if (host)
return insertLog({
timestamp: normalizeTimestamp(new Date()),
host,
seconds: (frequency / 1000) | 0,
})
})
)
} catch {}
2020-09-17 14:27:08 +02:00
}
setInterval(() => {
getAllTabs()
2020-09-18 21:00:59 +02:00
}, frequency)