As the other answers are mentioned, you can call the background.js functions from popup.js as follows:
var _background = chrome.extension.getBackgroundPage(); _background.backgroundJsFunction();
But to access popup.js or popup.html from background.js you have to use the message architecture as follows:
// in background.js chrome.runtime.sendMessage( { property: value } ); // in popup.js chrome.runtime.onMessage.addListener(handleBackgroundMessages); function handleBackgroundMessages(message) { if (message.property === value) // do stuff }
However, it looks like you can synchronously access popup.js from background.js, just as you can synchronously access another. chrome.extension.getViews can provide you with a popup window object, and you can use it to call functions, access variables, and access the DOM.
var _popup = chrome.extension.getViews( { type: 'popup' } )[0]; _popup.popupJsFunction(); _popup.document.getElementById('element'); _popup.document.title = 'poop'
Note that getViews() will return [] if the popup is not open, so you need to handle this.
I do not know why no one mentioned this. Perhaps there are some pitfalls or bad practices that I forgot? But in my limited testing in my own extension it works.
V. Rubinetti
source share