当键值未知时,如何遍历 chrome.storage 中的每个项目?

How to loop through each item in chrome.storage, when key values are unknown?

我可以遍历使用 chrome.storage.sync.set 存储的每个项目吗? 所有存储的值都有一个 URL 和一些 CSS 代码,但问题是,我不知道 URL。我在 popup.js:

中想到了这样的事情
for (item in chrome.storage.sync.get()) {
    alert("Executed for ${url}")
    // How would I get url?
}

但是,即使没有无效变量,也永远不会执行。

我怎样才能做到这一点?

所有 chrome API return Promise 或可以接受回调的都是 异步.

ManifestV3:在声明为 async

的函数中使用 await
async function enumStorage() {
  const all = await chrome.storage.sync.get();
  for (const [key, val] of Object.entries(all)) {
    // do something
  }
}

ManifestV2:使用回调

chrome.storage.sync.get(all => {
  for (const [key, val] of Object.entries(all)) {
    // do something
  }
});

请注意,弹出窗口是一个单独的 window,因此它有自己独立的开发工具和控制台:right-click 在弹出窗口中,select “检查”在菜单中。