1

I can't get my Chrome extension to save data correctly.

Here is my current code

function getQueryVariable(variable) { //Split URL by variables var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i<vars.length;i++) {	var pair = vars[i].split("="); if(pair[0] == variable){return pair[1];} } return(false); } function saveTicket2(ticket, contents) { //Save the data to local storage	chrome.storage.local.set({ticket : contents}, function() {	console.log('Saved', ticket, contents);	}); } function setData(ticketID, result) { //If data exists. Set it.	document.getElementsByName('message')[0].value = result[ticketID];	document.getElementsByName('message')[1].value = result[ticketID];	console.log(result); } //Grab Ticket ID var ticketID = getQueryVariable("id"); //Retrieve data if it exists try {	chrome.storage.local.get(ticketID, function(result) {	if (result) {	setData(ticketID, result);	}	}); } catch(err) {	console.log(err.message); } //Save ticket contents to local storage every 5 seconds window.setInterval(function() {	var contents= document.getElementsByName('message')[0].value;	var contents= document.getElementsByName('message')[1].value;	saveTicket2(ticketID, contents); }, 5000);

Within saveTicket2 the log works fine

Saved 222222 text 

The log within setData logs an empty object

So the chrome.storage.local.set seems to pass without error but it doesn't actually save anything.

From Chrome Storage Developer Page the example given is as follows

chrome.storage.sync.set({'value': theValue}, function() { // Notify that we saved. message('Settings saved'); }); 

Which is what I have. The storage permission is also in my manifest.

If I add the following to saveTicket2, right after the .set it also returns an empty object, confirming I'm either accessing it wrong or saving it wrong.

chrome.storage.local.get(ticket, function(result1) {	console.log(result1);	});

Does anyone know why this isn't saving?

1
  • BTW, code snippets don't make sense for chrome extensions because the snippet can't run in the browser. Commented Sep 21, 2015 at 3:14

1 Answer 1

1

In your code:

chrome.storage.local.set({ticket : contents}, function() { console.log('Saved', ticket, contents); }); 

The value of ticket hasn't set successfully.

It will work in this way:

function saveTicket2(ticket, contents) { //Save the data to local storage var dataObj = {}; dataObj[ticketID] = contents; chrome.storage.local.set(dataObj, function(){ if(!chrome.runtime.lastError){ console.log('Saved', ticket, contents); } }); } 

This is my test code:

function saveTicket2(ticket, contents) { //Save the data to local storage var dataObj = {}; dataObj[ticketID] = contents; chrome.storage.local.set(dataObj, function(){ if(!chrome.runtime.lastError){ console.log('Saved', ticket, contents); } });} function setData(ticketID, result) { //If data exists. Set it. console.log("setData: ", result); } //Grab Ticket ID var ticketID = "55555"; //Retrieve data if it exists function getData(){ try{ chrome.storage.local.get(ticketID, function(result) { if (result){ console.log(result); setData(ticketID, result[ticketID]); } }); }catch(err) { console.log(err.message); } } //Save ticket contents to local storage every 5 seconds window.setInterval(function() { var contents= new Date().getTime(); saveTicket2(ticketID, contents); }, 3000); window.setInterval(function() { getData(); }, 5000); 

If you want to know more about usage of chrome.storage, pls go to How to set a simple dynamic variable into chrome.storage.local.set for a chrome extension?

Sign up to request clarification or add additional context in comments.

1 Comment

can some one tell me how to push the data into an array and save this array to be able to add the new values to this array ........................

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.