开发者

Google Chrome - Is it possible to include a SQLite db within the extension

Is it possible to include a DB file pa开发者_StackOverflow中文版cked in with the extension when you download it. I'm trying to include a small DB with zip codes so the extension does not have to request a look up for the proper zip code.

possible at all?


Basically what I would do in a Chrome Extension, is packaging it up with a "zip_codes.json" file. Then when your extension loads, use XHR to read that file. For example, the below snippet is to asynchronously (you can use synchronous too if you want) get the zip codes stored in your extension.

var zipcodes = {};
var xhr = new XMLHttpRequest();
xhr.open('GET', chrome.extension.getURL('zip_codes.json'), false);
xhr.onreadystatechange = function() {
  zip_codes = JSON.parse(xhr.responseText);
  console.log(zip_codes);
}

I would believe this approach would be easier to manage than adding your own localStorage cache for the files, because once you ship it, you can always update that file.

So if your zip_codes.json file has the following:

{
  33445: 'Some zip'
}

You can just access that zip code using the following approach:

console.log(zip_codes[33445]);

Hope that helped!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜