简体   繁体   中英

Is it possible to access localStorage from manifest.json?

I'm sure this is possible, I just can't seem to figure it out.

I'm building a Chrome Extension, and I have set an API URL, which is user-specific, in localStorage["url"]. However, I need to add it to the permissions in my manifest.json file. It seems that I don't have access to localStorage in manifest, though.

Is there a way to do this?

No

simply because the manifest in a Chrome extension is pure JSON , meaning it's only parsed as text and cannot contain other data types or literals that aren't allowed by the strict JSON specifications. So you cannot include functions, and neither can you have one that would be called to return a value.

You cannot even enter comments either (actually, it seems like you can have comments now, but it wasn't the case earlier).

A way for you to achieve your goal would be too provide a web service that generates the extension on demand. It would populate a manifest.json on a per-user basis, and zip and sign your extension. But then it wouldn't be installable with the Chrome Web Store and you'd need to host it yourself.

If you worry about cross origin permissions , then all that matters in your API URL is a domain.

If they have a common domain, then just declare it in permissions. If there is a set of domains, you can declare a comma separated list of domains. If domains are unique for each user (hard to imagine), then you don't have other choice than to declare full range http://*/ , which is not the end of the world.

I'm pretty sure it's not possible; you'll have to set your API URL in a different file than manifest.json and ask for permissions manually. The reason this is done is to avoid permissions-hijacking: if your localStorage is compromised, then the extension could theoretically be wired to make requests wherever it wanted without the user's intervention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM