localStorage与网页和Chrome扩展程序不同 [英] localStorage different for webpage and chrome extension

查看:215
本文介绍了localStorage与网页和Chrome扩展程序不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Chrome扩展程序的 background.js 文件中访问 localStorage 对象。常规网页上的 localStorage 对象显示来自不同网页(包括我感兴趣的网页)的内容,但是我从后台访问的内容.js popup.js 为空。如何从我的Chrome扩展程序访问常规 localStorage (包含其他网页设置的项目)?

I'm trying to access the localStorage object from my chrome extension's background.js file. The localStorage object on a regular webpage shows me items from different webpages (including the ones I'm interested in), but the ones I access from background.js or popup.js are empty. How do I access the regular localStorage (with items set by another webpage) from my Chrome extension?

推荐答案

localStorage 对象是相对于本地环境的,这意味着 localStorage http://www.google.com 上的对象与<$ c $上的对象 完全不同 c> http://stackoverflow.com ,显然与您分机的后台页面完全不同。

The localStorage object is relative to the "local environment", this means that the localStorage object on http://www.google.com is totally different from the one on http://stackoverflow.com, and obviously is totally different from the one in the background page of your extension.

鉴于此,如果您想在扩展程序中存储内容,则需要在后台页面中使用 localStorage 对象,而不是在页面中您正在注入内容脚本的某个站点。

Given that, if you want to store something in your extension, you'll need to use the localStorage object in your background page, not in the page of some site you are injecting content scripts to.

如果您想访问网页' localStorage s,那么您必须在其上发送内容脚本,检索 localStorage 对象,并将其发送到 background.js 带有消息的脚本(参见 chrome扩展消息传递

If you want to access web pages' localStorages, then you'll have to send a content script on it, retrieve the localStorage object, and send it to the background.js script with a message (see chrome extensions message passing).

localStorage上的文档 此处

Documentation on localStorage from MDN: here.

这篇关于localStorage与网页和Chrome扩展程序不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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