javascript - 如何从内容脚本向弹出脚本发送消息以显示在 popup.html 上
问题描述
****我正在制作一个谷歌扩展软件,我有一个在内容脚本文件上创建的数组,我想在 popup.html 上显示它的元素,在那里我创建了一个 div 元素来包含它们,我想要每个元素数组位于不同的 div 中。
//content script
var arr= [a,b,c,d];
//popup.html
<div class="showhere"></div>
//required popup.html to be
<div class="showhere">
<div>a</div>
<div>b</div>
<div>c</div>
<div>d</div>
</div>
我希望我可以通过示例获得解决方案,谢谢。****
解决方案
弹出脚本,
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.subject == 'SOME_SUBJECT') {
console.log(request.data);
sendResponse("response");
}
else if (request.subject == 'OTHER_SUBJECT') {
console.log(request.data);
sendResponse("response");
}
});
内容脚本,
chrome.runtime.sendMessage({
subject: "SOME_SUBJECT",
data: {
name: "Roar"
}
}, function (response) {
console.log(response)
});
推荐阅读
- java - Cucumber Junit Runner 未显示测试用例行
- typescript - 与“any”类型相交的类型的 Typescript 泛型自动扩展为“any”类型
- build - 如何跨语言或跨平台构建单声道存储库?
- python-3.x - 如何从字符串列表中删除不需要的字符?
- c# - 根据服务器端任务的进度定期更新客户端 UI
- algorithm - Photoshop算法固定宽高
- amazon-web-services - 如何安排 AWS Lambda 根据小时以不同的频率运行
- angular - Ag-Grid 聚合字符串并在列上显示总和值
- c# - 在 Linq 中使用字典
- python - 从 Python 向 php 服务器发送数据