As I understand from Manage Events with Background Scripts and Migrate to Event Driven Background Scripts background script should be activated when events triggered.
background.js
chrome.runtime.onMessage.addListener((message, sender, reply) => {
const json = message.data;
// some code
reply({ result: true })
return true;
});
popup.js
chrome.runtime.sendMessage({ data: [<ArrayWithData>] },
function (response) {
logger.log(response);
}
);
Everything works well, but only in case of active background.
Why background not become active? Can someone explain what is my mistake? Or how can I activate and execute background.js on click from popup.js ?
I know that if I change persistence: true
in manifest.json or just remove it, everything will works fine. But I want to keep persistence false
and trigger background.js when needed.
You missed this part in the documentation that explains how a background script should be activated from popup.js
. After retrieving the background page object, you just need to call any function or even access a field.
I always put this at the top of my popup.js
:
// Initialize background page
chrome.runtime.getBackgroundPage(function(backgroundPage) {
console = backgroundPage.console;
})
That way I can also view console logs from the popup together with logs from the background view
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.