If you can live without checking for updates, this script should work (compare with the answer related to Chrome):
// In background page function onInstall() { console.log('Extension installed'); } var firstRun = typeof localStorage['extensionHasPreviouslyRun'] === 'undefined' || !JSON.parse(localStorage['extensionHasPreviouslyRun']); if (firstrun) { onInstall(); localStorage['extensionHasPreviouslyRun'] = JSON.stringify(true); }
If you also want to check for updates, you need to get the version from the plist file asynchronously:
// In background page function onInstall() { console.log('Extension installed'); } function onUpdate() { console.log('Extension update'); } function requestVersion(callback) { var xmlhttp = new XMLHttpRequest(); xmlhttp.open('GET', 'info.plist'); xmlhttp.onload = function () { var infoFile = xmlhttp.responseXML; var keys = infoFile.getElementsByTagName('key'); for (var i = 0; i < keys.length; i++){ if (keys[i].firstChild.data === 'CFBundleShortVersionString'){ var version = keys[i].nextElementSibling.firstChild.data; callback(version); break; } } } xmlhttp.send(); } requestVersion(function(version) { var storedVersion = localStorage['version']; if (storedVersion !== version) { // Check if we just installed this extension. if (typeof storedVersion === 'undefined') { onInstall(); } else { onUpdate(); } localStorage['version'] = version; } });
source share