如何在内容脚本和后台页面之间进行通信
我知道这个问题之前已经被问过,但我不知道如何让它发挥作用。
这是内容脚本:
console.log("online");
chrome.extension.sendRequest({foo: "yes"}, function(response) {
console.log(response.thefoo);
});
这是背景页面:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.foo == "yes")
sendResponse({thefoo:"this is the foo"});
else
sendResponse({"it didnt work"});
});
我这里的代码,它来自这里已回答的问题之一,我做了一些更改,但即使我准确地放置它,它也不起作用。 您可以在此处查看答案 Chrome 扩展程序:在内容脚本中访问 localStorage
i know this question has been asked before but i dont know hwo to make it work.
this is the content script:
console.log("online");
chrome.extension.sendRequest({foo: "yes"}, function(response) {
console.log(response.thefoo);
});
and this is the background page:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.foo == "yes")
sendResponse({thefoo:"this is the foo"});
else
sendResponse({"it didnt work"});
});
the code that i have here, its from one of the answered questions around here with a few changes that i made, but it didnt work even when i put it exactly.
you can see that answer here Chrome extension: accessing localStorage in content script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
---==== background.html ====
---=== contentScript.js ==---
您还需要在清单文件中定义内容脚本
---=== background.html ===---
---=== contentScript.js ===---
you also need to have the content script defined in the manifest file