Skip to content

Commit

Permalink
add preferences module
Browse files Browse the repository at this point in the history
  • Loading branch information
lieser committed May 1, 2020
1 parent 8371d95 commit e354683
Show file tree
Hide file tree
Showing 22 changed files with 1,135 additions and 187 deletions.
1 change: 1 addition & 0 deletions .vscode/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"DKIM",
"DMARC",
"dnssec",
"endregion",
"esversion",
"favicons",
"googlecode",
Expand Down
2 changes: 1 addition & 1 deletion _locales/de/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
"options_policy.signRules.sdid.allowSubDomains": {
"message": "Erlaube auch Subdomains der SDIDs"
},
"options_error.policy.wrong_sdid.asWarning": {
"options_policy.signRules.error.wrong_sdid.asWarning": {
"message": "Behandel falsche SDID als Warnung anstatt als Fehler"
},
"options_viewSigners": {
Expand Down
2 changes: 1 addition & 1 deletion _locales/en_US/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
"options_policy.signRules.sdid.allowSubDomains": {
"message": "Allow also subdomains of the SDIDs"
},
"options_error.policy.wrong_sdid.asWarning": {
"options_policy.signRules.error.wrong_sdid.asWarning": {
"message": "Treat wrong SDID as a warning instead of an error"
},
"options_viewSigners": {
Expand Down
2 changes: 1 addition & 1 deletion _locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
"options_policy.signRules.sdid.allowSubDomains": {
"message": "Autoriser également les sous domaines du SDID"
},
"options_error.policy.wrong_sdid.asWarning": {
"options_policy.signRules.error.wrong_sdid.asWarning": {
"message": "Traiter un mauvais SDID comme un avertissement au lieu d'une erreur"
},
"options_viewSigners": {
Expand Down
2 changes: 1 addition & 1 deletion _locales/hu/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
"options_policy.signRules.sdid.allowSubDomains": {
"message": "SDID-altartományok engedélyezése"
},
"options_error.policy.wrong_sdid.asWarning": {
"options_policy.signRules.error.wrong_sdid.asWarning": {
"message": "A helytelen SDID-t figyelmeztetésként kezelje, nem hibaként"
},
"options_viewSigners": {
Expand Down
2 changes: 1 addition & 1 deletion _locales/it/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
"options_policy.signRules.sdid.allowSubDomains": {
"message": "Permetti anche i sotto-domini dei SDID"
},
"options_error.policy.wrong_sdid.asWarning": {
"options_policy.signRules.error.wrong_sdid.asWarning": {
"message": "Considera SDID errato come un avviso invece che come un errore"
},
"options_viewSigners": {
Expand Down
2 changes: 1 addition & 1 deletion _locales/ja_JP/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
"options_policy.signRules.sdid.allowSubDomains": {
"message": "SDID のサブドメインも許容"
},
"options_error.policy.wrong_sdid.asWarning": {
"options_policy.signRules.error.wrong_sdid.asWarning": {
"message": "正しくない SDID をエラーではなく警告として扱う"
},
"options_viewSigners": {
Expand Down
2 changes: 1 addition & 1 deletion _locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
"options_policy.signRules.sdid.allowSubDomains": {
"message": "也允许 SDID 项的子域"
},
"options_error.policy.wrong_sdid.asWarning": {
"options_policy.signRules.error.wrong_sdid.asWarning": {
"message": "将错误的 SDID 视作一个警告而不是错误"
},
"options_viewSigners": {
Expand Down
17 changes: 13 additions & 4 deletions content/background.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@
import { DKIM_InternalError, DKIM_SigError } from "../modules/error.mjs.js";
import AuthVerifier from "../modules/AuthVerifier.mjs.js";
import Logging from "../modules/logging.mjs.js";
import prefs from "../modules/preferences.mjs.js";
import { setKeyFetchFunction } from "../modules/dkim/verifier.mjs.js";

Logging.setLogLevel(Logging.Level.Debug);
const log = Logging.getLogger("background");
(async () => {
await prefs.init();
if (prefs.debug) {
/** @type {number|undefined} */
// @ts-ignore
let logLevel = Logging.Level[prefs["logging.console"]];
if (!logLevel) {
logLevel = Logging.Level.Debug;
}
Logging.setLogLevel(logLevel);
}
})().catch(error => log.fatal("Setting debug log level failed with:", error));

// eslint-disable-next-line valid-jsdoc
/** @type {import("../modules/dkim/verifier.mjs.js").KeyFetchFunction} */
Expand All @@ -33,7 +45,6 @@ async function getKey(sdid, selector) {

const dnsRes = await browser.jsdns.txt(`${selector}._domainkey.${sdid}`);


if (dnsRes.bogus) {
throw new DKIM_InternalError(null, "DKIM_DNSERROR_DNSSEC_BOGUS");
}
Expand All @@ -45,8 +56,6 @@ async function getKey(sdid, selector) {
if (dnsRes.data === null || dnsRes.data[0] === "") {
throw new DKIM_SigError("DKIM_SIGERROR_NOKEY");
}
console.log("dd");


if (!dnsRes.data) {
throw new DKIM_SigError("DKIM_SIGERROR_NOKEY");
Expand Down
10 changes: 5 additions & 5 deletions content/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<fieldset disabled="disabled">
<div>
<label class="proxyConfig" data-i18n="options_dns.proxy.type"></label>
<select class="proxyConfig" data-pref="dns.proxy.type">
<select class="proxyConfig" data-pref="dns.proxy.type" data-pref-type="string">
<option value="socks">SOCKS v5</option>
<option value="socks4">SOCKS v4</option>
</select>
Expand All @@ -91,7 +91,7 @@
<label data-i18n="options_dns.proxy.host"></label>
<input type="text" data-pref="dns.proxy.host">
<label data-i18n="options_dns.proxy.port"></label>
<input type="number" data-pref="dns.proxy.port" size="10" min="0" max="65535">
<input type="number" data-pref="dns.proxy.port" data-pref-type="string" size="10" min="0" max="65535">
</div>
</fieldset>
</fieldset>
Expand All @@ -103,7 +103,7 @@
<input type="text" data-pref="dns.libunbound.path">
</div>
<label class="checkbox" data-i18n="options_dns.libunbound.path.relToProfileDir">
<input type="checkbox" data-pref="dns.libunbound.path.relToProfileDire">
<input type="checkbox" data-pref="dns.libunbound.path.relToProfileDir">
</label>
</fieldset>
</section>
Expand Down Expand Up @@ -135,8 +135,8 @@
<label class="checkbox" data-i18n="options_policy.signRules.sdid.allowSubDomains">
<input type="checkbox" data-pref="policy.signRules.sdid.allowSubDomains">
</label>
<label class="checkbox" data-i18n="options_error.policy.wrong_sdid.asWarning">
<input type="checkbox" data-pref="error.policy.wrong_sdid.asWarning">
<label class="checkbox" data-i18n="options_policy.signRules.error.wrong_sdid.asWarning">
<input type="checkbox" data-pref="policy.signRules.error.wrong_sdid.asWarning">
</label>
<label class="checkbox" data-i18n="options_policy.DMARC.shouldBeSigned.enable">
<input type="checkbox" data-pref="policy.DMARC.shouldBeSigned.enable">
Expand Down
103 changes: 102 additions & 1 deletion content/options.mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
// @ts-check
/* eslint-env browser */

import Logging from "../modules/logging.mjs.js";
import prefs from "../modules/preferences.mjs.js";

const log = Logging.getLogger("Options");

/**
* Set the active pane to the given navigation selector
*
Expand Down Expand Up @@ -54,8 +59,10 @@ function setNavigation(navSelector) {

/**
* Add navigation logic to <nav> elements and initialize navigation.
*
* @returns {void}
*/
document.addEventListener("DOMContentLoaded", () => {
function initNavigation() {
const navElements = Array.from(document.querySelectorAll("nav"));
for (const navElement of navElements) {
/** @type {HTMLElement[]} */
Expand All @@ -69,4 +76,98 @@ document.addEventListener("DOMContentLoaded", () => {
};
}
}
}

/**
* React to a change event on an HTML element representing a preference.
*
* @param {Event} event
* @returns {void}
*/
function preferenceChanged(event) {
try {
const target = event.target;
if (!(target instanceof HTMLElement)) {
log.warn("Received unexpected change event for non HTML element", event);
return;
}
const prefName = target.dataset.pref;
if (!prefName) {
log.warn("Received unexpected change event for element without data-pref attribute", event);
return;
}
if (target instanceof HTMLInputElement) {
if (target.getAttribute("type") === "checkbox") {
prefs.setValue(prefName, target.checked);
} else if (target.getAttribute("type") === "text" ||
target.dataset.prefType === "string"
) {
prefs.setValue(prefName, target.value);
} else if (target.getAttribute("type") === "number") {
prefs.setValue(prefName, parseInt(target.value, 10));
} else {
log.error("Received change event for input element without unexpected type", event);
}
} else if (target instanceof HTMLSelectElement) {
if (target.dataset.prefType === "string") {
prefs.setValue(prefName, target.value);
} else {
prefs.setValue(prefName, parseInt(target.value, 10));
}
} else {
log.error("Received change event for unexpected element", event);
}
} catch (e) {
log.fatal("Unexpected error in preferenceChanged():", e);
}
}

/**
* Initialize logic for preferences.
*
* @returns {Promise<void>}
*/
async function initPreferences() {
await prefs.init();

// set prefs to initial value
/** @type {HTMLElement[]} */
const prefElements = Array.from(document.querySelectorAll("[data-pref]"));
for (const element of prefElements) {
const prefName = element.dataset.pref;
if (!prefName) {
log.error("Preference element has unexpected data-pref attribute", element);
continue;
}
if (element instanceof HTMLInputElement) {
if (element.getAttribute("type") === "checkbox") {
element.checked = prefs.getBool(prefName);
} else if (element.getAttribute("type") === "text" ||
element.dataset.prefType === "string"
) {
element.value = prefs.getString(prefName);
} else if (element.getAttribute("type") === "number") {
element.value = prefs.getNumber(prefName).toString();
} else {
log.error("Input element has unexpected type", element);
}
} else if (element instanceof HTMLSelectElement) {
if (element.dataset.prefType === "string") {
element.value = prefs.getString(prefName);
} else {
element.value = prefs.getNumber(prefName).toString();
}
} else {
log.error("Unexpected preference element", element);
}
}

// listening to changes
document.body.addEventListener("change", preferenceChanged);
}

document.addEventListener("DOMContentLoaded", () => {
initNavigation();
initPreferences().
catch(e => log.fatal("Unexpected error in initPreferences():", e));
});
Loading

0 comments on commit e354683

Please sign in to comment.