是否可以使用chrome.tabs.sendmessage从内容脚本向背景页发送消息
Is it possible to send a message from a Content Script to a Background Page using chrome.tabs.sendmessage?
我已经找到了很多关于将消息从后台页面发送到内容脚本的示例和文档,但无法找到将消息从内容脚本发送到后台页面的方法。
原因是我想使用chrome.downloader.download
,例如
chrome.downloads.download({
"url": randomImageForSpotCheck
}, function () {...
spotcheck(randomImage);
});
第一次谷歌搜索出现:
从内容脚本发送请求如下:
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
(此处)
在你的后台脚本中,你应该有一些代码来监听这样一条消息:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (sender.tab) {
// is from content script
// access stuff like:
// request.horseradish
sendResponse({});
}
});
并且您的内容脚本可以发送消息:
// vv put whatever data you want here
chrome.runtime.sendMessage({horseradish: true}, function(response) {
// this is a callback function to execute,
// response is the object sent by your sendResponse({});
// this one ^^
});
相关文章:
- Google Apps 脚本:尝试根据单元格的背景颜色发送电子邮件
- 脚本随机更改背景渐变色[但从特定范围]
- 是否可以使用chrome.tabs.sendmessage从内容脚本向背景页发送消息
- 简单的 Java 脚本背景图像不起作用
- 编写替换背景图像的用户脚本
- 将背景图像更改为另一个由ajax调用脚本动态创建的图像
- 谷歌浏览器扩展程序内容脚本和背景
- Chrome 扩展 API:Chrome.tabs.captureVisibleTab 在背景页面上到内容脚本
- 设置使用背景更改脚本的背景
- 在java脚本的帮助下,在日历中显示背景日期应该是红色的,我也从oracle数据库中获取数据
- java脚本不会更改背景图像
- 如何从我的Chrome扩展中获取变量'的背景脚本
- chrome扩展中的可变范围,尤其是在背景页和内容脚本之间
- 内容脚本和背景交流
- 选项启用了内容脚本Chrome扩展,没有背景页
- 网站不会显示在IE一旦我激活脚本.除了背景,一切都不见了
- 脚本定义加载时的背景,宽度/高度可变
- 内容脚本和背景页面之间的消息传递在chrome扩展是不工作,因为它应该是
- 背景脚本代码和内容脚本代码的区别
- 如何有背景脚本和类似于默认弹出的东西