Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Trying to write a FF extension, I've got problems with the storage

Alhazred

Coder
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:
JSON:
{
    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
JavaScript:
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:
JavaScript:
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: GitHub - 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 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?
 
I've solved the problem, I misunderstood the use of the variable to store the promise, it doesn't act as an alias, but it contains the value returned by browser.storage.local.get() when it is executed for the first time.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom