From 771888599ec7c1fad208d19fa3a3162905a12456 Mon Sep 17 00:00:00 2001 From: glacambre Date: Thu, 17 May 2018 08:23:25 +0200 Subject: [PATCH] native_background.ts: implement writePref() --- src/native_background.ts | 69 ++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/src/native_background.ts b/src/native_background.ts index b35d620b..f2ed0ee7 100644 --- a/src/native_background.ts +++ b/src/native_background.ts @@ -326,6 +326,29 @@ export async function getProfileDir() { } } +export async function parsePrefs(prefFileContent: string) { + // This RegExp currently only deals with " but for correctness it should + // also deal with ' and ` + // We could also just give up on parsing and eval() the whole thing + const regex = new RegExp( + /^(user_|sticky_|lock)?[pP]ref\("([^"]+)",\s*"?([^\)]+?)"?\);$/, + ) + // Fragile parsing + let allPrefs = prefFileContent.split("\n").reduce((prefs, line) => { + let matches = line.match(regex) + if (!matches) { + return prefs + } + const key = matches[2] + let value = matches[3] + // value = " means that it should be an empty string + if (value == '"') value = "" + prefs[key] = value + return prefs + }, {}) + return allPrefs +} + /** When given the name of a firefox preference file, will load said file and * return a promise for an object the keys of which correspond to preference * names and the values of which correspond to preference values. @@ -334,27 +357,8 @@ export async function getProfileDir() { */ export async function loadPrefs(filename): Promise<{ [key: string]: string }> { const result = await read(filename) - if (result.code == 2) return {} - // This RegExp currently only deals with " but for correctness it should - // also deal with ' and ` - // We could also just give up on parsing and eval() the whole thing - const regex = new RegExp( - /^(user_|sticky_|lock)?[pP]ref\("([^"]+)",\s*"?([^\)]+?)"\);$/, - ) - // Fragile parsing - let allPrefs = result.content.split("\n").reduce((prefs, line) => { - let matches = line.match(regex) - if (!matches) { - return prefs - } - const key = matches[2] - let value = matches[3] - // value = "" means that it should be an empty string - if (value == '"') value = "" - prefs[key] = value - return prefs - }, {}) - return allPrefs + if (result.code != 0) return {} + return parsePrefs(result.content) } /** Returns a promise for an object that should contain every about:config @@ -366,7 +370,6 @@ export async function getPrefs(): Promise<{ [key: string]: string }> { const profile = (await getProfileDir()) + "/" const prefFiles = [ // Debian has these - "/etc/firefox/firefox.js", "/usr/share/firefox/browser/defaults/preferences/firefox.js", "/usr/share/firefox/browser/defaults/preferences/debugger.js", "/usr/share/firefox/browser/defaults/preferences/devtools-startup-prefs.js", @@ -374,6 +377,7 @@ export async function getPrefs(): Promise<{ [key: string]: string }> { "/usr/share/firefox/browser/defaults/preferences/firefox-branding.js", "/usr/share/firefox/browser/defaults/preferences/vendor.js", "/usr/share/firefox/browser/defaults/preferences/firefox.js", + "/etc/firefox/firefox.js", // Pref files can be found here: // https://developer.mozilla.org/en-US/docs/Mozilla/Preferences/A_brief_guide_to_Mozilla_preferences profile + "grepref.js", @@ -387,7 +391,6 @@ export async function getPrefs(): Promise<{ [key: string]: string }> { profile + "user.js", ] let promises = [] - let prers = {} // Starting all promises before awaiting because we want the calls to be // made in parallel for (let file of prefFiles) { @@ -400,3 +403,23 @@ export async function getPrefs(): Promise<{ [key: string]: string }> { export async function getPref(name: string): Promise { return (await getPrefs())[name] } + +/** Writes a preference to user.js */ +export async function writePref(name: string, value: any) { + if (typeof value == "string") value = `"${value}"` + const file = (await getProfileDir()) + "/user.js" + // No need to check the return code because read returns "" when failing to + // read a file + const text = (await read(file)).content + let prefPos = text.indexOf(`pref("${name}",`) + if (prefPos < 0) { + write(file, `${text}\nuser_pref("${name}", ${value});\n`) + } else { + let substr = text.substring(prefPos) + let prefEnd = substr.indexOf(";\n") + console.log(text, substr, prefPos, prefEnd) + substr = text.substring(prefPos, prefPos + prefEnd) + console.log(substr) + write(file, text.replace(substr, `pref("${name}", ${value})`)) + } +}