Eug*_*gun 7 google-chrome local-storage google-chrome-extension
我的 chrome 扩展有两种状态: 1. 某些站点在 localStorage(另一个域)中有身份验证数据,所以我必须显示主窗口。2.没有身份验证数据,所以我必须显示带有登录名和密码表单的窗口。
为了定义是否提供身份验证数据,我想检查他的 localStorage,但看起来不可能。
但是,chrome.storage.local.get并且chrome.storage.local.set非常适合扩展的 localStorage。
或者有没有办法做到这一点 - 访问另一个站点的 localStorage ?
是的,您可以在页面上执行脚本来访问本地存储。有几种不同的方法可以做到这一点,包括内容脚本或注入脚本。
我使用chrome-extension-async了async/await支持。
例如在 popup.js
document.addEventListener('DOMContentLoaded', async () => {
try {
const key = "foobar"
// Get the current tab
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
const tab = tabs[0];
// Execute script in the current tab
const fromPageLocalStore = await chrome.tabs.executeScript(tab.id, { code: `localStorage['${key}']` });
// Store the result
await chrome.storage.local.set({[key]:fromPageLocalStore[0]});
}
catch(err) {
// Log exceptions
}
});
Run Code Online (Sandbox Code Playgroud)