【发布时间】:2015-12-13 07:44:30
【问题描述】:
我正在尝试制作 chrome 扩展。我的后台脚本在用户界面中按下按钮时触发。它应该向我的内容脚本发送一条消息,该脚本会将响应发送回后台脚本。无法完全弄清楚这应该如何工作。单击侦听器正在触发,但消息传递不起作用。这是我到目前为止得到的:
后台脚本:
$(document).on('click', '#getGlobalFuncsBtn', function(){
chrome.extension.sendMessage({text:"getStuff"},function(reponse){
if(reponse.type == "test"){
console.log('test received');
}
});
console.log('click listener');
});
内容脚本:
chrome.extension.onMessage.addListener(function(message,sender,sendResponse){
if(message.text == "getStuff"){
console.log('test sent');
sendResponse({type:"test"});
}
});
弹出 HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button id="getGlobalFuncsBtn">Get Global Funcs</button>
<script src="jquery-2.0.2.min.js"></script>
<script src="background.js"></script>
</body>
</html>
manifest.json:
{
"manifest_version": 2,
"name": "Var Viewer",
"description": "This extension allows you to view and change all user defined js global vars.",
"version": "1.1",
"permissions": [
"storage"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["jquery-2.0.2.min.js","content.js"]
}
],
"web_accessible_resources": [
"jquery-2.0.2.min.js"
],
"background": {
"scripts": ["background.js"],
"persistent": true
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "pop.html",
"default_title": "Var Viewer"
}
}
【问题讨论】:
标签: javascript google-chrome-extension