javascript - 从 Chrome 扩展程序清除特定域的 session 和本地存储

标签 javascript jquery google-chrome google-chrome-extension session-storage

是否可以通过 chrome 扩展清除特定域的 session 和本地存储?

假设我想将 somedomain.com 定位到 sessionStoragelocalStorage。我现在的做法是:

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/

相关文章:

javascript - 对单击和按下(向左和向右)箭头键执行操作

javascript - 使用 Spring 转义 HTML 中的撇号

如果我使用 14 :30 目标,javascript 运输倒计时会失败

javascript - 用jquery替换字符

javascript - 在 angularJS 中使用 html ID

html - IE 9 中无法加载字体

javascript - 在哪里可以找到 Upshot.js 示例和文档?

jquery - 在没有滚动条的全屏页面上具有视差效果的图像

ios - WebRTC 在 Chrome 21 for iOS 中可用吗?

php - 与 laragon 一起安装的 Laravel 项目在谷歌浏览器中强制使用 https