Programmatically enable/disable an extension

Talk about add-ons and extension development.
Post Reply
Yaron10
Posts: 472
Joined: July 5th, 2011, 6:32 am

Programmatically enable/disable an extension

Post by Yaron10 »

Hello,

The following code toggles (enables/disables) an extension:

Code: Select all

	
Components.utils.import("resource://gre/modules/AddonManager.jsm");

AddonManager.getAddonByID("uBlock0@raymondhill.net", function(addon) {
	  if (!addon) {
		alert("Not found!");
		return;
	  }
  addon.userDisabled = !addon.userDisabled;
});
I'm trying to enable/disable Roboform.
This is its install.rdf:

Code: Select all

<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:install-manifest">
    <em:type>2</em:type>
    <em:id>{22119944-ED35-4ab1-910B-E619EA06A115}</em:id>
    <em:name>RoboForm Toolbar</em:name>
    <em:version>7.9.23.0</em:version>
    <em:description>RoboForm is password manager and form filler for Firefox.</em:description>
    <em:creator>Siber Systems Inc.</em:creator>
    <em:homepageURL>http://www.roboform.com</em:homepageURL>
    <em:targetPlatform>WINNT</em:targetPlatform>
    <em:targetPlatform>Darwin</em:targetPlatform>
    <em:unpack>true</em:unpack>
    <em:bootstrap>true</em:bootstrap>

    <!-- Firefox -->
    <em:targetApplication>
      <Description>
        <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
        <em:minVersion>4.0</em:minVersion>
        <em:maxVersion>52.0</em:maxVersion>
      </Description>
    </em:targetApplication>
  </Description>
</RDF>
I understand I should use "AddonManager.getAddonBySyncGUID()".
The following code does not work ("Not found!").

Code: Select all

Components.utils.import("resource://gre/modules/AddonManager.jsm");

AddonManager.getAddonBySyncGUID("22119944-ED35-4ab1-910B-E619EA06A115", function(addon) {
  if (!addon) {
    alert("Not found!");
    return;
  }
  addon.userDisabled = !addon.userDisabled;
});
I'd appreciate your help.
morat
Posts: 6403
Joined: February 3rd, 2009, 6:29 pm

Re: Programmatically enable/disable an extension

Post by morat »

The id and syncGUID strings are not the same. The syncGUID string is not shown in the install.rdf file.

Code: Select all

// Greasemonkey
alert(addon.id); // {e4a8a97b-f2ed-450b-b12d-ee082ba24781}
alert(addon.syncGUID); // {c91c4126-4022-4a9e-b9f2-07f9feb10e18}
Greasemonkey
http://addons.mozilla.org/en-US/firefox ... asemonkey/

Greasemonkey install.rdf
http://github.com/greasemonkey/greasemo ... nstall.rdf

You can check the id and syncGUID strings with scratchpad in browser environment.

Instructions:

* open about:config
* set devtools.chrome.enabled to true
* tools > web developer > scratchpad
* environment > browser
* edit > paste (i.e. copy and paste code snippet)
* execute > run

Code: Select all

AddonManager.getAddonsByTypes(["extension"], function (addons) {
  var list = [];
  addons.forEach(function (addon) {
    if (addon.hidden == false) {
      list.push(addon.name + ", id: " + addon.id +
        ", syncGUID: " + addon.syncGUID);
    }
  });
  alert(list.join("\n"));
});
P.S.

And do not forget the braces in the id and syncGUID strings.
Yaron10
Posts: 472
Joined: July 5th, 2011, 6:32 am

Re: Programmatically enable/disable an extension

Post by Yaron10 »

Hello morat,

Thank you for the detailed and helpful reply. I appreciate it.

Well, before posting here I tried "AddonManager.getAddonByID" with the ID in install.rdf.
It would work with an extension having the "uBlock0@raymondhill.net" ID type but not with an extension having the "{e4a8a97b-f2ed-450b-b12d-ee082ba24781}" ID type.
I then tried "AddonManager.getAddonBySyncGUID" (with and without the braces) which as you've explained shouldn't work with the ID in install.rdf.

I suppose I only tried "AddonManager.getAddonByID" *without* the braces.
Using it now *with* the braces, it works perfectly.

***
Sorry for the late reply. I've had a busy day.

Best regards.
Post Reply