Search code examples
javascriptgoogle-chromegoogle-chrome-extensionbrowser-extension

"chrome.runtime.reload is not a function" when trying to livereload my extension runtime


I am building a chrome extension, and have integrated it with livereload-js. It is working, except that the call to chrome.runtime.reload() throws an error that the function reload doesn't exist on the chrome.runtime. This is weird, since the documentation says that this method should be there and when I read the the docs for the reload method it says that this method should be available without any additional permissions needed. If this was removed in Manifest version 3, it hasn't been explained in the docs.

To simplify I have removed most of the code from the app, and am simply trying to get the livereload to work. Here is my manifest.json:

{
    "name": "Cool AF",
    "description": "something cool",
    "version": "1.1",
    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    },
    "content_scripts": [
        {
            "matches": ["https://nevermind/*"],
            "css": ["main.css"],
            "js": ["main.js", "livereload.js"]
        }
    ],
    "host_permissions": ["http://localhost:35729/"],
    "permissions": ["storage", "management", "activeTab", "alarms", "background"]
}

The code in livereload.js considers if it is a chrome extension and calls chrome.runtime.reload(), which throws an error. I have removed all other code from the extension besides a console.log that let's me know the extension is loading. Any ideas on how to get the reload function to appear on the chrome.runtime object?


Solution

  • chrome.runtime.reload isn't available for content scripts.

    See documentation for the list of methods available to content scripts; some chrome.runtime methods are, but not all:

    runtime:

    • connect
    • getManifest
    • getURL
    • id
    • onConnect
    • onMessage
    • sendMessage

    The idiomatic way is to message your background script to request it to perform an operation content scripts aren't allowed to do.