【问题标题】:how to access IndexedDB (of current opened domain/tab) from chrome extension如何从 chrome 扩展访问 IndexedDB(当前打开的域/选项卡)
【发布时间】:2016-11-30 01:18:10
【问题描述】:

我目前在 google.com 域上有 indexedDB。我希望能够从 google chrome 扩展程序中读取它。我怎样才能做到这一点?我需要添加任何特定权限吗? 我目前有:

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

用什么命令我可以做到这一点?谢谢!

编辑:我已阅读我可以从内容脚本访问它(只要带有域的选项卡是打开的 - 这是我的情况),但我不知道该怎么做......

【问题讨论】:

标签: google-chrome google-chrome-extension indexeddb


【解决方案1】:

对于任何仍然感兴趣的人,我对这个问题的解决方案 -

这是放在扩展的内容脚本中-

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

这是用于背景或弹出脚本 -

 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】:

    要访问当前选项卡的indexeddb,将“activeTab”添加到manifest.json中的“permissions”标签,然后创建一个内容脚本,内容脚本将有助于访问indexeddb,因为它在网页上下文中运行,然后添加内容为 manifest.json 文件中的“content_scripts”标签创建的脚本。 例如,在 manifest.json 中添加以下内容:

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

    有关比赛的更多信息,请点击此处:https://developer.chrome.com/extensions/match_patterns .

    在内容脚本中添加打开存储,然后在对象存储上执行事务并在对象存储上执行查询。 例如,在内容脚本中添加以下内容:

    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());
     };
    }
    

    上面代码的解释: 它访问窗口对象并打开版本为“1”的存储“firebaseLocalStorageDb”,然后在成功访问该对象后查找结果并在驻留在存储内的对象存储“firebaseLocalStorage”上执行事务。最后查询objectstore“storage”的实例,得到所有的键值对。 更多信息请查看:https://javascript.info/indexeddb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多