Get current active theme in #JetPack

May 25, 2011
By Bogomil Shopov
4 Comments
Post Image

I need to know ID of current active Firefox Persona in order to get the textcolor value from users.pref to be able to display different icons on my widget, because I don’t wont to display blue icon on blue background.

This tutorial shows:

      How to access Addon Manager from a JetPack script
      How to get all Addons based on their type [extension, theme, locale, multipackage]
      How to get Active Persona’s ID

It’s a lot for such short script, isn’t it?


//we need this component
const {Cu} = require("chrome");

//import AddonManager
var AddonManager = Cu.import("resource://gre/modules/AddonManager.jsm")
                              .AddonManager;

//We need to get all Addons that are with type 'theme'
var types =['theme'];

//get all themes
var currentTheme = AddonManager
                          .getAddonsByTypes(types,function(addons){
	//Firefox always shows active theme as 0 element (not sure, need confirmation)
       
       //get the ID of the active theme/persona
	var tid=addons[0].id.toString();
      
        //show it in console 
        console.log("Current Id:"+ tid);
});

Unfortunatelly textcolor value is not part of the object (yet) and the only way to find it is to check with this id (tid) in prefs.js

Bogomil Shopov

I care about privacy, ethical design, and freedom in many aspects. I spend 20+ years working as a web developer and architect, analyst, manager, and product owner/manager in different environments, several countries, and multiple software industries like Healthcare and Hospitality. I wore many hats, and I use the knowledge gathered to optimize the flow of value across complex systems.

4 Comments

  • PJ

    May 25, 2011 at 3:33 pm

    Can you get the active theme directly from prefs? anyway this is a great example for calling AddonManager – it shows jetpack is not about web-pages only technology,but can work with core Firefox JS objects.

    Reply
  • Michael Kaply

    May 25, 2011 at 3:41 pm

    Actually, you can get the LightweightThemeManager directly.

    var lwtm = Cu.import(“resource://gre/modules/LightweightThemeManager.jsm”);

    var bgcolor;
    if (lwtm.currentTheme) {
    bgcolor = lwtm.currentTheme.accentcolor;
    }

    If you need the id, lwtm.currentTheme.id

    Reply
  • Michael Kaply

    May 25, 2011 at 3:42 pm

    Actually, you want textcolor, so .textcolor.

    See:

    http://mike.kaply.com/2010/07/08/what-is-a-persona/

    For all the details about what’s in a Persona.

    Reply
  • Bogo

    May 25, 2011 at 4:00 pm

    Thanks Michael. For jetPack it should be:

    var lwtm = Cu.import('resource://gre/modules/LightweightThemeManager.jsm').LightweightThemeManager;
    
    
    Reply

Leave a reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.