title: Chrome extension post-install hook/API function: does it exist?
body: <p>Is there a Chrome extension post install hook/API function that will let me perform an action after the plugin is installed or updated?</p>
  
  <p>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.</p>
  
  <h3>Update</h3>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>I don't know how to access localStorage from the background page; localStorage can only be accessed from a content script. </p>
  
  <p>To get the version number from the background page to the content script requires the use of chrome API function to execute scripts:</p>
  
  <p><code>chrome.tabs.executeScript(null, {code:function_to_execute}, function() { // callback });</code></p>
  
  <p>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.</p>
  
  <h3>update 2</h3>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <h2>HowTo</h2>
  
  <p>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).</p>
  
  <pre><code>// 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;
  })();
  </code></pre>
  
  <p>Now you can access your version number like this: <code>chrome.manifest.version</code></p>
  
  <p>To write to localStorage just pass it in like so: <code>localStorage['my_plugin_version'] = chrome.manifest.version</code></p>