Search code examples
google-chrome-extension

How to make chrome extension active permanent


im building an extension in chrome but it's always inactive if i dont open debug mode of plugin, it lead to the plugin does not working when i enter to target pages, eg: dms.mydomain-inc.com

enter image description here

How to make it's active permanent when i access to page registering in manifest.json? Here is my manifest.json

{
    "name": "Tool for test",
    "description": "Build an Extension!",
    "version": "1.0",
    "manifest_version": 3,
    "background": {
      "service_worker": "background.js",
      "persistent": true
    },
    "options_page": "options.html",
    "permissions": ["storage", 
        "activeTab", 
        "scripting",
        "webRequest"],
    "host_permissions": [
        "*://*.mydomain.vn/*",
        "*://dms.mydomain-inc.com/",
        "wss://news.mydomain-inc.com/"
        ],
    "content_scripts": [
        {
            "matches": ["*://news.mydomain-inc.com/"],
            "css": ["css/style.css"],
            "js": ["contentScript.js", "jquery-3.5.1.min.js"]
        }
        ],
    "action": {
      "default_popup": "popup.html",
      "default_icon": {
        "16": "/images/get_started16.png",
        "32": "/images/get_started32.png",
        "48": "/images/get_started48.png",
        "128": "/images/get_started128.png"
      }
    },
    "icons": {
      "16": "/images/get_started16.png",
      "32": "/images/get_started32.png",
      "48": "/images/get_started48.png",
      "128": "/images/get_started128.png"
    }
  }

Thanks


Solution

  • it is normal thing developing manifest version 3 extensions. Chrome is becoming a browser that is everything but light and this is partly due to the dozens of extensions the user installs. Thus Google introduced service worker in extensions in order to free up some memory whenever possible. SW is activated when necessary and then goes to sleep until the moment it's awakened to perform a new job.

    Said this,

    1. "persistent": true in manifest is useless and could generate an error.
    2. we have to realize that when SW becomes inactive all variables and objects defined in this script will be lost unless we plan to save them somehow in a persistent storage. Normally, the asynchronous chrome.storage API are used to save variables \ objects used in the SW.
    3. If you cannot allow SW become inactive (for some important reason) you could use a "dirty" technique that bypasses this "restriction". This technique consists in opening a long-lasting communication channel and sending fictitious messages between SW and any browser tab. For more details on this technique read this thread: Persistent Service Worker in Chrome Extension