admin管理员组

文章数量:1296392

I need to store a users profile and his preferences in a localStorage object \ cookies in a way that they'll be accessible(readable) and writable from both the web-app, and the chrome extension (that are basically part of the same product).

I found this cool library and this article that specifies how to use it.

The problem is that xauth is down, and so is the server page that is required to use the library.

Any alternatives

I need to store a users profile and his preferences in a localStorage object \ cookies in a way that they'll be accessible(readable) and writable from both the web-app, and the chrome extension (that are basically part of the same product).

I found this cool library and this article that specifies how to use it.

The problem is that xauth is down, and so is the server page that is required to use the library.

Any alternatives

Share Improve this question edited Jul 9, 2014 at 20:51 Charles 51.4k13 gold badges106 silver badges144 bronze badges asked Jul 9, 2014 at 10:58 Oleg BelousovOleg Belousov 10.1k14 gold badges74 silver badges128 bronze badges 7
  • Err. Context scripts share access to domain's localStorage. – Xan Commented Jul 9, 2014 at 11:00
  • the domain is chrome-extension://{id} thus not accessible from the web, or am I missing something? – Oleg Belousov Commented Jul 9, 2014 at 11:01
  • Note: content scripts. If you inject a script into the page in question, localStorage will be shared. – Xan Commented Jul 9, 2014 at 11:02
  • So, basically you are suggesting to add the web-app's domain to the list of matching domains, and then the chrome.localStorage object will be available from the web, even on non-chrome browsers?, doesn't sound rational – Oleg Belousov Commented Jul 9, 2014 at 11:06
  • What's chrome.localStorage? Are you confusing things with chrome.storage.local? – Xan Commented Jul 9, 2014 at 11:07
 |  Show 2 more ments

2 Answers 2

Reset to default 9

You can use both localStorage and cookies.

  1. If you inject a content script in the web app's page, its localStorage is shared with domain's own storage. You can then municate with your background script to pass information.

  2. If you include "cookies" permission in your manifest, you can manipulate cookies using chrome.cookies API.

Edit: You can also make your extension externally connectable from the web app to maintain synchronization of the changes.

Update 2016

This cross-storage from zendesk is a great alternative to the 2 librarys that you mention

Even better you can config which client site/domain has a specific permission (either to get, set, delete)

Sample codes:

Hub

// Config s.t. subdomains can get, but only the root domain can set and del
CrossStorageHub.init([
  {origin: /\.example.$/,            allow: ['get']},
  {origin: /:\/\/(www\.)?example.$/, allow: ['get', 'set', 'del']}
]);

Note the $ for matching the end of the string. The RegExps in the above example will match origins such as valid.example., but not invalid.example..malicious..

Client

var storage = new CrossStorageClient('https://store.example./hub.html');

storage.onConnect().then(function() {
  return storage.set('newKey', 'foobar');
}).then(function() {
  return storage.get('existingKey', 'newKey');
}).then(function(res) {
  console.log(res.length); // 2
}).catch(function(err) {
  // Handle error
});

本文标签: javascriptSharing a local storage object across a webapp and a Chrome extensionStack Overflow