最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you retrieve this value from chrome.storage? - Stack Overflow

programmeradmin0浏览0评论

I'm a real noob to Javascript/JSON, so this might be really obvious. I'm storing values to chrome.storage in a chrome extension JS file:

chrome.storage.sync.set({'username' : username}, function() {
    console.log('Saved',username);
}); 
chrome.storage.sync.set({'password' : password}, function() {
    console.log('Saved', username);
});

I know it's passed correctly because the console.log returns the right values.

Then I try to retrieve it in my content script, but it only returns [object Object].

chrome.storage.sync.get("username", function (username) {
    console.log("Passed successfully: Username "+username);
    studentUsername = username;
});

chrome.storage.sync.get('password', function (password) {
    console.log("Passed successfully: Password "+password);
    studentUsername = username;
});

I'm not sure why it's doing this.

I'm a real noob to Javascript/JSON, so this might be really obvious. I'm storing values to chrome.storage in a chrome extension JS file:

chrome.storage.sync.set({'username' : username}, function() {
    console.log('Saved',username);
}); 
chrome.storage.sync.set({'password' : password}, function() {
    console.log('Saved', username);
});

I know it's passed correctly because the console.log returns the right values.

Then I try to retrieve it in my content script, but it only returns [object Object].

chrome.storage.sync.get("username", function (username) {
    console.log("Passed successfully: Username "+username);
    studentUsername = username;
});

chrome.storage.sync.get('password', function (password) {
    console.log("Passed successfully: Password "+password);
    studentUsername = username;
});

I'm not sure why it's doing this.

Share Improve this question edited Nov 26, 2015 at 1:21 Teepeemm 4,5185 gold badges37 silver badges60 bronze badges asked Nov 25, 2015 at 23:53 The-ArkanianThe-Arkanian 612 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Yes, it always returns an object. You'll have to access the property from the object.

chrome.storage.sync.get("username", function (obj) {  
    console.log("Passed successfully: Username "+obj.username)
    studentUsername = obj.username; 
});
发布评论

评论列表(0)

  1. 暂无评论