0

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.

Sever
  • 2,338
  • 5
  • 35
  • 55
  • 1
    The posted code isn't enough to diagnose the problem. Do you rely on global variables in the background script? First, maybe you don't need a background script at all as the popup has the same level of access. Second, make sure you're looking at the correct console: right-click the popup and click "inspect". – wOxxOm Aug 28 '18 at 04:28
  • @wOxxOm, From documentation: background script should be activated when events triggered. I call event from popup json and onMessage dont triggered. Which piece of code should also i need to add? By the way, nackground script is in inactive mode, so console.log will not work. The main goal of my post is set background active on event triggered. – Sever Aug 28 '18 at 06:39
  • You need to show [MCVE](/help/mcve), otherwise we can only randomly guess. It seems you misunderstood the documentation. The background script runs in a separate page and that hidden page is automatically loaded on a message when persistent is false. See [here](https://stackoverflow.com/a/10258029) how to inspect it. The popup is a separate page too. The background script doesn't run inside the popup (some people load it in popup.html but that's a mistake caused by misunderstanding). Your console.log runs in the popup so it'll be printed in the popup's devtools console. – wOxxOm Aug 28 '18 at 06:47
  • @wOxxOm, Sorry maybe I dont undestood you. Cause I already show MCVE. Also I dont have the trouble with console.log. And I know about background is separate page. My question is rather clear. I need to background become active when I sendMessage from popup.js. – Sever Aug 28 '18 at 08:25
  • Sadly, it's not an MCVE but I don't want to argue over this. Hopefully someone else will guess right. – wOxxOm Aug 28 '18 at 08:27
  • @wOxxOm Only thing to reproduce is just put onMessage to background.js and then sendMessage from popup.js while background.js is in inactive mode. Thanks anyway. – Sever Aug 28 '18 at 12:02
  • 1
    When I said it's not MCVE, it means the posted code doesn't reproduce the problem so "V" is missing and personally I find it very tiring to argue about such things. The posted code successfully works (logger.log being console.log), assuming everything else that isn't posted in the question is implemented correctly. – wOxxOm Aug 28 '18 at 12:14
  • @wOxxOm. Your are totally right. I use logger (from background). But background is inactive, thats why sendMessage was never called. Thank you very much. I need to be more careful – Sever Aug 28 '18 at 12:58
  • Yeah, consider using chrome.runtime.getBackgroundPage(bg => bg.console.log('foo')) – wOxxOm Aug 29 '18 at 14:12

1 Answers1

4

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

duliba
  • 269
  • 2
  • 9