Chrome 扩展程序如何将数据从内容脚本发送到 popup.html [英] Chrome Extension how to send data from content script to popup.html
问题描述
我知道很多帖子都问过这个问题,但老实说我不明白.我是 JavaScript、Chrome 扩展和所有东西的新手,我有这门课.所以我需要制作一个插件,使用跨域请求来计算任何给定页面上的 DOM 对象.到目前为止,我已经能够使用 Chrome 扩展 API 实现这一点.现在的问题是我需要在我的 popup.html 页面上从 contentScript.js 文件中显示数据.我不知道该怎么做,我已经尝试阅读文档,但是在 chrome 中发送消息我就是不明白该怎么做.
I know this this has been asked in numerous posts but honestly I don't get them. I am new to JavaScript, Chrome Extensions and everything and I have this class assignment. So I need to make a plugin that would count DOM objects on any given page using Cross Domain Requests. I've been able to achieve this so far using Chrome Extension API's. Now the problem is I need to show the data on my popup.html page from the contentScript.js file. I don't know how to do that I've tried reading the documentation but messaging in chrome I just can't understand what to do.
以下是到目前为止的代码.
following is the code so far.
manifest.json
manifest.json
{
"manifest_version":2,
"name":"Dom Reader",
"description":"Counts Dom Objects",
"version":"1.0",
"page_action": {
"default_icon":"icon.png",
"default_title":"Dom Reader",
"default_popup":"popup.html"
},
"background":{
"scripts":["eventPage.js"],
"persistent":false
},
"content_scripts":[
{
"matches":["http://pluralsight.com/training/Courses/*", "http://pluralsight.com/training/Authors/Details/*", "https://www.youtube.com/user/*", "https://sites.google.com/site/*", "http://127.0.0.1:3667/popup.html"],
"js":["domReader_cs.js","jquery-1.10.2.js"]
//"css":["pluralsight_cs.css"]
}
],
"permissions":[
"tabs",
"http://pluralsight.com/*",
"http://youtube.com/*",
"https://sites.google.com/*",
"http://127.0.0.1:3667/*"
]
popup.html
<!doctype html>
<html>
<title> Dom Reader </title>
<script src="jquery-1.10.2.js" type="text/javascript"></script>
<script src="popup.js" type="text/javascript"></script>
<body>
<H1> Dom Reader </H1>
<input type="submit" id="readDom" value="Read DOM Objects" />
<div id="domInfo">
</div>
</body>
</html>