适用于Chrome扩展程序的HTML5 sessionStorage或chrome.storage? [英] HTML5 sessionStorage or chrome.storage for Chrome Extension?

查看:230
本文介绍了适用于Chrome扩展程序的HTML5 sessionStorage或chrome.storage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Chrome扩展程序,它只是一个内容脚本。我想保留内容脚本中计算的一些数据,以便在浏览时轻松访问它,而无需在每个页面上重新计算。我只需要为会话存储数据。



我在查看 chrome.storage API ,但看起来数据会在会话结束后持续存在。我有使用HTML5 sessionStorage的经验,但我觉得我应该在这里利用Google的API。



感谢您的任何输入!谢谢!


在内容脚本中,使用sessionStorage将访问和修改该网站的sessionStorage,而不是您的扩展。


$ b $

如果您希望它可用于其他网站上的内容脚本并避免破坏网站,则必须使用chrome.storage.local。



没有自动清除chrome.storage.local数据,但您可以创建一个在启动时清除它的事件页面。



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



活动页面


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!

解决方案

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

这篇关于适用于Chrome扩展程序的HTML5 sessionStorage或chrome.storage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆