Move prefs.js parsing to Zotero.Profile.readPrefsFromFile(prefsFile)

This commit is contained in:
Dan Stillman 2018-04-01 13:44:10 -04:00
parent 7f81e62bc8
commit 8d0dc359b4
2 changed files with 33 additions and 29 deletions

View file

@ -255,35 +255,7 @@ Zotero.DataDirectory = {
// Read in prefs
let prefsFile = OS.Path.join(profileDir, "prefs.js");
if (yield OS.File.exists(prefsFile)) {
// build sandbox
var sandbox = new Components.utils.Sandbox("http://www.example.com/");
Components.utils.evalInSandbox(
"var prefs = {};"+
"function user_pref(key, val) {"+
"prefs[key] = val;"+
"}"
, sandbox);
(yield Zotero.File.getContentsAsync(prefsFile))
.split(/\n/)
.filter((line) => {
// Strip comments
return !line.startsWith('#')
// Only process lines in our pref branch
&& line.includes(ZOTERO_CONFIG.PREF_BRANCH);
})
// Process each line individually
.forEach((line) => {
try {
Zotero.debug("Processing " + line);
Components.utils.evalInSandbox(line, sandbox);
}
catch (e) {
Zotero.logError("Error processing prefs line: " + line);
}
});
var prefs = sandbox.prefs;
let prefs = yield Zotero.Profile.readPrefsFromFile(prefsFile);
// Check for data dir pref
if (prefs['extensions.zotero.dataDir'] && prefs['extensions.zotero.useDataDir']) {

View file

@ -240,6 +240,38 @@ Zotero.Profile = {
},
readPrefsFromFile: async function (prefsFile) {
var sandbox = new Components.utils.Sandbox("http://www.example.com/");
Components.utils.evalInSandbox(
"var prefs = {};"+
"function user_pref(key, val) {"+
"prefs[key] = val;"+
"}"
, sandbox);
(await Zotero.File.getContentsAsync(prefsFile))
.split(/\n/)
.filter((line) => {
// Strip comments
return !line.startsWith('#')
// Only process lines in our pref branch
&& line.includes(ZOTERO_CONFIG.PREF_BRANCH);
})
// Process each line individually
.forEach((line) => {
try {
Zotero.debug("Processing " + line);
Components.utils.evalInSandbox(line, sandbox);
}
catch (e) {
Zotero.logError("Error processing prefs line: " + line);
}
});
return sandbox.prefs;
},
//
// Private methods
//