Hello, I am trying to write my first extension for Firefox.
This extension should get the title and URL of all the open tabs, and then store these information inside the local storage in this form:
{
1: {title: "Google", url: "https://google.com"},
2: ...,
3: ...
}
Once some tabs are stored, there is a button available to open all the stored tabs at once.
This button, when no tabs are stored, is disabled.
As the user saves some tabs, it should become available, but this doesn’t happen.
I’ve defined the function to access the storage and I’ve set a listener to the onChanged event of the storage like this
let getStorage = browser.storage.local.get();
browser.storage.onChanged.addListener(updateButtonsState);
the listener is triggered and it calls this function that should change the status of the buttons:
function updateButtonsState() {
// Get the data from the storage
getStorage.then(list => {
// If no tabs are stored
if (Object.keys(list).length === 0) {
// Set the button to open the stored tabs as disabled
getStoredBtn.setAttribute("disabled", '');
// Set the button to delete the storage as disabled
delStoredBtn.setAttribute("disabled", '');
}
else {
// Set the button to open the stored tabs as enabled
getStoredBtn.removeAttribute("disabled");
// Set the button to delete the storage as enabled
delStoredBtn.removeAttribute("disabled");
}
});
}
Even if inside the storage inspector I can see the tabs stored, the function above sees the storage as empty, if I refresh the page, the button is set as active, if I refresh again, it is disabled. In addition, a function to clear the storage seems to don’t work properly.
I do not understand this behavior.
The code posted doesn’t cover all the process, which would be a lot of code to post. If you want, you can see the full code on Github and even download it to see it in action.
This is the link: https://github.com/LuigiCaradonna/TabsUrl
Just as a quick guide (ask me if you need more details):
- the entry point is bg.js which wakes cs.js as the extension’s button is clicked
- cs.js initializes a modal window (modal.js) containing the list of the open tabs and 4 buttons at the bottom
- manage_storage.js contains the code which interacts with the local storage
- save_text.js has its own purpose, it saves the links of the open tabs inside a text file, it works correctly and it is not involved with the troublesome process
Do you see where the problem is?