问题
I'm building an offline HTML5 app (it will be delivered as a zipped up .crx
file). It will be installed and used entirely offline. At no point will there be internet access.
AFAIK, there is no way to include a pre-populated sqlite DB (and I know you cannot include an indexedDB), so all the data must be included outside a database, but accessible to the javascript code, and then on the first run, put into the database.
What's the best way to do this? (Both from a development/maintenance standpoint as well as actual deployment)
Do I create a database in development and write additional code to spit that out as one of these:
- JSON objects?
- Arrays?
- Actual JavaScript code for inserting into the IndexedDB?
- CSV files?
How does everyone else handle this?
回答1:
I use actual js code with data in array of JSON object. Just JSON file is better, but not way to load it. CSV file may also be desirable, since user can easily edit and inspect. It is also quite trivial to convert csv to json objects.
回答2:
In response to install, create the database and then read in a text file containing the data and insert its contents into the database. Something like the following:
chrome.runtime.onInstalled.addListener(function(details, prevVersion) {
var req = indexedDB.open(dbname,dbversion);
req.onupgradeneeded = function(event) {
var db = event.target;
db.createObjectStore('store1','etc');
};
var xhr = new XMLHttpRequest();
xhr.onload = function(event) {
var data = event.target.response;
for(key in initData) {
req.result.getObjectStore('store1').put(key, initData[key]);
}
};
xhr.open("GET", chrome.extension.getURL('/initdata.json'), true);
xhr.type='json';
xhr.send();
});
来源:https://stackoverflow.com/questions/16597577/best-way-to-include-data-that-will-populate-indexeddb-with-offline-html5-app