【问题标题】:I want to do DOM manipulation in my chrome extension using chrome.commands我想使用 chrome.commands 在我的 chrome 扩展中进行 DOM 操作
【发布时间】:2020-11-22 23:55:20
【问题描述】:

我想制作一个 chrome 扩展,我可以在特定页面上使用键盘命令(例如:“Ctrl+Shift+A”),然后触发一些 DOM 操作代码(例如单击按钮)。

但我面临的问题是“chrome.commands”在后台环境中运行(manifest.json 中的背景属性),如果我在 content_scripts 中包含“chrome.commands.onCommand”(在 manifest.json 中)我得到这个错误。

Uncaught TypeError: Cannot read property 'onCommand' of undefined.

如何让它发挥作用?

【问题讨论】:

    标签: javascript dom google-chrome-extension


    【解决方案1】:

    使用两个脚本。 background script 注册了一个 chrome.commands 监听器,该监听器注入了一个 content script(或向 manifest.json 中声明的内容脚本发送消息)。内容脚本在网页中运行并可以访问其 DOM。

    【讨论】:

      猜你喜欢
      • 2018-06-13
      • 2016-03-30
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 2014-06-03
      • 2017-07-30
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多