Skip to content

Instantly share code, notes, and snippets.

@yzen
Created April 9, 2015 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yzen/ce55479d7518743f11f6 to your computer and use it in GitHub Desktop.
Save yzen/ce55479d7518743f11f6 to your computer and use it in GitHub Desktop.
function renameAddon(addon, name) {
if (!name) {
return Promise.reject('no name given');
}
if (!isAddon(addon)) {
return Promise.reject('not an addon');
}
var manifest = getManifest(addon);
if (name === manifest.name) {
return Promise.reject('name is unchanged');
}
return new Promise(function(resolve, reject) {
addon.manifest.name = name;
addon.export().then(function(blob) {
var request = navigator.mozApps.mgmt.uninstall(addon);
request.onsuccess = function() {
install(blob).then(resolve).catch(reject);
};
request.onerror = function() { reject(); };
}).catch(reject);
});
}
function install(blob) {
return new Promise(function(resolve, reject) {
// Save the blob to a file because we don't support importing memory blobs
var sdcard = navigator.getDeviceStorage('sdcard');
// Delete the tempfile from the SD card.
var deleteRequest = sdcard.delete(ADDON_FILENAME);
deleteRequest.onsuccess = deleteRequest.onerror = function() {
// Add the addon blob to the SD card using the temp filename.
var addNamedRequest = sdcard.addNamed(blob, ADDON_FILENAME);
addNamedRequest.onsuccess = function() {
// Retrieve the new tempfile.
var getRequest = sdcard.get(ADDON_FILENAME);
getRequest.onsuccess = function () {
var addonFile = getRequest.result;
// Import the addon using the tempfile.
navigator.mozApps.mgmt.import(addonFile).then(function (addon) {
// Enable the addon by default.
enableAddon(addon);
resolve(addon);
}).catch(reject);
};
getRequest.onerror = function (error) { reject(error); };
};
addNamedRequest.onerror = function(error) { reject(error); };
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment