Hello,
I’m writing an extension that uses a content script that gets executed in a given tab with the user preses the browserAction icon button.
The desired behavour I’m looking for is as follows:
- First press - Executes content script and triggers a function (showing a div for example)
- Second press - Sends a message to content script and triggers a function (hides the div for example)
- Third press shows the div… and so on.
At the moment, every press of the button executes a new script.
I have an idea of how to achieve my desied outcome but it seems a bit overkill and I’m wondering if anone has faced a similar challenge and if there’s a more elegant way.
My current idea:
- Query for all tabs and keep track of open and closed tabs with event listeners
- Wite tab id’s to an object to keep track of them i.e
{tabID1: {status: false}, tabID2: {status: true}}
- When the user presses the button, check if tabID1.status is false, if so execute script and send initial toggle message
- 2nd press - Again, check tab.id.status and if the extension is running (div is visible), send a message to hide the div.
Is this best way or am I overthinking it?
Thanks,
Nick