I am working on a context menu that links to a page mod and is facing a problem.
I can send a message with the right mouse button on the page / tab until I refresh the page. When I refresh the page, a new working menu is created and the context menu cannot communicate with the working one.
Now I have two identical workers, but he looks like an old one, expired. This means that this loop in onMessage: does not work because it raises the first worker.
for (index = 0; index < workers.length; index += 1) {
if (workers[index].tab.index === tabs.activeTab.index) {
workers[index].port.emit("rightClick", string, ss.storage.product);
}
}
I was looking to remove the old worker when upgrading, but there seems to be no way to do this. Did I fundamentally miss something about working with workers?
The error I get is: Error: The page is currently hidden and can no longer be used until it is visible again.
This is consistent with the fact that with respect to the worker, a new page is now viewing me on the same tab. I thought work.on ('detach', function () {}) should have handled this, but it looks like it is only when the tab is closed.
Any advice would be appreciated.
added OK after a short break, I decided to use the detachWorker function, recommended elsewhere for disconnecting. I placed it at the top of my pageMod object as shown below
// Clean up duplicate worker
for (index in workers) {
if(workers[index].url === worker.url && workers[index].tab.index === worker.tab.index) {
detachWorker(workers[index], workers);
}
}
This fixes the problem (for now), although I don't think this is the right approach. Any success in solving :).