HTML5 sessionStorage or chrome.storage for Chrome Extension?

余生颓废 提交于 2019-12-08 08:01:39

问题


I have a Chrome Extension that is only a content script. I want to persist some data that is calculated in the content script so I can easily access it while browsing without having to recalculate it on each page. I only need the data to be stored for the session.

I was looking at the chrome.storage API but it seems data will persist there past the session. I have previous experience using HTML5 sessionStorage, but I feel like I should be leveraging Google's API here.

Any input is appreciated, thanks!


回答1:


Inside a content script, using sessionStorage will access and modify the sessionStorage of that site, not of your extension.

You must use chrome.storage.local if you want it to be available to content scripts on other sites and to avoid breaking the sites.

There is no automatic clearing of chrome.storage.local data, but you can create an event page that clears it on startup.

manifest.json:

"background": { "scripts": [ "background.js" ], "persistent": false }

background.js:

chrome.runtime.onStartup.addListener(function() {
 chrome.storage.local.clear()
})

chrome.storage.local.clear

chrome.runtime.onStartup

Event Pages



来源:https://stackoverflow.com/questions/39501435/html5-sessionstorage-or-chrome-storage-for-chrome-extension

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