Best way to include data (that will populate IndexedDB) with offline HTML5 app?

寵の児 提交于 2021-01-27 14:27:19

问题


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:

  1. JSON objects?
  2. Arrays?
  3. Actual JavaScript code for inserting into the IndexedDB?
  4. 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

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