开发者

Is it possible to access localStorage from manifest.json?

I'm sur开发者_运维问答e 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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜