Dev : Extensions : install.js: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
No edit summary
(I should have used <pre> here.)
Line 14: Line 14:


==Code==
==Code==
// XpiInstaller
<pre>// XpiInstaller
// By Pike (Heavily inspired by code from Henrik Gemal and Stephen Clavering)
// By Pike (Heavily inspired by code from Henrik Gemal and Stephen Clavering)


var XpiInstaller = {
var XpiInstaller = {
// --- Editable items begin ---
// --- Editable items begin ---
extFullName: 'Sample Extension', // The name displayed to the user (don't include the version)
extFullName: 'Sample Extension', // The name displayed to the user (don't include the version)
extShortName: 'sampleext', // The leafname of the JAR file (without the .jar part)
extShortName: 'sampleext', // The leafname of the JAR file (without the .jar part)
extVersion: '1.0',
extVersion: '1.0',
extAuthor: 'Insert-your-name-here',
extAuthor: 'Insert-your-name-here',
extLocaleNames: null, // e.g. ['en-US', 'en-GB']
extLocaleNames: null, // e.g. ['en-US', 'en-GB']
extSkinNames: null, // e.g. ['classic', 'modern']
extSkinNames: null, // e.g. ['classic', 'modern']
extPostInstallMessage: null, // Set to null for no post-install message
extPostInstallMessage: null, // Set to null for no post-install message
// --- Editable items end ---
// --- Editable items end ---
profileInstall: true,
profileInstall: true,
silentInstall: false,
silentInstall: false,
install: function()
install: function()
{
{
var jarName = this.extShortName + '.jar';
var jarName = this.extShortName + '.jar';
var profileDir = Install.getFolder('Profile', 'chrome');
var profileDir = Install.getFolder('Profile', 'chrome');
// Parse HTTP arguments
// Parse HTTP arguments
this.parseArguments();
this.parseArguments();
// Check if extension is already installed in profile
// Check if extension is already installed in profile
if (File.exists(Install.getFolder(profileDir, jarName)))
if (File.exists(Install.getFolder(profileDir, jarName)))
{
{
if (!this.silentInstall)
if (!this.silentInstall)
{
{
Install.alert('Updating existing Profile install of ' + this.extFullName + ' to version ' + this.extVersion + '.');
Install.alert('Updating existing Profile install of ' + this.extFullName + ' to version ' + this.extVersion + '.');
}
}
this.profileInstall = true;
this.profileInstall = true;
}
}
else if (!this.silentInstall)
else if (!this.silentInstall)
{
{
// Ask user for install location, profile or browser dir?
// Ask user for install location, profile or browser dir?
this.profileInstall = Install.confirm('Install ' + this.extFullName + ' ' + this.extVersion + ' to your Profile directory (OK) or your Browser directory (Cancel)?');
this.profileInstall = Install.confirm('Install ' + this.extFullName + ' ' + this.extVersion + ' to your Profile directory (OK) or your Browser directory (Cancel)?');
}
}
// Init install
// Init install
var dispName = this.extFullName + ' ' + this.extVersion;
var dispName = this.extFullName + ' ' + this.extVersion;
var regName = '/' + this.extAuthor + '/' + this.extShortName;
var regName = '/' + this.extAuthor + '/' + this.extShortName;
Install.initInstall(dispName, regName, this.extVersion);
Install.initInstall(dispName, regName, this.extVersion);
// Find directory to install into
// Find directory to install into
var installPath;
var installPath;
if (this.profileInstall) installPath = profileDir;
if (this.profileInstall) installPath = profileDir;
else installPath = Install.getFolder('chrome');
else installPath = Install.getFolder('chrome');
// Add JAR file
// Add JAR file
Install.addFile(null, jarName, installPath, null);
Install.addFile(null, 'chrome/' + jarName, installPath, null);
// Register chrome
// Register chrome
var jarPath = Install.getFolder(installPath, jarName);
var jarPath = Install.getFolder(installPath, jarName);
var installType = this.profileInstall ? Install.PROFILE_CHROME : Install.DELAYED_CHROME;
var installType = this.profileInstall ? Install.PROFILE_CHROME : Install.DELAYED_CHROME;
// Register content
// Register content
Install.registerChrome(Install.CONTENT | installType, jarPath, 'content/' + this.extShortName + '/');
Install.registerChrome(Install.CONTENT | installType, jarPath, 'content/' + this.extShortName + '/');
// Register locales
// Register locales
for (var locale in this.extLocaleNames)
for (var locale in this.extLocaleNames)
{
{
var regPath = 'locale/' + this.extLocaleNames[locale] + '/' + this.extShortName + '/';
var regPath = 'locale/' + this.extLocaleNames[locale] + '/' + this.extShortName + '/';
Install.registerChrome(Install.LOCALE | installType, jarPath, regPath);
Install.registerChrome(Install.LOCALE | installType, jarPath, regPath);
}
}
// Register skins
// Register skins
for (var skin in this.extSkinNames)
for (var skin in this.extSkinNames)
{
{
var regPath = 'skin/' + this.extSkinNames[skin] + '/' + this.extShortName + '/';
var regPath = 'skin/' + this.extSkinNames[skin] + '/' + this.extShortName + '/';
Install.registerChrome(Install.SKIN | installType, jarPath, regPath);
Install.registerChrome(Install.SKIN | installType, jarPath, regPath);
}
}
// Perform install
// Perform install
var err = Install.performInstall();
var err = Install.performInstall();
if (err == Install.SUCCESS || err == Install.REBOOT_NEEDED)
if (err == Install.SUCCESS || err == Install.REBOOT_NEEDED)
{
{
if (!this.silentInstall && this.extPostInstallMessage)
if (!this.silentInstall && this.extPostInstallMessage)
{
{
Install.alert(this.extPostInstallMessage);
Install.alert(this.extPostInstallMessage);
}
}
}
}
else
else
{
{
this.handleError(err);
this.handleError(err);
return;
return;
}
}
},
},
parseArguments: function()
parseArguments: function()
{
{
// Can't use string handling in install, so use if statement instead
// Can't use string handling in install, so use if statement instead
var args = Install.arguments;
var args = Install.arguments;
if (args == 'p=0')
if (args == 'p=0')
{
{
this.profileInstall = false;
this.profileInstall = false;
this.silentInstall = true;
this.silentInstall = true;
}
}
else if (args == 'p=1')
else if (args == 'p=1')
{
{
this.profileInstall = true;
this.profileInstall = true;
this.silentInstall = true;
this.silentInstall = true;
}
}
},
},
handleError: function(err)
handleError: function(err)
{
{
if (!this.silentInstall)
if (!this.silentInstall)
{
{
Install.alert('Error: Could not install ' + this.extFullName + ' ' + this.extVersion + ' (Error code: ' + err + ')');
Install.alert('Error: Could not install ' + this.extFullName + ' ' + this.extVersion + ' (Error code: ' + err + ')');
}
}
Install.cancelInstall(err);
Install.cancelInstall(err);
}
}
};
};
 
XpiInstaller.install();
XpiInstaller.install();</pre>

Revision as of 08:30, 27 July 2004

To make your extension install in Mozilla Suite, Mozilla Firefox pre-0.9 and Mozilla Thunderbird pre-0.7, you need to have an install.js file in the root of your XPI file. If you want your extension install in Firefox 0.9 and later and/or Thunderbird 0.7 and later only, you don't need install.js, you need an install.rdf instead.

XPI file structure

You can use the code below (taken from Pike's extensions [1]). It assumes the following structure of your XPI file:

sampleext.xpi
  chrome\
    sampleext.jar
      content\
        ...
      locale\  (optional)
      skin\    (optional)
  install.js
  install.rdf (optional -- see above)

Code

// XpiInstaller
// By Pike (Heavily inspired by code from Henrik Gemal and Stephen Clavering)

var XpiInstaller = {
	// --- Editable items begin ---
	extFullName: 'Sample Extension', // The name displayed to the user (don't include the version)
	extShortName: 'sampleext', // The leafname of the JAR file (without the .jar part)
	extVersion: '1.0',
	extAuthor: 'Insert-your-name-here',
	extLocaleNames: null, // e.g. ['en-US', 'en-GB']
	extSkinNames: null, // e.g. ['classic', 'modern']
	extPostInstallMessage: null, // Set to null for no post-install message
	// --- Editable items end ---
	
	profileInstall: true,
	silentInstall: false,
	
	install: function()
	{
		var jarName = this.extShortName + '.jar';
		var profileDir = Install.getFolder('Profile', 'chrome');
		
		// Parse HTTP arguments
		this.parseArguments();
		
		// Check if extension is already installed in profile
		if (File.exists(Install.getFolder(profileDir, jarName)))
		{
			if (!this.silentInstall)
			{
				Install.alert('Updating existing Profile install of ' + this.extFullName + ' to version ' + this.extVersion + '.');
			}
			this.profileInstall = true;
		}
		else if (!this.silentInstall)
		{
			// Ask user for install location, profile or browser dir?
			this.profileInstall = Install.confirm('Install ' + this.extFullName + ' ' + this.extVersion + ' to your Profile directory (OK) or your Browser directory (Cancel)?');
		}
		
		// Init install
		var dispName = this.extFullName + ' ' + this.extVersion;
		var regName = '/' + this.extAuthor + '/' + this.extShortName;
		Install.initInstall(dispName, regName, this.extVersion);
		
		// Find directory to install into
		var installPath;
		if (this.profileInstall) installPath = profileDir;
		else installPath = Install.getFolder('chrome');
		
		// Add JAR file
		Install.addFile(null, 'chrome/' + jarName, installPath, null);
		
		// Register chrome
		var jarPath = Install.getFolder(installPath, jarName);
		var installType = this.profileInstall ? Install.PROFILE_CHROME : Install.DELAYED_CHROME;
		
		// Register content
		Install.registerChrome(Install.CONTENT | installType, jarPath, 'content/' + this.extShortName + '/');
		
		// Register locales
		for (var locale in this.extLocaleNames)
		{
			var regPath = 'locale/' + this.extLocaleNames[locale] + '/' + this.extShortName + '/';
			Install.registerChrome(Install.LOCALE | installType, jarPath, regPath);
		}
		
		// Register skins
		for (var skin in this.extSkinNames)
		{
			var regPath = 'skin/' + this.extSkinNames[skin] + '/' + this.extShortName + '/';
			Install.registerChrome(Install.SKIN | installType, jarPath, regPath);
		}
		
		// Perform install
		var err = Install.performInstall();
		if (err == Install.SUCCESS || err == Install.REBOOT_NEEDED)
		{
			if (!this.silentInstall && this.extPostInstallMessage)
			{
				Install.alert(this.extPostInstallMessage);
			}
		}
		else
		{
			this.handleError(err);
			return;
		}
	},
	
	parseArguments: function()
	{
		// Can't use string handling in install, so use if statement instead
		var args = Install.arguments;
		if (args == 'p=0')
		{
			this.profileInstall = false;
			this.silentInstall = true;
		}
		else if (args == 'p=1')
		{
			this.profileInstall = true;
			this.silentInstall = true;
		}
	},
	
	handleError: function(err)
	{
		if (!this.silentInstall)
		{
			Install.alert('Error: Could not install ' + this.extFullName + ' ' + this.extVersion + ' (Error code: ' + err + ')');
		}
		Install.cancelInstall(err);
	}
};

XpiInstaller.install();