简体   繁体   中英

IndexedDB instance shared between addon content script and settings page

I am trying to build a browser extension that has a content script which collects data from the visited webpages and saves it into an IndexedDB.

The extension has a "settings page" which is an html popup; I would like for this page to be able to access data from the same indexedDB used by the content script, but so far I have had no success.

I thought the problem might be caused by the browser CORS policies (I am now testing it on Firefox), but the same issue does not happen if using the local storage API - in that case I can easily access the same info from both scripts.

Below is the code used by the settings page in order to open the db (the same code used by the content script, where it correctly retrieves all data inserted):

var db;
var openRequest = indexedDB.open('dbname', 1);

openRequest.onupgradeneeded = function(e) {
  var db = e.target.result;
  if (!db.objectStoreNames.contains('profiles')) {
    var storeOS = db.createObjectStore('profiles',
      {keyPath: 'id'});
  }
};
openRequest.onsuccess = function(e) {
  console.log('[*] Local database opened successfully');
  db = e.target.result;
  document.getElementById('utenti').innerHTML += 'SUCCESS';
};
openRequest.onerror = function(e) {
  console.log('[!] Error while opening local database');
  console.dir(e);
  document.getElementById('utenti').innerHTML += 'FAIL';

};

function logTimestamps(timestamps) {
  document.getElementById('utenti').innerHTML += '<br>There are ' + timestamps.length +
    ' timestamp(s) saved in IndexedDB: ' + timestamps.join(', ');
}

function getProfiles() {
    var tx = db.transaction('profiles', 'readonly');
    var profilesDB = tx.objectStore('profiles');

    if ('getAll' in profilesDB) {
      profilesDB.getAll().onsuccess = function(event) {
        logTimestamps(event.target.result);
      };
    } else {
      var timestamps = [];
      profilesDB.openCursor().onsuccess = function(event) {
        var cursor = event.target.result;
        if (cursor) {
          timestamps.push(cursor.value);
          cursor.continue();
        } else {
          logTimestamps(timestamps);
        }
      };
    }

}

I have tried to "manually" add items into the IndexDB from the settings page script; doing so, the content is correctly extracted from the db.

Therefore I suppose it must be a problem of sharing the same db between the two scripts. How can I achieve this? Thanks!

You can connect to the same indexedDB database from two scripts so long as both scripts share the same origin. To connect to the same indexedDB database from two scripts that share the same origin, you open a connection to the same database name in each script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM