从弹出窗口中调用后台函数
有没有办法从弹出窗口调用后台脚本中的函数?我无法比这个问题进一步解释。这不是我想做的事情的错误,而是我完全不知道该怎么做的事情。我希望能够单击弹出页面中的按钮来调用后台页面中定义的函数。
Is there a way to call a function in the background script from the popup? I can't explain it much further than that question. It's not an error I'm having with what I'm trying to do but rather something I completely don't know how to do. I want to make it possible to click a button in the popup page that'll call a function defined in the background page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个
Try this
使用消息传递确实是可能的。
popup.js
背景.js
It is indeed possible, using Message Passing.
popup.js
background.js
您可以只在popup.js 中调用background.js 函数。您不需要做任何额外的事情。至少对我来说是这样。
编辑:您可能需要添加
"background": {
“脚本”:“背景.js”
}
在你的manifest.json 文件中。
You can just call background.js functions in popup.js. You don't need to do anything extra. At least that is the case for me.
Edit: you probably need to add
"background": {
"scripts": "background.js"
}
in your manifest.json file.
您仍然可以使用
chrome.extension.sendMessage
。但这种做法正在被弃用。
请改用 chrome.runtime.sendMessage 和 chrome.runtime.onMessage.addListener。
You can still use
chrome.extension.sendMessage
.But that is getting deprecated.
Use
chrome.runtime.sendMessage
andchrome.runtime.onMessage.addListener
instead.