Update object stored in chrome extension's local storage

非 Y 不嫁゛ 提交于 2019-11-28 14:41:20

Extensions can use a database: IndexedDB (the sample code may look convoluted, but it's pretty simple in the actual extensions, for example two small functions here, getStyles and saveStyle, or IDB-keyval wrapper library).

If you want to use chrome.storage, just maintain a global queue array that is populated by the server listener:

queue.push(newItem);
updateStorage();

and processed in chrome.storage.local.get callback:

function updateStorage() {
    if (!queue.length) {
        return;
    }
    chrome.storage.local.get('commands', function(data) {
        data.commands = [].concat(data.commands || [], queue);
        queue = [];
        chrome.storage.local.set(data);
    });
}

As Javascript engine is single-threaded, there's no need to worry queue might be changed while chrome.storage.local.get runs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!