I am a HTML/Javascript novice trying to create a simple Browser Action" chrome extension. I have spent several hours trying to get a piece of it to work. When the popup opens, a button exists called"myBtn", when clicked an element with the ID of "Demo" is supposed to change from text to innerHTML that is the current tabs Url. I have managed to get to a point where upon clicking the button the default text is replaced with "undefined". Every change I make to fix this seems to take me backward. I have read many posts on this and other sites but cannot resolve. Anyone see the error in my code preventing the Url from diplaying?
I have "tabs" and "activeTab" permissions in the manifest. relevant code is:
"manifest_version": 2,
"name": "Sample",
"description": "This extension launches Form on current page",
"version": "1.0",
"icons": { "128": "ITVCicon128x128.png" },
"browser_action": {
"default_icon": "ITVCicon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"activeTab",
"https://ajax.googleapis.com/"
Popup.html is:
<!DOCTYPE html>
<html>
<body>
<h1>My Web Page</h1>
<p> click the button to get the current tab URL for cut/paste <button
id="myBtn"> Try it</button> </p>
<p id="demo">Url displays Here</p>
<script src="popup.js"></script>
</body>
</html>
The popup.js containing the functions is:
function geturl() {
document.getElementById("demo") .innerHTML =
chrome.tabs.query({currentWindow: true, active: true}, function (tabs){
var tabURL = tabs[0].url;
console.log(tabURL);
});
}
document.getElementById("myBtn").addEventListener("click", geturl);