According to the documentation, the browserAction icon can either have an action triggered OR a popup window opened, but not both simultaneously. You can switch between one or the other programmatically, but never have both work at the same time.
The listener defined in chrome.browserAction.onClicked.addListener
will NOT be called if you have a popup.html
file defined in your manifest.
On the other hand, if you want to programmatically modify the HTML of the popup window, you can do so only when the popup window is open, so you either include the code in the popup.js
file itself, or you use chrome.runtime.getBackgroundPage
to access the background window object and call the code you already had there.
For example:
background.js
var $username = "noname", $password;
function modifyMyPopup(){
var popup = chrome.extension.getViews({type:"popup"}); //get the open popup windows
for (var i = 0; i < popup.length; i++) //check all open popups
if (popup[i].popupMarker) //if it is our browserAction popup...
popup[i].document.body.innerHTML = "Hello" + $username; //modify its body's innerHTML
}
chrome.storage.sync.get(["username","password"],function(items){ //you can get several items at once from the storage
$username = items.username;
$password = items.password;
});
popup.html
<html>
<head>
<script src="popup.js" defer></script>
</head>
<body>
Goodbye!
</body>
</html>
popup.js
var popupMarker = 1;
chrome.runtime.getBackgroundPage(function(bgPage){
bgPage.modifyMyPopup();
});
background.js:20 Uncaught TypeError: Cannot read property 'innerHTML' of undefined(…) – Eminem347 Dec 09 '16 at 14:16