【发布时间】:2018-02-13 05:51:09
【问题描述】:
我的问题是:有没有办法在我的自定义 Chrome DevTools 面板和我的 Chrome 扩展的内容脚本之间直接通信?现在,我似乎需要使用后台脚本作为内容脚本和开发工具面板之间的中介。
我尝试使用自定义窗口事件:
//在开发工具面板中
let event = new Event('suman-dev-tools', {
value: 'foo'
} as any);
window.dispatchEvent(event);
//在内容脚本中
window.addEventListener('suman-dev-tools', function(ev){
console.log('my devtools panel has spoken:', ev);
});
但这似乎不起作用 - 看起来我无法使用窗口事件在两件事之间进行通信。我必须使用后台页面在 devtools 页面和内容脚本之间进行通信吗?
【问题讨论】:
-
尝试通过 chrome.runtime.connect 为您的内容脚本打开一个端口
-
wOxxOm 如果你不知道这个技巧,@Deliasz 的答案很好
标签: javascript google-chrome google-chrome-extension google-chrome-devtools