0

I made a Chrome extension to do some routine tasks that I have to do as checking some values and clicking some buttons. It works really well when it's on focused tab but it stops working when it is in background or not focused.

My background.js:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if(changeInfo.status == 'complete') {
        chrome.tabs.query({
            active: true,
            currentWindow: true
        }, function(tabs) {
            //checking something
            chrome.tabs.query({currentWindow: true, active: true},
            function(tabs) {
                chrome.tabs.sendMessage(tab.id, {type: 1});
            });
        });
    }
});

My content.js:

chrome.runtime.onMessage.addListener(function(request, sender, response) {
    if(request.type == 1) {
        //actions
    }
});

I don't really know what I can do to let this works in background too. Thanks for the help

Giorgio
  • 65
  • 10
  • The problem is likely that the site simply stops running scripts when it's not visible and there's nothing you can do about it usually. You should also remove chrome.tabs.query and simply use `tab` which is already provided in the parameters. – wOxxOm Mar 07 '20 at 15:49
  • Well, you can lie to the site about it being invisible: insert a [page script](https://stackoverflow.com/a/9517879) that 1) overrides addEventListener and ignores events like `blur` or `visibilitychange` when it's issued on `document` or `window`, 2) redefines document.visibilityState using Object.defineProperty. – wOxxOm Mar 07 '20 at 15:52

0 Answers0