javascript - 弹出脚本复制后台脚本网络活动 - Chrome 扩展
问题描述
我使用 React 制作 Chrome 扩展并使用 WebPack 构建。我有 3 个输出文件,背景、内容和弹出窗口。
在我注入的内容脚本中,我有一个执行以下操作的函数:
chrome.runtime.sendMessage({action: "getDesigns", name: $}, (response) => {
if (response.error) throw(response.error);
this.setState({
designs: response.data.designs,
fetching: false,
selectedFile: null
})
});
在我的后台脚本中,我有一个该操作的侦听器,当触发它时,它会调用此函数
function bgGetDesigns(req, sendResponse) {
getDesigns({
'per_page': 100,
'name': req.name
})
.then((res) => {
sendResponse({success: true, data: res});
})
.catch((e) => {
sendResponse({success: false, error: e});
});
}
我注意到的是,如果我的弹出脚本打开,当我通过内容脚本调用 sendMessage 时,后台脚本执行网络请求(正确)但是弹出脚本也执行网络请求(错误)
这是预期的行为,还是由 webpack 构建我的包的方式引起的?
解决方案
推荐阅读
- ruby-on-rails - Rails将设计用户名设置为评论属性
- pattern-matching - SML/NJ 中的模式匹配空 Fifo.fifo
- machine-learning - sklearn 实际是如何计算 AUROC 的?
- sql - 在 PeopleSoft 中显示正在运行的进程的状态
- javascript - 如果某些 http 请求待处理,Window.History.pushState 不起作用
- angular - 如何从提供者中的承诺将此数据返回到组件
- apache-kafka - 如何在谷歌云上部署 Kafka
- java - Java中调用了哪个重载方法
- go - golang函数可以返回接口{}{} - 如何返回地图列表
- mysql - 获取 java.lang.NumberFormatException:对于输入字符串:“aNumber”