mirror of
https://github.com/vale981/tridactyl
synced 2025-03-06 01:51:40 -05:00
native_background.ts: implement writePref()
This commit is contained in:
parent
b8b1470c86
commit
771888599e
1 changed files with 46 additions and 23 deletions
|
@ -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<string> {
|
||||
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})`))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue