Skip to content

Commit

Permalink
fix: 🐛 "An extension cannot have more than 500 active alarms" error
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam committed May 12, 2024
1 parent 94d845d commit 01e8ba7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
31 changes: 25 additions & 6 deletions src/entrypoints/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ const debug = (msg: string, ...args: Array<unknown>): void => console.debug(`%c
const m2s = (millis: number): number => Math.round(millis / 1000)
let stats: StatsCollector | undefined = undefined

// register the error handler to catch global errors (unhandled exceptions) and unhandled promise rejections to
// send the error events to the stats collector
;((): void => {
const errorsHandler = (error: unknown): void => {
if (stats && error) {
stats.send(newErrorEvent(error, { page: 'background' })).then((err) => err && debug('stats error', err))
}
}

if (self && self.addEventListener) {
self.addEventListener('error', errorsHandler)
self.addEventListener('error', errorsHandler)
}
})()

// run the background script
;(async () => {
// register the content scripts
Expand Down Expand Up @@ -93,19 +108,23 @@ let stats: StatsCollector | undefined = undefined
}

// create a timer to renew the user-agent automatically
const userAgentRenewTimer = new Timer(m2s(initSettings.renew.intervalMillis), async () => {
const userAgentRenewTimer = new Timer('renew-user-agent', m2s(initSettings.renew.intervalMillis), async () => {
await renewUserAgent(settings, currentUserAgent, remoteUserAgentList, hostOS, latestBrowserVersions)
debug('user-agent was renewed automatically', await currentUserAgent.get())
})

// create a timer to update the remote user-agents list automatically
const remoteListUpdateTimer = new Timer(m2s(initSettings.remoteUseragentList.updateIntervalMillis), async () => {
await remoteUserAgentList.update()
debug('remote user-agent list updated automatically', await remoteUserAgentList.get(false))
})
const remoteListUpdateTimer = new Timer(
'update-remote-list',
m2s(initSettings.remoteUseragentList.updateIntervalMillis),
async () => {
await remoteUserAgentList.update()
debug('remote user-agent list updated automatically', await remoteUserAgentList.get(false))
}
)

// create a timer to update the latest browser versions automatically (once in 6 hours)
const latestBrowserVersionsTimer = new Timer(60 * 60 * 6, async () => {
const latestBrowserVersionsTimer = new Timer('update-latest-browser-versions', 60 * 60 * 6, async () => {
await latestBrowserVersions.update()
debug('latest browser versions updated automatically', latestBrowserVersions.get())
})
Expand Down
6 changes: 3 additions & 3 deletions src/entrypoints/background/timer/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export default class Timer {

/**
* Create a new timer. Keep in mind that the minimum interval is 30 seconds, and the timer should be started
* manually.
* manually. The name of the timer should be unique.
*/
constructor(intervalSec: number, handler: (timer: Timer) => void) {
constructor(name: string, intervalSec: number, handler: (timer: Timer) => void) {
this.intervalSec = Math.max(this.minDelayInSeconds, intervalSec)

this.name = crypto.randomUUID()
this.name = name
this.handler = handler
}

Expand Down

0 comments on commit 01e8ba7

Please sign in to comment.