Chrome extension store large amounts of data

馋奶兔 提交于 2019-11-27 08:11:12

问题


I'm looking for an efficient way to store large amounts of data in my chrome extension. I've got a few txt files which are around 1-2mb. I'd like my chrome extension to 'cache' them locally so I don't need to fetch them every time. I've found syncFileSystem but this is only available for packed apps.

There were warnings when trying to install this extension:

'syncFileSystem' is only allowed for packaged apps, but this is a extension.

What is the best way to store this sort of data in a chrome extension?

manifest.json

{
    "manifest_version": 2,
    "name": "__MSG_name__",
    "version": "1.0",
    "default_locale": "en",
    "description": "__MSG_description__",
    "icons" : {
        "16" : "img/logo_enabled_16.png",
        "48": "img/logo_enabled_48.png",
        "128": "img/logo_enabled_128.png"
    },
    "browser_action": {
        "default_icon": "img/logo_enabled_48.png",
        "default_title": "__MSG_browser_action_title__",
        "default_popup":"options.html"
    },
    "background": {
        "scripts": [
            "js/chrome.js",
            "js/filter.js",
            "js/background.js"
        ],
        "persistent": true
    },
    "content_scripts": [{
        "matches": [
            "http://*/*",
            "https://*/*"
        ],
        "js": [
            "js/docReady.js",
            "js/content.js"
        ]
    }],
    "offline_enabled":true,
    "options_ui": {
        "chrome_style": true,
        "page":"options.html"
    },
    "permissions": [
        "activeTab",
        "tabs",
        "webRequest",
        "webRequestBlocking",
        "webNavigation",
        "storage",
        "syncFileSystem",
        "http://*/*",
        "https://*/*"
    ],
    "short_name": "__MSG_shortName",
    "minimum_chrome_version":"45.0.2454.101",
    "web_accessible_resources":[
        "css/bootstrap.min.css",
        "js/jquery.min.js",
        "js/chrome.js",
        "js/bootstrap.min.js"
    ]
}

回答1:


Only WebSQL, IndexedDB, chrome.storage.local and HTML5 File System (sandboxed file system) can grow past 5MB limit via "unlimitedStorage" permission.

manifest.json: "permissions": ["unlimitedStorage"]

Provides an unlimited quota for storing HTML5 client-side data, such as databases and local storage files. Without this permission, the extension or app is limited to 5 MB of local storage.

Notes:

  • WebSQL is deprecated by W3C in favor of the slower IndexedDB but I think it will stay in Chrome for the obvious reason that it's faster and more flexible due to being SQL-based.
  • chrome.storage.local is the easiest to use but it may not be the fastest with the large objects, do some tests if speed is important.
  • Use a Zip/LZMA Javascript library to compress/decompress the text files if the gain is significant.


来源:https://stackoverflow.com/questions/34060487/chrome-extension-store-large-amounts-of-data

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