Chrome扩展程序本地存储:Background.html是否无权访问本地存储? [英] Chrome Extension Local Storage: Background.html doesn't have access to localstorage?

查看:171
本文介绍了Chrome扩展程序本地存储:Background.html是否无权访问本地存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的弹出窗口和选项卡中,从LocalStorage进行写入和读取工作正常.但是,当我尝试从后台页面添加值时,它似乎根本没有写入.我正在通过刷新并寻找要显示的值来查看Chrome开发者工具中的本地存储.

Writing and reading from LocalStorage is working fine from my popup and tab. However, when I attempt to add a value from my background page, it doesn't seem to write at all. I'm viewing local storage in Chrome Developer Tools by refreshing and looking for the value to show.

在以下示例中,添加新书签后,background.html'lastId'的代码会正确显示在警报中.但是,该值不存储.此外,对已知值的请求似乎失败,没有警报显示. (尝试下面显示的两种语法的结果相同.)

In the following example code for background.html 'lastId' is displayed correctly in the alert when a new bookmark is added. However, the value is not stored. Additionally, the request for a known value appears to fail with no alert displaying. (Results are the same attempting both syntaxes shown below.)

<html>
<script>
// Grab the id of newly created bookmarks
chrome.bookmarks.onCreated.addListener(function(id) {
    var lastId = id;
    alert(lastId);
    localStorage['lastId'] = lastId;
            var testvalue = localStorage['309'];
    alert(testvalue);
            localStorage.setItem('lastId', lastId);
    var testvalue2 = localStorage.getItem('309');
    alert(testvalue2);
});
</script>
</html>

我一直认为我必须只是缺少一些小的语法问题或其他东西,但是什么也看不到.如果我的清单声明不正确,我认为该警报不适用于该ID.陷入困境...

I keep thinking I must just be missing some small syntax issue or something but can't see anything. If my manifest declaration was incorrect I don't think the alert would work for the id. Stumped...

更新:事实证明,您必须在后台页面的更新上强制重新加载扩展名,因为它们在打开时会持久保存在浏览器的内存中.这就是为什么我保存的代码似乎无法正常工作的原因.它没有刷新,我感到很尴尬.

UPDATE: Turns out that you have to force reload of the extension on updates to background pages since they are persistent in browser memory when opened. That is why my saved code appeared not to work. It wasn't refreshed and I am duly embarrassed.

推荐答案

嗯,我可以考虑几件事.

Hm, I can think about couple things.

您说它可以在选项卡和弹出窗口中使用.这是很奇怪的,因为它不应该这样做(如果使用选项卡,则表示内容脚本).内容脚本只能访问属于它们注入的站点的localStorage.扩展程序文件夹中的弹出窗口,背景页面,选项页面以及任何其他页面都只能访问扩展程序自己的localStorage.那两个本地存储又完全分开了.因此,也许您正在检查错误的localStorage?

You say that it works in a tab and a popup. This is very strange because it shouldn't (if by tab you mean content script). Content scripts are able to access only localStorage that belongs to a site they are injected. Popup, background, option pages, and any othe page from extension's folder can only access extension's own localStorage. Those two local storages and completely separated. So maybe you are inspecting wrong localStorage?

要查看扩展程序自己的localStorage,您需要检查背景或弹出页面,并检查检查器中的资源"选项卡.要检查站点或内容脚本localStorage,您需要在页面上打开常规检查器.

To see extension's own localStorage you need to inspect background or popup page and check resources tab in the inspector. To inspect site or content script localStorage you need to open regular inspector on the page.

第二个时刻是您的localStorage分配可能与预期不符.

Second moment is your localStorage assignment might be not what you are expecting.

如果您运行:

var lastId = 5;
localStorage['lastId'] = lastId;

您将获得分配给lastId属性的值5.因此,要读取书面价值,您需要运行:

you will get value 5 assigned to lastId property. So to read written value you need to run:

alert(localStorage['lastId']); //not localStorage['5']

如果要存储数组,则需要通过JSON序列化/反序列化它们,因为localStorage只能存储字符串.

If you want to store arrays then you would need to serialize/unserialize them through JSON as localStorage can store only strings.

这篇关于Chrome扩展程序本地存储:Background.html是否无权访问本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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