Is it possible to prevent a minimized tab or closed Google Chrome browser from falling asleep?

  • 0
    Is it possible to make it so that after some time, say an hour or two, the tabs in the minimized browser do not fall asleep and do not refresh when the cursor moves over these tabs? Required for the extension to run in the background
    JavaScript Arianna Leonard, Mar 26, 2019

  • 1 Answers
  • 0
    Yes, in theory, you can prevent the unloading of tabs by applying 2 tricks.



    Add to background.js :

    chrome.tabs.onCreated.addListener(function(tab) {
    chrome.tabs.update(tab.id, {
    autoDiscardable: false
    });
    });

    chrome.tabs.onReplaced.addListener(function(tabId) {
    chrome.tabs.update(tabId, {
    autoDiscardable: false
    });
    });

    chrome.runtime.onInstalled.addListener(function(details) {
    chrome.tabs.query({}, function(tabs) {
    tabs.forEach(function(tab) {
    chrome.tabs.update(tab.id, {
    autoDiscardable: false
    });
    });
    });
    });


    You must have the appropriate permissions in your extension manifest:

    ...
    "permissions" : [ "tabs" ],
    "background" : { "persistent": true, "scripts": [ "background.js" ] },
    ...


    In addition, on the active tab, you need to place an mp3 track and start it in an endless loop:

    <video id="antifreeze" style="position: fixed; right: 30px; bottom: 30px; z-index: 99999; height: 55px; width: 300px;" controls loop name="media">
    <source src="https://qna.habr.com/silence.mp3" type="audio/mpeg">
    </video>


    Starting with version 81 of chrome, the tabs freeze after 5 minutes after minimizing. The exceptions are tabs in which some medical content is played (video there or music).
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!