[英]Data not passing from popup to background in chrome extension
I think I got the hang of it but it's just not working... and I can't figure out why. 我想我掌握了这个要诀,但是它没有用……我不知道为什么。
Manifest: 表现:
{
"name": "Dummy Extension",
"description": "Dummy Extension Description",
"version": "2.0",
"permissions": [
"activeTab"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_title": "Dummy Extension",
"default_popup": "popup.html"
},
"manifest_version": 2,
"content_scripts": [ {
"js": [ "jquery.min.js", "background.js" ],
"matches": [ "http://*/*", "https://*/*"]
}]
}
Background: 背景:
chrome.extension.onMessage.addListener( function(request,sender,sendResponse)
{
if( request.greeting === "GetURL" )
{
var tabURL = "Not set yet";
chrome.tabs.query({active:true},function(tabs){
if(tabs.length === 0) {
sendResponse({});
return;
}
tabURL = tabs[0].url;
sendResponse( {navURL:tabURL} );
});
}
}
Popup.html Popup.html
<!DOCTYPE html>
<head>
<script src='popup.js'></script>
<script src='jquery.min.js'></script>
</head>
<body>
Hello World!
<br />
<input id="tabURL" type="text" />
<br />
<input value="SEND!" type="button" id="send" />
</body>
</html>
And popup.js 还有popup.js
function getURL() {
chrome.extension.sendMessage({greeting: "GetURL"},
function (response) {
tabURL = response.navURL;
$("#tabURL").val(tabURL);
});
}
$("#send").click(getURL());
I just can't figure out whats wrong, jquery is defined, I get no console errors. 我只是不知道出了什么问题,定义了jquery,没有控制台错误。 Any help would be great! 任何帮助将是巨大的!
$("#send").click(getURL());
gets executed before DOM is fully constructed and fails. 在完全构建DOM并失败之前执行。 Also, you need to pass the reference to getURL
, not execute it. 另外,您需要将引用传递给getURL
,而不是执行它。
To fix: 修理:
$(document).ready(function(){
$("#send").click(getURL);
});
By the way, you may be looking in the wrong console for errors. 顺便说一句,您可能正在错误的控制台中查找错误。 See this debugging tutorial for popups. 有关弹出窗口,请参见此调试教程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.