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

假装没事ソ 提交于 2020-02-05 04:23:26

问题


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 permissions? i currently have:

"permissions": [ "tabs", "bookmarks", "unlimitedStorage", "*://*/*", "identity", "https://*.google.com/*", "https://ssl.gstatic.com/", "https://www.googleapis.com/", "https://accounts.google.com/" ],

with what command i can do this? thank you!

Edit: i have readed i can access it from content script(aslong as the tab with domain is open - which is my case), but i dont know how to do that...


回答1:


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
              }
           }
}



回答2:


To access indexeddb of current tab add "activeTab" to "permissions" tag in manifest.json, Then create a content script, content script will be helpful in accessing the indexeddb as it runs in context of webpages, then add the content script created to the "content_scripts" tag in manifest.json file. For Eg in manifest.json add the following:

"permissions": ["activeTab"],
  "content_scripts": [
  {
  "matches": ["add the domains of the webpages where content script needs to run"],
  "js": ["contentScript.js"]
  }
]

For more info on matches check out here:https://developer.chrome.com/extensions/match_patterns .

Inside content script add open the store and then perform transaction on the object store and perform queries on the object store. For Eg in content script add following:

if (!("indexedDB" in window)) {
  alert("This browser doesn't support IndexedDB");
 } else {
  let indexdb = window.indexedDB.open("firebaseLocalStorageDb", 1);
  indexdb.onsuccess = function() {
  let db = indexdb.result;
  let transaction = db.transaction("firebaseLocalStorage", "readwrite");
  let storage = transaction.objectStore("firebaseLocalStorage");
  console.log(storage.getAll());
 };
}

Explanation of the above code: It accesses the window object and opens the store "firebaseLocalStorageDb" with version "1", then after successfully accessing the object it looks for the result and performs transaction on the objectstore "firebaseLocalStorage" residing inside the store. Finally query the instance of objectstore "storage" to get all the key-value pairs. For more info check: https://javascript.info/indexeddb



来源:https://stackoverflow.com/questions/38589075/how-to-access-indexeddb-of-current-opened-domain-tab-from-chrome-extension

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