是否可以通过 chrome 扩展清除特定域的 session 和本地存储?
假设我想将 somedomain.com
定位到 sessionStorage
或 localStorage
。我现在的做法是:
function clearSessionSotorage(key) {
/**
* Clears the session storage value for the given key
* @param string key The key whose value should be cleared, if not provided, all keys are cleared for the current domain
*/
var code = key ? 'window.sessionStorage.clear(' + key + ')' : 'window.sessionStorage.clear()';
chrome.tabs.executeScript(null, { code: code });
}
function clearLocalStorage(key) {
/**
* Clears the local storage value for the given key
* @param string key The key whose value should be cleared, if not provided, all keys are cleared for the current domain
*/
var code = key ? 'window.localStorage.clear(' + key + ')' : 'window.localStorage.clear()';
chrome.tabs.executeScript(null, { code: code });
}
只要事件选项卡打开了 somedomain.com
上的页面,此功能就有效。
但是,我希望定位特定域,而不需要将该域显示在事件选项卡中。
如果可以的话,我该怎么做?
我考虑过循环浏览所有选项卡来查找目标 URL,这将是一个改进,但实际上,我更喜欢一个完全避免域处于事件状态的选项。
最佳答案
如果您希望清除当前打开但在浏览器窗口中未事件的特定域的 sessionStorage/localStorage,则需要在该域的上下文中执行一些脚本。
我认为您可以使用 chrome.tabs.query()
( link1 ) 和 chrome.tabs.executeJavaScript()
( link2 ) 的组合来完成此操作代码为字符串或使用内容脚本文件的名称。
chrome.tabs.query({
'url' : 'target-domain.com'
});
chrome.tabs.executeScript({
code: 'sessionStorage.clear()'
});
(or)
chrome.tabs.executeScript(null, {file: "content_script.js"});
对于 tabs.query() 中的 url
属性,您可以使用上述模式 here ;
关于javascript - 从 Chrome 扩展程序清除特定域的 session 和本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36216266/