问题描述
我试图跟踪我的google chrome扩展程序的标签缩略图,并希望能够将它们保存到我的本地存储中。目前,我有以下几点:
: localStorage 限制为5MB。考虑使用异步用于保存数据的API。I'm trying to keep track of tab thumbnails for my google chrome extension, and would love for the ability to save them to my local storage. Currently, I have something along the lines of:
chrome.tabs.captureVisibleTab(tab.windowId, function(thumb) { // other code here... }How can I save thumb to local storage? Or should I not be saving thumb to local storage and instead reloading those thumbnails the next time the browser loads?
解决方案The callback of chrome.tabs.captureVisibleTab receives a data-URI (data:image/png;base64,... or data:image/jpg;base64,...). This is a plain string, which can be saved in localStorage as follows:
chrome.tabs.captureVisibleTab(tab.windowId, function(thumb) { // Example: Save by key URL localStorage.setItem(tab.url, thumb); }); // <-- Don't forget the closing parenthesis..In this example, the screenshot was saved in the same key as the tab's URI using localStorage.setItem.
You can enumerate through the keys as follows:for (var i=0; i<localStorage.length; i++) { var keyname = localStorage[i]; // Or localStorage.key(0); var thumb = localStorage.getItem(keyname);// <-- Retrieve the value }If you don't like the thumb, it can be removed using the localStorage.removeItem method:
var keyname = 'https://stackoverflow.com/'; // For example localStorage.removeItem(keyname);Note: localStorage is limited to 5MB. Consider using the asynchronous chrome.storage API for persisting data.
这篇关于如何将标签缩略图保存到本地存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!