2009-07-21 00:03:33 -04:00
|
|
|
/*
|
|
|
|
Extensions Compatibility Library
|
|
|
|
|
|
|
|
Usage:
|
2010-07-02 04:39:24 -04:00
|
|
|
Asynchronus:
|
|
|
|
var extensions = window['piro.sakura.ne.jp'].extensions;
|
|
|
|
extensions.isAvailable('my.extension.id@example.com', {
|
|
|
|
ok : function() { extensions.goToOptions('my.extension.id@example.com'); },
|
|
|
|
ng : function() { alert('NOT INSTALLED'); }
|
|
|
|
});
|
|
|
|
// just same to:
|
|
|
|
// extensions.isInstalled('my.extension.id@example.com', {
|
|
|
|
// ok : function() {
|
|
|
|
// extensions.isEnabled('my.extension.id@example.com', {
|
|
|
|
// ok : function() { extensions.goToOptions('my.extension.id@example.com'); }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
extensions.isInstalled('my.extension.id@example.com', {
|
|
|
|
ok : function(aDir) {
|
|
|
|
var dir = aDir; // nsILocalFile
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Synchronus: (DEPRECATED)
|
|
|
|
if (extensions.isAvailable('my.extension.id@example.com'))
|
|
|
|
extensions.goToOptions('my.extension.id@example.com');
|
|
|
|
// just same to:
|
|
|
|
// if (extensions.isInstalled('my.extension.id@example.com') &&
|
|
|
|
// extensions.isEnabled('my.extension.id@example.com'))
|
|
|
|
// extensions.goToOptions('my.extension.id@example.com');
|
|
|
|
var dir = extensions.getInstalledLocation('my.extension.id@example.com'); // nsILocalFile
|
2009-07-21 00:03:33 -04:00
|
|
|
|
2010-05-02 00:06:54 -04:00
|
|
|
lisence: The MIT License, Copyright (c) 2009-2010 SHIMODA "Piro" Hiroshi
|
2009-07-21 00:03:33 -04:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
|
|
|
|
original:
|
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/extensions.js
|
|
|
|
*/
|
2010-06-22 19:46:51 -04:00
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
/* To work as a JS Code Module */
|
2010-06-22 21:32:31 -04:00
|
|
|
if (typeof window == 'undefined') {
|
|
|
|
this.EXPORTED_SYMBOLS = ['extensions'];
|
2010-06-22 19:46:51 -04:00
|
|
|
|
2010-06-26 11:13:11 -04:00
|
|
|
// If namespace.jsm is available, export symbols to the shared namespace.
|
|
|
|
// See: http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/namespace.jsm
|
|
|
|
try {
|
|
|
|
let ns = {};
|
|
|
|
Components.utils.import('resource://treestyletab-modules/namespace.jsm', ns);
|
|
|
|
/* var */ window = ns.getNamespaceFor('piro.sakura.ne.jp');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
window = {};
|
|
|
|
}
|
2010-06-22 19:46:51 -04:00
|
|
|
}
|
|
|
|
|
2009-07-21 00:03:33 -04:00
|
|
|
(function() {
|
2010-08-11 12:55:28 -04:00
|
|
|
const currentRevision = 11;
|
2009-07-21 00:03:33 -04:00
|
|
|
|
|
|
|
if (!('piro.sakura.ne.jp' in window)) window['piro.sakura.ne.jp'] = {};
|
|
|
|
|
|
|
|
var loadedRevision = 'extensions' in window['piro.sakura.ne.jp'] ?
|
|
|
|
window['piro.sakura.ne.jp'].extensions.revision :
|
|
|
|
0 ;
|
|
|
|
if (loadedRevision && loadedRevision > currentRevision) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-02 00:06:54 -04:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
// Firefox 3.7 or later
|
|
|
|
var AM = {};
|
2010-06-22 14:00:16 -04:00
|
|
|
if ('@mozilla.org/addons/integration;1' in Cc)
|
2010-05-02 00:06:54 -04:00
|
|
|
Components.utils.import('resource://gre/modules/AddonManager.jsm', AM);
|
|
|
|
|
2009-07-21 00:03:33 -04:00
|
|
|
window['piro.sakura.ne.jp'].extensions = {
|
|
|
|
revision : currentRevision,
|
|
|
|
|
2010-05-02 00:06:54 -04:00
|
|
|
// Firefox 3.7 or later
|
2010-08-11 12:55:28 -04:00
|
|
|
_getInstalledAddonNow : function(aId)
|
2010-05-02 00:06:54 -04:00
|
|
|
{
|
|
|
|
var addon;
|
|
|
|
AM.AddonManager.getAddonByID(aId, function(aAddon) {
|
|
|
|
addon = aAddon;
|
|
|
|
});
|
2010-06-22 14:00:16 -04:00
|
|
|
var thread = Cc['@mozilla.org/thread-manager;1']
|
2010-05-02 00:06:54 -04:00
|
|
|
.getService()
|
|
|
|
.mainThread;
|
|
|
|
while (addon === void(0)) {
|
|
|
|
thread.processNextEvent(true);
|
|
|
|
}
|
|
|
|
return addon;
|
|
|
|
},
|
|
|
|
|
2010-07-02 04:56:28 -04:00
|
|
|
_formatCallbacks : function(aOKCallback, aNGCallback)
|
2010-07-02 04:39:24 -04:00
|
|
|
{
|
|
|
|
var callbacks = {
|
|
|
|
ok : aOKCallback,
|
|
|
|
ng : aNGCallback
|
|
|
|
};
|
|
|
|
if (typeof aOKCallback != 'function')
|
|
|
|
callbacks = aOKCallback;
|
2010-07-02 04:51:07 -04:00
|
|
|
|
|
|
|
if (!('ok' in callbacks) || typeof callbacks.ok != 'function')
|
|
|
|
callbacks.ok = function() {};
|
|
|
|
if (!('ng' in callbacks) || typeof callbacks.ng != 'function')
|
|
|
|
callbacks.ng = function() {};
|
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
return callbacks;
|
|
|
|
},
|
|
|
|
|
2010-05-02 00:06:54 -04:00
|
|
|
// Firefox 3.6 or older
|
2010-07-02 04:39:24 -04:00
|
|
|
_ExtensionManager : ('@mozilla.org/extensions/manager;1' in Cc) ?
|
2010-05-02 00:06:54 -04:00
|
|
|
Cc['@mozilla.org/extensions/manager;1']
|
|
|
|
.getService(Ci.nsIExtensionManager) :
|
|
|
|
null,
|
2010-07-02 04:39:24 -04:00
|
|
|
_RDF : Cc['@mozilla.org/rdf/rdf-service;1']
|
2010-05-02 00:06:54 -04:00
|
|
|
.getService(Ci.nsIRDFService),
|
2010-07-02 04:39:24 -04:00
|
|
|
_WindowMediator : Cc['@mozilla.org/appshell/window-mediator;1']
|
2010-05-02 00:06:54 -04:00
|
|
|
.getService(Ci.nsIWindowMediator),
|
2010-07-02 04:39:24 -04:00
|
|
|
_Prefs : Cc['@mozilla.org/preferences;1']
|
2010-05-02 00:06:54 -04:00
|
|
|
.getService(Ci.nsIPrefBranch),
|
2009-07-21 00:03:33 -04:00
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
|
|
|
|
isAvailable : function(aId, aOKCallback, aNGCallback)
|
2009-07-21 00:18:37 -04:00
|
|
|
{
|
2010-08-11 12:55:28 -04:00
|
|
|
return this.isEnabled(aId, aOKCallback, aNGCallback);
|
2009-07-21 00:18:37 -04:00
|
|
|
},
|
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
|
|
|
|
isInstalled : function(aId, aOKCallback, aNGCallback)
|
2009-07-21 00:03:33 -04:00
|
|
|
{
|
2010-07-02 04:39:24 -04:00
|
|
|
if (!aOKCallback)
|
|
|
|
return this._ExtensionManager ? this._isInstalled_EM(aId) : this._isInstalled_AM(aId) ;
|
|
|
|
|
|
|
|
var callbacks = this._formatCallbacks(aOKCallback, aNGCallback);
|
|
|
|
if (this._ExtensionManager) {
|
2010-08-11 12:55:28 -04:00
|
|
|
callbacks[this._isInstalled_EM(aId) ? 'ok' : 'ng']();
|
2010-07-02 04:39:24 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AM.AddonManager.getAddonByID(aId, function(aAddon) {
|
2010-08-11 12:55:28 -04:00
|
|
|
callbacks[aAddon ? 'ok' : 'ng']();
|
2010-07-02 04:39:24 -04:00
|
|
|
});
|
|
|
|
}
|
2010-05-02 00:06:54 -04:00
|
|
|
},
|
2010-07-02 04:39:24 -04:00
|
|
|
_isInstalled_EM : function(aId)
|
2010-05-02 00:06:54 -04:00
|
|
|
{
|
2010-07-02 04:39:24 -04:00
|
|
|
return this._ExtensionManager.getInstallLocation(aId) ? true : false ;
|
2010-05-02 00:06:54 -04:00
|
|
|
},
|
2010-07-02 04:39:24 -04:00
|
|
|
_isInstalled_AM : function(aId)
|
2010-05-02 00:06:54 -04:00
|
|
|
{
|
2010-08-11 12:55:28 -04:00
|
|
|
return this._getInstalledAddonNow(aId) ? true : false ;
|
2009-07-21 00:03:33 -04:00
|
|
|
},
|
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
|
|
|
|
isEnabled : function(aId, aOKCallback, aNGCallback)
|
2010-05-02 00:06:54 -04:00
|
|
|
{
|
2010-07-02 04:39:24 -04:00
|
|
|
if (!aOKCallback)
|
|
|
|
return this._ExtensionManager ? this._isEnabled_EM(aId) : this._isEnabled_AM(aId) ;
|
|
|
|
|
|
|
|
var callbacks = this._formatCallbacks(aOKCallback, aNGCallback);
|
|
|
|
if (this._ExtensionManager) {
|
2010-08-11 12:55:28 -04:00
|
|
|
callbacks[this._isEnabled_EM(aId) ? 'ok' : 'ng']();
|
2010-07-02 04:39:24 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-08-11 12:55:28 -04:00
|
|
|
AM.AddonManager.getAddonByID(aId, function(aAddon) {
|
|
|
|
callbacks[aAddon && aAddon.isActive ? 'ok' : 'ng']();
|
|
|
|
});
|
2010-07-02 04:39:24 -04:00
|
|
|
}
|
2010-05-02 00:06:54 -04:00
|
|
|
},
|
2010-07-02 04:39:24 -04:00
|
|
|
_isEnabled_EM : function(aId)
|
2009-07-21 00:03:33 -04:00
|
|
|
{
|
2010-08-11 12:55:28 -04:00
|
|
|
if (!this._isInstalled_EM(aId))
|
|
|
|
return false;
|
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
var res = this._RDF.GetResource('urn:mozilla:item:'+aId);
|
2009-07-21 00:03:33 -04:00
|
|
|
var appDisabled = false;
|
|
|
|
try {
|
2010-07-02 04:39:24 -04:00
|
|
|
appDisabled = this._ExtensionManager.datasource.GetTarget(
|
2009-07-21 00:03:33 -04:00
|
|
|
res,
|
2010-07-02 04:39:24 -04:00
|
|
|
this._RDF.GetResource('http://www.mozilla.org/2004/em-rdf#appDisabled'),
|
2009-07-21 00:03:33 -04:00
|
|
|
true
|
2010-05-02 00:06:54 -04:00
|
|
|
).QueryInterface(Ci.nsIRDFLiteral)
|
2009-07-21 00:03:33 -04:00
|
|
|
.Value == 'true';
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
var userDisabled = false;
|
|
|
|
try {
|
2010-07-02 04:39:24 -04:00
|
|
|
userDisabled = this._ExtensionManager.datasource.GetTarget(
|
2009-07-21 00:03:33 -04:00
|
|
|
res,
|
2010-07-02 04:39:24 -04:00
|
|
|
this._RDF.GetResource('http://www.mozilla.org/2004/em-rdf#userDisabled'),
|
2009-07-21 00:03:33 -04:00
|
|
|
true
|
2010-05-02 00:06:54 -04:00
|
|
|
).QueryInterface(Ci.nsIRDFLiteral)
|
2009-07-21 00:03:33 -04:00
|
|
|
.Value == 'true';
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return !appDisabled && !userDisabled;
|
|
|
|
},
|
2010-07-02 04:39:24 -04:00
|
|
|
_isEnabled_AM : function(aId)
|
2010-05-02 00:06:54 -04:00
|
|
|
{
|
2010-08-11 12:55:28 -04:00
|
|
|
var addon = this._getInstalledAddonNow(aId);
|
|
|
|
return addon ? addon.isActive : false ;
|
2010-05-02 00:06:54 -04:00
|
|
|
},
|
2009-07-21 00:03:33 -04:00
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
|
|
|
|
getInstalledLocation : function(aId, aCallback)
|
|
|
|
{
|
|
|
|
if (!aCallback)
|
|
|
|
return this._ExtensionManager ? this._getInstalledLocation_EM(aId) : this._getInstalledLocation_AM(aId) ;
|
|
|
|
|
|
|
|
if (this._ExtensionManager) {
|
|
|
|
aCallback(this._getInstalledLocation_EM(aId));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AM.AddonManager.getAddonByID(aId, function(aAddon) {
|
|
|
|
var location = null;
|
2010-08-11 12:55:28 -04:00
|
|
|
if (aAddon)
|
2010-07-02 04:39:24 -04:00
|
|
|
location = aAddon.getResourceURI('/').QueryInterface(Ci.nsIFileURL).file.clone();
|
|
|
|
aCallback(location);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_getInstalledLocation_EM : function(aId)
|
|
|
|
{
|
|
|
|
var addon = this._ExtensionManager.getInstallLocation(aId);
|
2010-08-11 12:55:28 -04:00
|
|
|
if (!addon) return null;
|
|
|
|
return addon.getItemFile(aId, '').clone();
|
2010-07-02 04:39:24 -04:00
|
|
|
},
|
|
|
|
_getInstalledLocation_AM : function(aId)
|
|
|
|
{
|
2010-08-11 12:55:28 -04:00
|
|
|
var addon = this._getInstalledAddonNow(aId);
|
|
|
|
if (!addon) return null;
|
2010-07-07 08:27:24 -04:00
|
|
|
return addon.getResourceURI('/').QueryInterface(Ci.nsIFileURL).file.clone();
|
2010-07-02 04:39:24 -04:00
|
|
|
},
|
|
|
|
|
2010-08-11 12:55:28 -04:00
|
|
|
getVersion : function(aId, aCallback)
|
|
|
|
{
|
|
|
|
if (!aCallback)
|
|
|
|
return this._ExtensionManager ? this._getVersion_EM(aId) : this._getVersion_AM(aId) ;
|
|
|
|
|
|
|
|
if (this._ExtensionManager) {
|
|
|
|
aCallback(this._getVersion_EM(aId));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AM.AddonManager.getAddonByID(aId, function(aAddon) {
|
|
|
|
aCallback(aAddon ? aAddon.version : null );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_getVersion_EM : function(aId)
|
|
|
|
{
|
|
|
|
if (!this._isInstalled_EM(aId))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
var res = this._RDF.GetResource('urn:mozilla:item:'+aId);
|
|
|
|
var version = null;
|
|
|
|
try {
|
|
|
|
version = this._ExtensionManager.datasource.GetTarget(
|
|
|
|
res,
|
|
|
|
this._RDF.GetResource('http://www.mozilla.org/2004/em-rdf#version'),
|
|
|
|
true
|
|
|
|
).QueryInterface(Ci.nsIRDFLiteral)
|
|
|
|
.Value;
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
},
|
|
|
|
_getVersion_AM : function(aId)
|
|
|
|
{
|
|
|
|
var addon = this._getInstalledAddonNow(aId);
|
|
|
|
return addon ? addon.version : null ;
|
|
|
|
},
|
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
|
2010-06-22 14:00:16 -04:00
|
|
|
goToOptions : function(aId, aOwnerWindow)
|
2009-07-21 00:03:33 -04:00
|
|
|
{
|
2010-07-02 04:39:24 -04:00
|
|
|
var self = this;
|
|
|
|
var callback = function(aURI) {
|
|
|
|
self.goToOptionsInternal(aURI, aOwnerWindow);
|
|
|
|
};
|
2010-08-11 12:55:28 -04:00
|
|
|
return this._ExtensionManager ? this._getOptionsURI_EM(aId, callback) : this._getOptionsURI_AM(aId, callback);
|
2010-07-02 04:39:24 -04:00
|
|
|
},
|
|
|
|
_getOptionsURI_EM : function(aId, aCallback)
|
|
|
|
{
|
|
|
|
var res = this._RDF.GetResource('urn:mozilla:item:'+aId);
|
|
|
|
var uri = null;
|
|
|
|
try {
|
|
|
|
uri = this._ExtensionManager.datasource.GetTarget(
|
|
|
|
res,
|
|
|
|
this._RDF.GetResource('http://www.mozilla.org/2004/em-rdf#optionsURL'),
|
|
|
|
true
|
|
|
|
).QueryInterface(Ci.nsIRDFLiteral)
|
|
|
|
.Value;
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
return aCallback ? aCallback(uri) : uri ;
|
|
|
|
},
|
|
|
|
_getOptionsURI_AM : function(aId, aCallback)
|
|
|
|
{
|
|
|
|
if (aCallback) {
|
|
|
|
AM.AddonManager.getAddonByID(aId, function(aAddon) {
|
|
|
|
aCallback(aAddon && aAddon.isActive ? aAddon.optionsURL : null );
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
2010-08-11 12:55:28 -04:00
|
|
|
var addon = this._getInstalledAddonNow(aId);
|
2010-07-02 04:39:24 -04:00
|
|
|
return (addon && addon.isActive) ? addon.optionsURL : null ;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
goToOptionsNow : function(aId, aOwnerWindow)
|
|
|
|
{
|
|
|
|
this.goToOptionsInternal(
|
|
|
|
(this._ExtensionManager ? this._getOptionsURI_EM(aId) : this._getOptionsURI_AM(aId) ),
|
|
|
|
aOwnerWindow
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
goToOptionsInternal : function(aURI, aOwnerWindow)
|
|
|
|
{
|
|
|
|
if (!aURI) return;
|
2009-07-21 00:03:33 -04:00
|
|
|
|
2010-07-02 04:39:24 -04:00
|
|
|
var windows = this._WindowMediator.getEnumerator(null);
|
2009-07-21 00:03:33 -04:00
|
|
|
while (windows.hasMoreElements())
|
|
|
|
{
|
|
|
|
let win = windows.getNext();
|
|
|
|
if (win.location.href == uri) {
|
|
|
|
win.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var instantApply = false;
|
|
|
|
try {
|
2010-07-02 04:39:24 -04:00
|
|
|
instantApply = this._Prefs.getBoolPref('browser.preferences.instantApply');
|
2009-07-21 00:03:33 -04:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
2010-06-22 14:00:16 -04:00
|
|
|
(aOwnerWindow || window).openDialog(
|
2009-07-21 00:03:33 -04:00
|
|
|
uri,
|
|
|
|
'',
|
|
|
|
'chrome,titlebar,toolbar,centerscreen,' + (instantApply ? 'dialog=no' : 'modal' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2010-06-22 19:46:51 -04:00
|
|
|
|
|
|
|
if (window != this) { // work as a JS Code Module
|
2010-06-22 21:32:31 -04:00
|
|
|
this.extensions = window['piro.sakura.ne.jp'].extensions;
|
2010-06-22 19:46:51 -04:00
|
|
|
}
|