0

我正在编写一个 Chrome 扩展程序,我想从 Devtools 页面访问 Chrome 存储 API。chrome.storage.local 变量(带有 get/set 函数)是可用的,但是当我调用它们时,回调永远不会被调用。当我使用我的后台页面访问存储 API 时,它按预期工作。

当然,我可以通过后台页面发送请求并通过消息将其传递到 Devtools 页面,但我正在寻找一种更清洁的方法。难道我做错了什么?我希望你理解我的问题,为了说明我的问题,我创建了一个我正在处理的小样本,可以在这里下载;http://g2f.nl/0w2kko9

它包括以下测试,用于验证 API 是否可用但不能按预期工作;

document.getElementById("p").innerHTML += "chrome.storage.local.set = " + !!chrome.storage.local.set
+ "<br>chrome.storage.local.get = " + !!chrome.storage.local.get;

chrome.storage.local.set( {'foo' : 'bar'} , function () {
    document.getElementById("p").innerHTML += '<br>foo = bar';
});

setTimeout(function () {
    document.getElementById("p").innerHTML += '<br>That was one second';
    chrome.storage.local.get('foo', function (result) {
        document.getElementById("p").innerHTML += '<br>foo = ' + result;
    });
}, 1000);
4

0 回答 0