localStorage different for webpage and chrome exte

2019-02-20 05:04发布

问题:

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' 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).

Documentation on localStorage from MDN: here.