Thanks to wOxxOm and Nadia Chibrikova for answering the question. In unloading the script, it is best to use a non-persistent background script, like so:
...
"background": {
"scripts": ["background.js"],
"persistent": false
},
...
This would unload after 15-30 seconds regardless of any intervals, as long as there are no open message ports and you don't inspect it in devtools. For my purposes, I am keeping a message port open, so I use a variable to keep track of whether Chrome is idle or not by using chrome.idle.onStateChanged, and modified my code accordingly, with this:
chrome.idle.setDetectionInterval(15);
chrome.idle.onStateChanged.addListener(function (state) {
if (state === "locked") {
isLocked = true;
} else {
isLocked = false;
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…