Skip to content

Instantly share code, notes, and snippets.

@nmaier
Created January 2, 2013 23:44
Show Gist options
  • Save nmaier/4439446 to your computer and use it in GitHub Desktop.
Save nmaier/4439446 to your computer and use it in GitHub Desktop.
experimental subskin bootstrapper (noia extreme version)
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* The original author is: Nils Maier <https://tn123.org/> */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
const lazySvc = XPCOMUtils.defineLazyServiceGetter.bind(XPCOMUtils);
Services = Object.create(Services);
lazySvc(Services, "sss", "@mozilla.org/content/style-sheet-service;1", "nsIStyleSheetService");
const THEME = "noia2_full@gd.noia";
const COMPANION = "noia2_full_xt@gd.noia";
const PREF_BRANCH = "extensions.noia2_full_v3_xt.";
const BASE_URI = "chrome://noia2_full_v3_xt_options/content/";
const BOOL_SHEETS = ["collapse", "newofficexp", "roundtabs", "personas", "noHead", "Cleaner", "BlueDropmarker", "inverse-size", "no-zebra", "reset-inv-bars"];
const RADIO_SHEETS = {
"color": ["grey", "dark", "lightdark", "blue"],
"throbber": ["stdThrobber", "bigThrobber", "newThrobber", "defThrobber", "defThrobberBlue"]
};
const Observer = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
observe: function(subject, topic, data) {
reload();
}
};
let dead = false;
let branch = null;
let applied = Object.create(null);
function install() {}
function uninstall() {}
function startup() {
// 1. Check the corresponding theme is installed
AddonManager.getAddonByID(THEME, function queryTheme(theme) {
// 1.1. If not, uninstall ourselves and exit early
if (!theme) {
AddonManager.getAddonByID(COMPANION, function queryAddon(addon) {
addon && addon.uninstall();
});
return;
}
// 2. Check the corresponding theme is active
// 2.1 If Not, exit early
if (!theme.isActive) {
return;
}
init();
});
}
function shutdown() {
if (!branch || dead) {
// not initialized
return;
}
dead = true;
branch.removeObserver("", Observer);
branch = null;
// unapply all
for each (let i in Object.keys(applied)) {
handleSheet(i, false);
}
}
function init() {
if (dead) {
return;
}
// apply default prefs
let defBranch = Services.prefs.getDefaultBranch(PREF_BRANCH);
for each (let i in Object.keys(RADIO_SHEETS)) {
defBranch.setIntPref(i, 0);
}
for each (let s in BOOL_SHEETS) {
defBranch.setBoolPref(s, false);
}
branch = Services.prefs.getBranch(PREF_BRANCH);
if ('nsIPrefBranch2' in Ci) {
branch.QueryInterface(Ci.nsIPrefBranch2);
}
branch.addObserver("", Observer, false);
reload();
}
function uri(sheet) {
return Services.io.newURI(BASE_URI + sheet + ".css", null, null);
}
function handleSheet(sheet, condition) {
if (condition) {
if (sheet in applied) {
return;
}
Services.sss.loadAndRegisterSheet(uri(sheet), Services.sss.AGENT_SHEET);
applied[sheet] = true;
}
else {
if (!(sheet in applied)) {
return;
}
Services.sss.unregisterSheet(uri(sheet), Services.sss.AGENT_SHEET);
delete applied[sheet];
}
}
function reload() {
if (dead) {
return;
}
for (let [sheet, options] in Iterator(RADIO_SHEETS)) {
let val = branch.getIntPref(sheet);
if (val < 0 || val >= options.length) {
val = 0;
branch.setIntPref(sheet, val);
return; // the observer will reload
}
for (let i = 0; i < options.length; ++i) {
handleSheet(options[i], i == val);
}
}
for each (let sheet in BOOL_SHEETS) {
handleSheet(sheet, branch.getBoolPref(sheet));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment