Good article!
I would like to point out that your "createContextMenu" has a bug.
function createContextMenu(createProperties) {
return new Promise((resolve, reject) => {
chrome.contextMenus.create(createProperties, () => {
const lastError = chrome.runtime.lastError
if (lastError && lastError.message &&
!lastError.message.startsWith("Cannot create item with duplicate id")) {
reject(lastError);
} else {
resolve();
}
});
});
}
You create a context menu and ignore the "duplicate id" error. For example, when you publish a new version, and you want to change the context menu's title, above code will fail to update it since "duplicate id".
Although there is a contextMenus.update() method, there is no contextMenus.get() method. So to update a context menu, I do it with contextMenus.remove/removeAll() then contextMenus.create() .
About the alarm use case:
Check that alarms don’t already exist before adding them. Otherwise, if you set an alarm for five minutes time, the Background ServiceWorker will restart before it fires. At that point, the alarm is recreated (no errors) for five minutes time again. This repeats ad infinitum, and five minutes never comes!
This is because you set the alarm when the service worker starts. Most use cases, setAlarm happens on an event, not on startup.