Utilize Chrome storage instead of localstorage to generate Parse sessions

I'm currently developing a Chrome Extension that relies on Parse User sessions. Because localstorage is limited to specific domains, I am looking to utilize chrome.storage so the data can be accessed across any site.

The existing Parse Javascript SDK uses localstorage for storing user objects and creating sessions. How can I modify this behavior to switch to using chrome.storage instead?

Here is my current approach:

on login
   store session token in chrome.storage

Then, during site visits:

if chrome.storage.sessiontoken
    create Parse session with the stored session token from chrome.storage

I'm trying to find a simpler solution, so if anyone has any suggestions, I'd appreciate it!

I wish I could just do: window.localstorage = chrome.storage

But I suspect that may cause issues. Has anyone found a way around this problem?

Answer №1

To effectively store data in a Chrome app using Parse, you'll need to create a custom storage controller utilizing the chrome API. This allows for a more efficient and non-blocking storage solution compared to the default window.localStorage API used by Parse.

Fortunately, Parse SDK is designed to support various types of storage (both synchronous and asynchronous) and enables users to set their own storage controller. Before initializing Parse, make sure to call

Parse.CoreManager.setStorageController(YOUR_STORAGE_CONTROLLER)
.

For example, you can refer to the Parse React Native storage controller on GitHub, which demonstrates an asynchronous storage approach:

  • A Sync controller object should have its async property set to 0 with functions like getItem, setItem,removeItem, and clear
  • An Async controller object should have its async property set to 1 with functions like
    getItemAsync, setItemAsync, removeItemAsync,
    and clear

Simply follow the example provided by Parse React Native to build your own custom storage controller using the chrome.storage API. This will enable Parse to utilize your controller instead of relying on localStorage.

For the original discussion on Github, visit: https://github.com/ParsePlatform/Parse-SDK-JS/issues/72

Answer №2

Substituting localstorage with chrome.storage directly is not possible due to their synchronous and asynchronous nature, as well as differences in API methods.

While it's not feasible to make the switch fully synchronous, there are some potential solutions:

  1. Handle storage operations in the background script where the domain for localStorage remains fixed.

  2. Create a local synchronous data copy of the storage like so:

    var localData = {};
    chrome.storage.local.get(null, function(data) {
      localData = data;
      doSomethingElse(); // This part is async
    })
    

    However, managing updates to this cache will pose challenges as you'll need to intercept writes and synchronize with chrome.storage, which may lead to asynchronous issues.

In essence, if a library utilizes localStorage internally, replacing it effectively without rewriting the library or utilizing the background context may prove difficult.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best approach for creating a brute-force solution for finding the smallest change algorithm

Looking for a solution to this algorithm challenge: Create a function that can determine the minimum amount of change that cannot be made with the given coins. function nonConstructibleChange(coins) { coins = coins.sort((a, b) => a - b); // O(nlogn) ...

An easy way to enable mobility for BootstrapDialog on mobile devices

Currently, I am utilizing the library available at https://github.com/nakupanda/bootstrap3-dialog in order to create a dialog box. However, my issue arises when viewing the dialog on mobile devices where it exceeds the screen size. On desktops, adjusting t ...

Steps to dynamically populate a datatable with JSON data by triggering a click event in jQuery

I am facing an issue with feeding JSON data into datatables when the search button is clicked. The JSON data structure is as follows: [ { "port_code":"BOM", "cont_details_id":"9", "price":"44.000", "cont_price":"500", "c ...

Utilizing SlickGrid and DataView for calculating totals efficiently without the need for grouping

I am attempting to utilize Slick Grid and DataView to calculate column totals similar to the example shown here: . However, I do not want to group my rows. Therefore, I attempted not passing a getter and formatter into the dataView.setGrouping(..) method. ...

Can a hyperlink exist without a specified href attribute?

One scenario could be when I receive this specific code from a third-party source <a class="cs_import">Add contacts from your Address Book</a> Curiously, the link "Add contacts from your Address Book" does not redirect to any page as expected ...

The div block containing the table is experiencing horizontal overlap as additional elements are inserted

When creating a table within the div section of the code, I am incorporating text using the DRAG and DROP feature with appropriate styling. The table adjusts in size when I resize my window, indicating that it is functioning correctly. However, as the num ...

I am looking to modify the ID of the select element nested within a td tag

This is the code snippet I am working with: <tr> <td class="demo"> <label>nemo#2 Gender</label> <select id="custG2" required="required"> <option>....</option> <option>M</option> ...

How can I successfully implement the Add To Calendar Button Package?

Recently, I decided to incorporate this particular component into my application. Although it seemed simple enough at first, I'm now faced with a puzzling TypeError originating from within the package. The react component I am constructing is as foll ...

How can I extract information from Firebase and set it in the this.props of a React component?

Hey there, I'm having some trouble with my Firebase setup. I need to transfer data from snapshot.val() into this.props.device, but when I try to do so within the this.firebaseRef.on() function, 'this' is showing up as 'null'. Any s ...

AngularJS application: the button requires two clicks to activate

In my attempt to develop a simple CRUD app using AngularJS, I encountered an issue. The initial step involves clicking a button that fetches data from a REST API and populates an array. This array is then iterated using ng-repeat to generate Bootstrap card ...

What is the best way to utilize my data with Charts.js for my specific situation?

I am utilizing Charts.js in my Angular project (not AngularJS) and I am trying to create a graphic representation with data from my database that shows the distribution between men and women. However, I am struggling to figure out how to loop through the d ...

Guide to Saving a List in Local Storage

I have successfully created a CRUD page where users can input text and it gets added to a list. Now, I am trying to save that list in localStorage but encountering an issue where the console log is showing an empty object. Any assistance on this matter wou ...

"Utilizing Javascript's Regex to selectively replace only the first character

Forgive me if this is a beginner question, but I'm really unsure about this. Is there a regex solution to replace a character that appears first in a string? For example: 12 13 14 15 51 41 31 21 All data where '1' is the first character s ...

What is the process for combining AngularJS filters?

I currently have a fragment object like this: [{id: 721, titulo: "Cotizador Gastos Médicos Bx+ 2019", descripcion: "Cotizador Gastos Médicos Bx+ Tarifas 2019", updateDate: "2020-03-25 14:30:22.0", idCategoria: "1", …}, {id: 88, titulo: "Cotizador GMM ...

I am looking to customize the color of my Material UI switch

I am having difficulty changing the color of my Material UI switch based on my preference. I have tried several ways, but have not achieved the desired outcome. const useStyles = makeStyles((theme) => ({ toggle: { '& .Mui-checked': ...

Ways to identify the moment jQuery datatable is initialized and populated with information

I am currently using the most recent version of jQuery datatables. I'm curious if there is a callback function available that triggers right after the data has been loaded and displayed in the table? While experimenting with a datatable in IE8, I hav ...

In what format is the parameter accepted by the .getDay() method?

Here's the plan: I need to extract information from an input element with type set as date. This data will then be stored in a .json file and later parsed when the program is initiated. Subsequently, I aim to utilize the date.getDay() function to dete ...

When a click event triggers, use the .get() method in jQuery to retrieve the content of a page and then update

I am currently facing an issue with a div class="contentBlock", which is acting as the container for updating content. <div class="contentBlock"></div> Unfortunately, the script I have written does not seem to be working properly :c $(docume ...

The jQuery function appears to be running multiple times in a loop

For some reason, every value in my JSON object is getting added to the "listOfCountries" array twice. It seems like there might be a loop going through the result object more than once. I could really use some assistance with this issue! var listOfCountri ...

Transferring data from jQuery Ajax to PHP

I'm facing a challenge in retrieving a value back to PHP that I can manipulate and save to the database. It appears that using the GET method with jQuery AJAX is not yielding the desired results. Below is the PHP code snippet where I attempt to captur ...