0

Hello I am currently building a chrome extension that automates a website and on a html page I save the users checkout data using localStorage.

I then realized that you cant call local storage in a content_script so what I did was this in the html page where I set and get the local storage.

this is what I use to save the users checkout info in the checkout html page for him to visually see and change when they want to:

var autofill = localStorage.getItem("checkout-info");
var filler = autofill.split(",");
$("#name").val(filler[0]);
$("#email").val(filler[1]);
$("#tel").val(filler[2]);

chrome.storage.sync.set({'autofiller': autofill}, function() {
});

the .val split is how I keep the data inside the inputs so the user can see it.

the chrome.storage is how I then take the data and call it later in the content_scripts file:

chrome.storage.sync.get(['autofiller'], function() {
});

checker = autofiller.split(",");


alert(checker[1], checker[2]);

and for some reason every time the alert part runs no matter what number it is it always alerts all the data not split with the commas.

Which is weird because the split works perfectly in the other file where I use localStorage.

I have also editet the file and tried this aswell:

chrome.storage.sync.set({
    'info0': (filler[0])
    'info1': (filler[1])
    'info2': (filler[2])
    'info3': (filler[3])
    'info4': (filler[4])
    'info5': (filler[5])
    'info6': (filler[6])
    'info7': (filler[7])
    'info8': (filler[8])
    'info9': (filler[9])
    'info10': (filler[10])
    'info11': (filler[11])
    'info12': (filler[12])}, function() {
});

then in the content_scripts file tried this:

chrome.storage.sync.get(['info0', 'info1', 'info2', 'info3','info4','info5','info6','info7', 
    'info8', 'info9', 'info10', 'info11', 'info12'], function() {
});


alert(info0);

I also tried doing the set method without the () between the fillers and it also did not work. Can anyone help me Please?

Any advice on why the split isn't working?

  • @PredatorIWD I don't understand how that has anything to do with my question? –  Aug 26 '18 at 05:54
  • Where does the `autofiller` variable comes from? Please provide the content of the callback function you passed to the `chrome.storage.sync.get` function. This is where you should have set the `autofiller` variable – rd3n Aug 26 '18 at 18:20
  • that is what I tried before. I tried to save the local storage split and set it in the chrome storage sync but it did not work @rd3n –  Aug 26 '18 at 20:52

1 Answers1

0

There is a big difference between localStorage and chrome.sync: the first one is synchronous, and chrome.sync is asynchronous, which means you have to use a callback function to work with retrieved data.

It is a pretty rookie question. Please, check the answers to this question: How do I return the response from an asynchronous call?

In specifically to your case, all processing of a data should be inside the callback function:

chrome.storage.sync.get(['autofiller'], function (result) {
    const checker = result.autofiller.split(',');
    alert(checker[1], checker[2]);
});
Deliaz
  • 2,697
  • 1
  • 19
  • 32
  • this is what I got when I ran the script: Error in response to storage.get: TypeError: result.autofiller.split is not a function –  Aug 27 '18 at 18:50
  • Make sure that "autofiller" is defined and it is a string. – Deliaz Aug 28 '18 at 05:01