[英]Single data store offline locally for both a web site and an extension in Chrome?
嗨,我目前正在開發一個網站和一個Chrome擴展程序。
當前,它們每個都在本地離線維護一系列對象。 該網站使用localStorage ,而擴展使用chrome.storage 。
現在,我想在2個數組之間進行同步。 它帶有一些解決方案:
1)在服務器上建立一個端點來獲取/發布數組
2)編寫代碼以在2個數組之間進行同步,即,每次更改1個數組時,我們會將更改填充到其他數組中,反之亦然。
3)僅在本地離線存儲該陣列的單個實例。
=>我正在嘗試實施此解決方案。
因此,是否有任何方法可以在本地離線存儲對象,並且可以在網站和Chrome擴展程序之間共享該對象?
如果將內容腳本與本地存儲一起使用,則會出現問題:
當前用例:
好吧,我想我找到了以下解決方案。 使用跨存儲庫
它由一個中心和許多客戶組成:
集線器:充當服務器,實際上與localStorage API交互
客戶:創建一個iframe以將文件加載到集線器上並發布消息以訪問集線器中的數據(獲取,設置,刪除)
樣例代碼:
Hub
// Config s.t. subdomains can get, but only the root domain can set and del
CrossStorageHub.init([
{origin: /\.example.com$/, allow: ['get']},
{origin: /:\/\/(www\.)?example.com$/, allow: ['get', 'set', 'del']}
]);
請注意,$用於匹配字符串的結尾。 上例中的RegExps將匹配源,例如valid.example.com,但不會匹配invalid.example.com.malicious.com。
Client
var storage = new CrossStorageClient('https://store.example.com/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
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.