如何从Chrome扩展程序访问(当前打开的域/选项卡的)IndexedDB [英] how to access IndexedDB (of current opened domain/tab) from chrome extension

查看:581
本文介绍了如何从Chrome扩展程序访问(当前打开的域/选项卡的)IndexedDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

我已经阅读了我可以从内容脚本访问它的方法(只要打开了带有域的标签-这是我的情况),但是我不知道该怎么做...

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

推荐答案

要访问当前选项卡的indexeddb,请在manifest.json中的"permissions"标签中添加"activeTab",然后创建一个内容脚本,该内容脚本将有助于访问indexeddb在网页上下文中运行时,然后将创建的内容脚本添加到manifest.json文件中的"content_scripts"标签中. 对于manifest.json中的Eg,添加以下内容:

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

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

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

在内部内容脚本中添加打开存储,然后在对象存储上执行事务并在对象存储上执行查询. 对于内容脚本中的Eg,添加以下内容:

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

上面的代码的解释: 它访问窗口对象,并打开版本为"1"的商店"firebaseLocalStorageDb",然后在成功访问该对象后,它会寻找结果并在位于商店内部的对象商店"firebaseLocalStorage"上执行事务.最后,查询对象存储存储"的实例以获取所有键值对. 有关更多信息,请检查: https://javascript.info/indexeddb

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

这篇关于如何从Chrome扩展程序访问(当前打开的域/选项卡的)IndexedDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆