Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable is set on chrome storage

I have a little snippet working with LocalStorage but I cannot make it work on Chrome Storage scheme yet.

When my application starts I check a variable in localStorage

var bookNarration=parseInt(localStorage.getItem("narration"));

If that variable is undefined, it means that my app has been opened for the first time and I after handle bookLanguage in a switch using the "default" declaration.

switch(window.bookNarration)
  {
    case 2:
        window.narrationShift = window.spanishShift;
    break;
    case 3:
        window.narrationShift = window.frenchShift;
    break;
    case 10:
        window.narrationShift = window.neutralShift;
    break;
    default:
        window.narrationShift = 0; }

To make it work with Chrome Storage I change my code on this way:

var bookNarration=parseInt(chrome.storage.local.get("narration"));

But I immediately get this error:

Invocation of form get(string) doesn't match definition get(optional string or array or object keys, function callback)

I have been searching for many hours trying to find a solution but I can't make it work. I thing that I just need to check if the value is already defined so if It isn't, I could use set() method to store my default value.

like image 933
Juan David Saab Avatar asked Feb 28 '13 18:02

Juan David Saab


People also ask

How do I check my chrome storage?

With the extension's background page open, just go to the developer tools by pressing F12, then go to the Application tab. In the Storage section expand Local Storage. After that, you'll see all your browser's local storage there.

Does Chrome sync local storage?

sync , the stored data will automatically be synced to any Chrome browser that the user is logged into, provided the user has sync enabled. When Chrome is offline, Chrome stores the data locally. The next time the browser is online, Chrome syncs the data. Even if a user disables syncing, storage.

How do I get the current value in chrome storage?

chrome. storage. local. get (['key'], function (result) {console. log ('Value currently is ' + result. key);}); When using storage.sync, the stored data will automatically be synced to any Chrome browser that the user is logged into, provided the user has sync enabled. When Chrome is offline, Chrome stores the data locally.

How to check storage space on Chromebook?

The easiest way to check storage space on Chromebook is using the Files app. Once you open the Files app, click on the 3-dot menu on the top-right corner, and it will display the remaining storage space on your Chromebook at the very bottom. 2.

How to view local storage in Chrome extensions?

Local storage is not in the cloud. Here is how to view the data: First, go the extension page of Chrome. For the extension that you want to examine, open its background page. Each extension has a "Inspect views: background page" link for this.

How do I see multiple variables in Chrome DevTools?

Chrome DevTools allows you to easily see multiple variables throughout your application. Watching variables within Sources keeps you out of the console and focused on improving your code. The Sources panel provides the ability to watch variables within your application. This is located in the watch section of the debugger sidebar.


2 Answers

The function expects a callback:

chrome.storage.local.get("narration", function(data)
{
    if(chrome.runtime.lastError)
    {
        /* error */

        return;
    }

    var bookNarration = parseInt(data.narration);

    switch(bookNarration)
    {
        /* ... */
    };
});
like image 149
James McLaughlin Avatar answered Nov 11 '22 11:11

James McLaughlin


There's no need to use the catch now (meaning I don't know if this changed since the accepted answer and now).

You can pass in the item(s) you want along with defaults should the item not exist

chrome.storage.local.get({narration: "", num: 0, books: []}, function(data)
{
    var bookNarration = parseInt(data.narration);

    switch(bookNarration)
    {
        var numBooks= data.books.length
        /* ... */
    };
});
like image 31
Troy Wray Avatar answered Nov 11 '22 12:11

Troy Wray