Chrome扩展程序 - 不保存数据

时间:2015-09-20 21:11:08

标签: javascript google-chrome google-chrome-extension

我无法通过Chrome扩展程序正确保存数据。

这是我目前的代码

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);

在saveTicket2中,日志工作正常

Saved 222222 text

setData中的日志记录一个空对象

所以chrome.storage.local.set似乎没有错误地传递,但实际上并没有保存任何内容。

Chrome Storage Developer Page,给出的例子如下

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

这就是我所拥有的。存储权限也在我的清单中。

如果我将以下内容添加到saveTicket2,就在.set之后,它也会返回一个空对象,确认我输入错误或保存错误。

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

有谁知道为什么不保存?

1 个答案:

答案 0 :(得分:0)

在您的代码中:

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

故障单的值未成功设置。

它将以这种方式工作:

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 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);

如果您想了解有关chrome.storage使用情况的更多信息,请转到How to set a simple dynamic variable into chrome.storage.local.set for a chrome extension?