An API to detect if a Chrome Extension has updated

As you might have been able to tell from my previous posts, I am a bit of a nut when it comes to the Management API in the Chrome Extension framework.
I got asked a question recently about detecting if a Chrome Extension has been updated. The good news is that there is an API for that. The bad news is that I told a little fib: there isn’t actually a specific API for detecting if an Extension/App had been updated.


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan

<p>As you might have been able to tell from my previous posts, I am a bit of a nut when it comes to the <a href="http://code.google.com/chrome/extensions/management.html">Management API</a> in the Chrome Extension framework.</p> <p>I got asked a question recently about detecting if a Chrome Extension has been updated. The good news is that there is an API for that. The bad news is that I told a little fib: there isn't actually a specific API for detecting if an Extension/App had been updated.</p> <p>Lets get back on to the Good News, you can detect if an Extension/App has been updated by listening to the <a href="http://code.google.com/chrome/extensions/management.html#event-onInstalled">onInstalled</a> event triggered by Chrome. Every time an extension is installed (or updated) this event gets fired.</p> <p>The important thing to remember is that it doesn't tell you directly if the installation was an update, so the thing that you need to do is first get a list of all the extensions installed by the user and then track the version number in onInstalled.</p> <p>Here is a neat little sample of it in action:</p> <div class="CodeRay"> <div class="code"><pre><span class="comment">/* Track all the version numbers (background.html) in your background page */</span> <span class="keyword">var</span> extensions = {}; <span class="keyword">var</span> <span class="function">getAllCallback</span> = <span class="keyword">function</span>(exts) { <span class="keyword">for</span>(<span class="keyword">var</span> i <span class="keyword">in</span> exts) { <span class="keyword">var</span> ext = exts[i]; extensions[ext.id] = ext; <span class="comment">// store a reference to the app</span> } }; <p>chrome.management.getAll(getAllCallback);</pre></div></p> </div> <p>Now that we have a list of the apps, we can listen for installatons. In the handler, we are only going notify they user if the extension/app is not new and the version has changed. An extension is not new if we already know about it.</p> <div class="CodeRay"> <div class="code"><pre><span class="keyword">var</span> <span class="function">onInstall</span> = <span class="keyword">function</span>(ext) { <span class="keyword">var</span> id = ext.id; <p><span class="keyword">if</span>(extensions[id] && extensions[id].version != ext.version) { chrome.browserAction.setBadgeText({<span class="key"><span class="delimiter">"</span><span class="content">text</span><span class="delimiter">"</span></span> :<span class="string"><span class="delimiter">"</span><span class="content">New</span><span class="delimiter">"</span></span> }); <span class="comment">// tell the user </span> extensions[id] = ext; <span class="comment">// track the extension</span> } };</pre></div></p> </div> <p>Cool.</p>


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan


Print Share Comment Cite Upload Translate Updates
APA

Paul Kinlan | Sciencx (2010-12-15T00:00:00+00:00) An API to detect if a Chrome Extension has updated. Retrieved from https://www.scien.cx/2010/12/15/an-api-to-detect-if-a-chrome-extension-has-updated/

MLA
" » An API to detect if a Chrome Extension has updated." Paul Kinlan | Sciencx - Wednesday December 15, 2010, https://www.scien.cx/2010/12/15/an-api-to-detect-if-a-chrome-extension-has-updated/
HARVARD
Paul Kinlan | Sciencx Wednesday December 15, 2010 » An API to detect if a Chrome Extension has updated., viewed ,<https://www.scien.cx/2010/12/15/an-api-to-detect-if-a-chrome-extension-has-updated/>
VANCOUVER
Paul Kinlan | Sciencx - » An API to detect if a Chrome Extension has updated. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2010/12/15/an-api-to-detect-if-a-chrome-extension-has-updated/
CHICAGO
" » An API to detect if a Chrome Extension has updated." Paul Kinlan | Sciencx - Accessed . https://www.scien.cx/2010/12/15/an-api-to-detect-if-a-chrome-extension-has-updated/
IEEE
" » An API to detect if a Chrome Extension has updated." Paul Kinlan | Sciencx [Online]. Available: https://www.scien.cx/2010/12/15/an-api-to-detect-if-a-chrome-extension-has-updated/. [Accessed: ]
rf:citation
» An API to detect if a Chrome Extension has updated | Paul Kinlan | Sciencx | https://www.scien.cx/2010/12/15/an-api-to-detect-if-a-chrome-extension-has-updated/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.