The Safari extension starts first and updates

How do I know if my Safari extension code is running for the first time after the user has installed the extension?

I would like to distinguish between a new installation of an extension and an update of an extension.

I am looking for something very similar to this answer , but for Safari, not Chrome. I could not "translate" the code from the answer in this link to Safari.

+4
source share
3 answers

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; } }); 
+4
source

we can get the version from safari.extension.displayVersion

 var storedVersion = safari.extension.settings.version; var currentVersion = safari.extension.displayVersion + '.' + safari.extension.bundleVersion; if (typeof storedVersion === 'undefined') { console.log('Extension installed'); safari.extension.settings.version = currentVersion } else if (currentVersion != storedVersion) { console.log('Extension update'); safari.extension.settings.version = currentVersion } 

don't forget to add a hidden customization item to Extension Builder

+1
source

Took @Claudijo the answer above and processed it in a small class:

 /** * ExtensionState * * @abstract */ var ExtensionState = (function() { /** * __configFilePath * * @access private * @return String (default: '../Info.plist') */ var __configFilePath = '../Info.plist'; /** * __getConfigVersion * * @access private * @param Function callback * @return void */ var __getConfigVersion = function(callback) { var xmlhttp = new XMLHttpRequest(); xmlhttp.open('GET', __configFilePath); xmlhttp.onload = function () { var infoFile = xmlhttp.responseXML, 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(); }; /** * __getLocalVersion * * @access private * @return String */ var __getLocalVersion = function() { return localStorage['version']; }; /** * __putLocalVersion * * @access private * @param String version * @return void */ var __putLocalVersion = function(version) { localStorage['version'] = version; }; // Public return { /** * installed * * @access public * @param Function callback * @return void */ installed: function(callback) { var localVersion = __getLocalVersion(); if (typeof localVersion === 'undefined') { __getConfigVersion(function(version) { callback(version); __putLocalVersion(version); }); } }, /** * updated * * @access public * @param Function callback * @return void */ updated: function(callback) { var localVersion = __getLocalVersion(); if (typeof localVersion !== 'undefined') { __getConfigVersion(function(version) { if (localVersion !== version) { callback(version); __putLocalVersion(version); } }); } } }; })() ExtensionState.installed(function(version) { console.log('(global.html): Installed'); }); ExtensionState.updated(function(version) { console.log('(global.html): Updated'); }); 
0
source

Source: https://habr.com/ru/post/1403417/


All Articles