How do I refresh/reload a Chrome Extension?

24,393

Solution 1

The chrome.send function is not accessible by your extension's javascript code, pages like the newtab page, history and the extensions page use it to communicate with the C++ controller code for those pages.

You can push updates of your extension to users who have it installed, this is described here. The user's application will be updated once the autoupdate interval is hit or when they restart the browser. You cannot however reload a user's extension programmatically. I think that would be a security risk.

Solution 2

Now the simplest way to make extension to reload itself is to call chrome.runtime.reload(). This feature doesn't need any permissions in manifest. To reload another extension use chrome.management.setEnabled(). It requires "permissions": [ "management" ] in manifest.

Solution 3

window.location.reload() works for me

I am using chromium 6.x so it might be fixed in newer version

Solution 4

I just had this same problem with an extension.

Turns out you can listen for storage changes within background.js using chrome.storage.onChanged and have that perform the refresh logic.

For example:

// Perform a reload any time the user clicks "Save"
chrome.storage.onChanged.addListener(function(changes, namespace) {
  chrome.storage.sync.get({
    profileId: 0
  }, function(items) {
    // Update status bar text here
  });
});

You could also reload parts of your extension this way by taking the changes parameter into account. chrome.runtime.reload() might be easier, but this has less overhead.

Solution 5

For all future Googlers - the Browser Extension spec now includes runtime.reload() - https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/reload

For Chrome, you might need to use chrome.runtime.reload() (but I'd handle such cases via Mozilla's awesome webextension-polyfill).

Share:
24,393

Related videos on Youtube

Traveling Tech Guy
Author by

Traveling Tech Guy

I'm a technologist, entrepreneur and developer. I started 3 startups, and currently assist other companies through Traveling Tech Guy - my software consulting company. I specialize in web and mobile development, integration projects, and managing software development projects of various sizes and complexities. I'm always looking for the next challenge, and always eager to learn a new technology/stack/paradigm. Most recently I've been using NodeJS, Express, React and various JavaScript frameworks. Prior to that, I developed in Scala, PHP, and several years of .Net in the enterprise. My mobile experience includes iOS, Android and WP. I've been an avid Stack Overflow user almost from day 1. I'm thankful to the community for helping me out of sticky code situations, and hope that I can contribute back by answering as many questions as I can - on Stack Overflow, Web Applications, Super User, and Android Enthusiasts.

Updated on July 25, 2020

Comments

  • Traveling Tech Guy
    Traveling Tech Guy over 2 years

    I'm developing an extension in Chrome 4 (currently 4.0.249.0) that will show the user's StackOverflow/SuperUser/ServerFault reputation in the status bar. I've designed an options page to get the user's profile IDs and I save them to localStorage and read them well in the extension. It all works great.
    The problem is I cannot find a (programmatic) way to refresh the extension upon options saving. I tried calling location.reload(); from the extension page itself upon right clicking it - to no avail. I pursued it further and tried looking at what Chrome's chrome://extensions/ page does to reload an extension, and found this code:

    /**
     * Handles a 'reload' button getting clicked.
     */
    function handleReloadExtension(node) {
      // Tell the C++ ExtensionDOMHandler to reload the extension.
      chrome.send('reload', [node.extensionId]);
    }
    

    Copying this code to my event handler did not help (and yes, I tried replacing [node.extensionId] with the actual code). Can someone please assist me in doing this the right way, or pointing me at a code of an extension that does this correctly? Once done, I'll put the extension and its source up on my blog.

  • Traveling Tech Guy
    Traveling Tech Guy about 13 years
    Thanks Pierre. Solved my problem by alerting the user to click my extension, which cause a redraw.
  • Marek
    Marek about 12 years
    I have used it within my background.js. But you can use it from your views as well: chrome.extension.getBackgroundPage().window.location.reload(‌​)
  • gengkev
    gengkev over 10 years
    this question is kind of old, but now you can trigger reloads of extensions if you have access to the extensions management API
  • bartek
    bartek over 6 years
    security risk? we get poor developer experience instead.
  • RayfenWindspear
    RayfenWindspear over 6 years
    The background page is a "page" afterall.

Related