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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
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.
If you want to access web pages' localStorage
s, 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).
Documentation on localStorage
from MDN: here.