Chrome扩展程序:无法取回我存储的数据 [英] Chrome extensions : cannot get back my stored datas

查看:111
本文介绍了Chrome扩展程序:无法取回我存储的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试保存我的Chrome扩展程序中的数据:

I try to save datas from my chrome extension :

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
      chrome.storage.sync.set(request, function() {
        chrome.storage.sync.get(null, function(datas) {
            console.log(datas);
            console.log("background");
            sendResponse(datas);
        })
      });
    }
 );

每次收到消息时,我都会存储此消息,然后将所有保存的项目作为响应发送出去.

Each time I'm receiving a message, I store this message, and I send all saved item as a response.

在此代码中,控制台中显示background,但数据为空(控制台中无内容)

In this code, in the console, background is displayed but datas is empty (Nothing in the console)

当我console.log(request)我的json对象正常时.

When I console.log(request) my json objet is ok.

在此代码中,为什么数据为空?

In this code, why datas is empty ?

谢谢

推荐答案

在存储/检索数据时,您应该提供/请求密钥:

You should provide/request a key when you store/retreive your data:

chrome.storage.sync.set({
   MyRequest: request
}, function(){
   chrome.storage.sync.get{
       MyRequest: null
   }, function(data){
      console.log(data.MyRequest);
   }
});

还要确保您具有清单文件中声明的存储"权限:

Also make sure you have permission "storage" declared in your manifest file:

"permissions": ["storage"]

这篇关于Chrome扩展程序:无法取回我存储的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆