After digging around some I found this StackOverflow thread, which lists a number of workarounds.
The solution that I used was setting alarms (1 minute period), which seem to wake the script up when they go off, as well as extend the idle time. One alarm going off every minute wasn’t enough to keep the script alive. I set three alarms, staggered so that each goes off every 20 seconds, and that seems to work.
browser.alarms.create("keep-loaded-alarm-0", {
periodInMinutes: 1
});
setTimeout(() => browser.alarms.create("keep-loaded-alarm-1", {
periodInMinutes: 1
}), "20000");
setTimeout(() => browser.alarms.create("keep-loaded-alarm-2", {
periodInMinutes: 1
}), "40000");
browser.alarms.onAlarm.addListener(() => {
console.log("keeping extension alive - log for debug");
});
The only caveat is that the script shuts off initially, and then comes back on after the first alarm goes off for the first time (alarms don’t go off for a minute, so the script is idle 1 minute before anything extends it). For my use case, this isn’t a massive issue.