13

Is there a Chrome extension post install hook/API function that will let me perform an action after the plugin is installed or updated?

I would like to perform an action after my extension is installed, and only right after it is installed. This action should only be performed once (post-install or post-update) of the extension.

Update

Some people have proposed setting the version of the extension in localStorage, the problem that I have is that the content script that has access to localStorage is not loaded into the page when the plugin is first installed.

AFAIK after a plugin is installed, and it makes use of a content script injected into the tab/page, the page has to be reloaded.

I don't know how to access localStorage from the background page; localStorage can only be accessed from a content script.

To get the version number from the background page to the content script requires the use of chrome API function to execute scripts:

chrome.tabs.executeScript(null, {code:function_to_execute}, function() { // callback });

However, when you install a plugin, and the page that this plugin needs to inject a content script into is already loaded, it does not inject the content script, you have to reload the page.

update 2

Looking at some of the tips provided in more detail, for the purpose of saving the version number, it is possible to access the localStorage of the background page. However, for what I need to do, which is reload a specific tab at a specific URL (in order to make sure the content script is the newest version) after installing or updating a plugin, it ended up being unnecessary to bother with localStorage.

For the sake of staying on topic, the advice given about writing the version number to localStorage (in the background page) and then checking against the version number in the manifest file is good enough to allow someone to run a script the first time it is installed/or updated.

HowTo

Make manifest file available to the background page (note: this is taken from somewhere else, I don't take credit for it, but I can't remember the source, if you know, let me know and I will add it).

// MAKE MANIFEST FILE AVAILABLE
chrome.manifest = (function() {
    var manifestObject = false;
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            manifestObject = JSON.parse(xhr.responseText);
        }
    };
    xhr.open("GET", chrome.extension.getURL('/manifest.json'), false);
    try {
        xhr.send();
    } catch(e) {
        console.log('Couldn\'t load manifest.json');
    }
    return manifestObject;
})();

Now you can access your version number like this: chrome.manifest.version

To write to localStorage just pass it in like so: localStorage['my_plugin_version'] = chrome.manifest.version

Victor S
  • 5,098
  • 5
  • 44
  • 62
  • 1
    Hey Victor - thanks for posting the question. I think your concerns about SE quality standards can be answered by reading up on the posting FAQ (http://stackoverflow.com/faq). It's true that we like our questions formed a certain way. We aren't trying to be jerks - we're just trying to get the question formulated very clearly, and leaving out anything that isn't relevant to the question. I've made edits to your question to make the title more clear, and the text shorter, and more to the point. That's all we're looking for - sticking strictly to the facts of the question. – jefflunt Dec 18 '11 at 01:48
  • 1
    There's been a new answer in one of the other questions you visited so long ago, and Chrome now _does_ have this feature built-in! Check out the answer [HERE](http://stackoverflow.com/a/14957674/130691), I was very happy to see how easy it is these days :) – JMTyler Sep 11 '13 at 00:41

2 Answers2

4

You can do this using a background page. When the extension is installed, the background page is opened in the background, and thus executed. To make sure it's not executed every time, simply store a value in localStorage.

Håvard
  • 9,900
  • 1
  • 41
  • 46
  • I have found another post that suggests, similar to your suggestion, to use the background page and localStorage to store the version of the plugin. Which I think will be enough for what I want to do, although I guess this means that as of yet, the Chrome API does not support something 'built-in' for this behaviour... http://stackoverflow.com/questions/4910684/how-do-i-let-my-chrome-extension-users-know-when-the-extension-is-updated – Victor S Dec 18 '11 at 02:26
  • I don't know how to access localStorage from the background page. As far as I know, localStorage can only be accessed from a content script. The problem here is that to get the version number from the background page to the content script requires the use of chrome API's to execute scripts ```chrome.tabs.executeScript(null, {code:function_to_execute}, function() { // callback });```. The problem now is, that when you install a plugin, and the page that this plugin needs to inject a content script into is already loaded, it does not inject the content script. – Victor S Dec 18 '11 at 04:03
  • You can access `localStorage` in the background page. Did you even try? – Håvard Dec 18 '11 at 13:31
  • Ah, but you mean the localStorage of the background page, not of the content script... – Victor S Dec 18 '11 at 20:31
1

As of 2023, when I am writing this , the onInstalled event provided in chrome.runtime is the most reliable method to do this.

chrome.runtime.onInstalled.addListener(
  callback: function,
)

as per the documentation of onInstalled:

onInstalled fired when the extension is first installed, when the extension is updated to a new version, and when Chrome is updated to a new version.

you can further improve your logic by checking the reason [chrome.runtime.OnInstalledReason] in you callback parameter. It specifies whether it was an install , update, chrome_update or a shared_module_update

Here is an example code that you can try out (just paste this in to your background script)

chrome.runtime.onInstalled.addListener((detail)=>{
    if (detail.reason === chrome.runtime.OnInstalledReason.UPDATE) {
      console.log("Hello, I have just been updated!");
    }
  }
);
Tharaka Deshan
  • 1,349
  • 3
  • 15
  • 30