Skip to content

Commit

Permalink
Fixes #229: Phases (1.) and (2.) of making background.js generic
Browse files Browse the repository at this point in the history
* `manifest.json` now contains a `short_name` property, which can be used for generating a default prefs file path and an options dialog URI
* `background.js` now utilizes the manifest to lookup the extension's short name, and to generate that default prefs path and dialog URI for registration
  • Loading branch information
eyalroz committed Jul 29, 2024
1 parent 75c71d9 commit 1aa9737
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 42 deletions.
106 changes: 67 additions & 39 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,85 @@
// Loader / background script
// for the removedupes Thunderbird extension
// by Eyal Rozenberg
// Loader / background script
// for the removedupes Thunderbird extension
// by Eyal Rozenberg

function registerDefaultPrefs(manifest) {
let shortname = manifest.short_name;
if (!shortname && !manifest.default_prefs) {
return;
}
let path = manifest.default_prefs ?? `defaults/preferences/${shortname}.js`;
messenger.WindowListener.registerDefaultPrefs(path);
};

function registerOptionsPage(manifest) {
let shortname = manifest.short_name;
if (!shortname && !manifest.options_dialog) {
return;
}
let uri = manifest.options_dialog ?? `chrome://${shortname}/content/${shortname}-prefs.xhtml`;
messenger.WindowListener.registerOptionsPage(uri);
}

// TODO: Instead of registering overlay injectors for windows,
// we should generate overlay injectors ourselves
function registerChromeInjectors(manifest,registrationInfo) {
let shortname = manifest.short_name;
if (!shortname) {
return;
}
for (let [windowHref, relativeInjectorPath] of registrationInfo) {
let absoluteWindowHref = windowHref.startsWith('about:') ?
windowHref : `chrome://messenger/content/${windowHref}`;
let jsFile = `chrome://${shortname}/content/overlay-injectors/${relativeInjectorPath}`;
messenger.WindowListener.registerWindow(absoluteWindowHref, jsFile);
}
};

(async function () {
messenger.WindowListener.registerDefaultPrefs("defaults/preferences/removedupes.js");
let manifest = browser.runtime.getManifest();
let shortname = manifest.short_name;
if (!manifest.short_name) { return; }

// Arrange it so that the register doesn't need us to repeat the shortname all the time
messenger.WindowListener.registerChromeUrl([
["content", "removedupes", "chrome/content/"],
["content", shortname, "chrome/content/"],

// skin elements are no longer supported as such, they are now considered just part of the content

// Overlays can't just be registered and then apply; rather, we need to register an injector script, see below

// Style elements need not be registered; it's enough to just inject them later on (in injector scripts)

["locale", "removedupes", "en-US", "chrome/locale/en-US/"],
["locale", "removedupes", "de", "chrome/locale/de/"],
["locale", "removedupes", "it", "chrome/locale/it/"],
["locale", "removedupes", "ja", "chrome/locale/ja/"],
["locale", "removedupes", "ja-JP-mac", "chrome/locale/ja/"],
["locale", "removedupes", "zh-TW", "chrome/locale/zh-TW/"],
["locale", "removedupes", "zh-CN", "chrome/locale/zh-CN/"],
["locale", "removedupes", "sk-SK", "chrome/locale/sk-SK/"],
["locale", "removedupes", "pt-PT", "chrome/locale/pt-PT/"],
["locale", "removedupes", "pt-BR", "chrome/locale/pt-BR/"],
["locale", "removedupes", "nl", "chrome/locale/nl/"],
["locale", "removedupes", "fr", "chrome/locale/fr/"],
["locale", "removedupes", "pl", "chrome/locale/pl/"],
["locale", "removedupes", "he-IL", "chrome/locale/he-IL/"],
["locale", "removedupes", "ru-RU", "chrome/locale/ru-RU/"],
["locale", "removedupes", "da", "chrome/locale/da/"],
["locale", "removedupes", "cs-CZ", "chrome/locale/cs-CZ/"],
["locale", "removedupes", "es-AR", "chrome/locale/es-AR/"],
["locale", "removedupes", "es-ES", "chrome/locale/es-ES/"],
["locale", "removedupes", "is-IS", "chrome/locale/is-IS/"],
["locale", "removedupes", "sv-SE", "chrome/locale/sv-SE/"],
["locale", "removedupes", "sl-SI", "chrome/locale/sl-SI/"]
["locale", shortname, "en-US", "chrome/locale/en-US/"],
["locale", shortname, "de", "chrome/locale/de/"],
["locale", shortname, "it", "chrome/locale/it/"],
["locale", shortname, "ja", "chrome/locale/ja/"],
["locale", shortname, "ja-JP-mac", "chrome/locale/ja/"],
["locale", shortname, "zh-TW", "chrome/locale/zh-TW/"],
["locale", shortname, "zh-CN", "chrome/locale/zh-CN/"],
["locale", shortname, "sk-SK", "chrome/locale/sk-SK/"],
["locale", shortname, "pt-PT", "chrome/locale/pt-PT/"],
["locale", shortname, "pt-BR", "chrome/locale/pt-BR/"],
["locale", shortname, "nl", "chrome/locale/nl/"],
["locale", shortname, "fr", "chrome/locale/fr/"],
["locale", shortname, "pl", "chrome/locale/pl/"],
["locale", shortname, "he-IL", "chrome/locale/he-IL/"],
["locale", shortname, "ru-RU", "chrome/locale/ru-RU/"],
["locale", shortname, "da", "chrome/locale/da/"],
["locale", shortname, "cs-CZ", "chrome/locale/cs-CZ/"],
["locale", shortname, "es-AR", "chrome/locale/es-AR/"],
["locale", shortname, "es-ES", "chrome/locale/es-ES/"],
["locale", shortname, "is-IS", "chrome/locale/is-IS/"],
["locale", shortname, "sv-SE", "chrome/locale/sv-SE/"],
["locale", shortname, "sl-SI", "chrome/locale/sl-SI/"]
]);

let registerChromeInjectors = function (registrationInfo) {
for (let [windowHref, relativeInjectorPath] of registrationInfo) {
let absoluteWindowHref = windowHref.startsWith('about:') ?
windowHref : `chrome://messenger/content/${windowHref}`;
let jsFile = `chrome://removedupes/content/overlay-injectors/${relativeInjectorPath}`;
messenger.WindowListener.registerWindow(absoluteWindowHref, jsFile);
}
};

registerChromeInjectors([
registerChromeInjectors(manifest, [
["messenger.xhtml", "messenger.js"],
["about:3pane", "3pane.js"],
["customizeToolbar.xhtml", "customizeToolbar.js"]
]);

messenger.WindowListener.registerOptionsPage("chrome://removedupes/content/removedupes-prefs.xhtml");
registerDefaultPrefs(manifest);
registerOptionsPage(manifest);
messenger.WindowListener.startListening();
})();
7 changes: 4 additions & 3 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "Eyal Rozenberg",
"homepage_url": "https://github.com/eyalroz/removedupes",
"name": "Remove Duplicate Messages",
"short_name" : "removedupes",
"description": "Search mail folders for duplicate messages and move or delete them",
"icons": {
"32": "chrome/content/removedupes.png",
Expand All @@ -30,9 +31,9 @@
}
},
"background": {
"scripts": [
"background.js"
]
"scripts": [
"background.js"
]
}
}

0 comments on commit 1aa9737

Please sign in to comment.