2

I have to use localStorage variable of browser in extension .js code of crossrider browser extension how to access localStorage variable inside crossrider extenson code like i want to use localStorae.setItem("foo","demo") how to access foo varaible in extension code

1 Answers1

6

If I understand you correctly, you can can simply assign the value returned by localStorage to a variable, as follows:

In the extension.js file:

appAPI.ready(function($) {
    var dataFromLocalStaorage = localStorage.getItem("foo");
    console.log('Value is ' + dataFromLocalStaorage);
});

However, we recommend that you use the following Crossrider APIs to work with local storage: appAPI.db for working synchronously with Crossrider's local database implementation, appAPI.db.async for working asynchronous with Crossrider's local database implementation. This works for all browsers supported by Crossrider.

So, for example, you can save and retrieve data from the local database and use them within your extension code, as follows:

In the extension.js file:

appAPI.ready(function($) {
    // Extension variable
    var dataToSaveToLocalDB = {scriptName: "Hello World", scriptType: "JS"};

    // Save variable to local database
    appAPI.db.set('myData', dataToSaveToLocalDB);

    // Retrieve variable from the local database
    var dataRetrievedFromLocalDB = appAPI.db.get('myData');

    // Use the variable in the extension
    console.log('Script name: ', dataRetrievedFromLocalDB.scriptName);
});
Shlomo
  • 3,763
  • 11
  • 16