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
<!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
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. Any help would be great!
$("#send").click(getURL());
gets executed before DOM is fully constructed and fails. Also, you need to pass the reference to getURL
, not execute it.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.