how to access IndexedDB (of current opened domain/tab) from chrome extension

后端 未结 2 1427
北荒
北荒 2021-01-07 01:49

I currently have indexedDB on google.com domain. i want to be able to read it from google chrome extension. how can i accomplish this? do i need to add any specific permissi

2条回答
  •  孤街浪徒
    2021-01-07 02:15

    For anyone still interested, my solution to this problem -

    this is placed in content script of extension -

     chrome.extension.onConnect.addListener(function(port) {
     if(port.name == "extension_request" ) {
      port.onMessage.addListener(function(msg) {
        if (msg.db) {
          window.indexedDB.webkitGetDatabaseNames().onsuccess = function(sender,args)
          {
            var r = sender.target.result;
            if(r.contains(msg.db)){
                var openRequest = indexedDB.open(msg.db);
                // your code
                port.postMessage({foo: bar}); // your result which you want to send        
            }
           }
        }
     }
    }
    

    and this is for background or popup script -

     chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            var port = chrome.tabs.connect(tabs[0].id,{name: "extension_request"});
                port.postMessage({db: "database_name_example"}); // send database name
                port.onMessage.addListener(function(msg) {
                  if (msg.foo ) {
                   // do your stuff in extension
                  }
               }
    }
    

提交回复
热议问题