Implement autoSubmit, reading settings from pass file and .browserpass.json (#43)

This commit is contained in:
Maxim Baz
2019-03-16 03:45:41 +01:00
committed by GitHub
parent 1676b90187
commit 423a9bbb43
2 changed files with 60 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ var appID = "com.github.browserpass.native";
// default settings
var defaultSettings = {
autoSubmit: false,
gpgPath: null,
stores: {},
foreignFills: {}
@@ -168,7 +169,8 @@ async function fillFields(settings, login, fields) {
var fillRequest = {
origin: new URL(settings.tab.url).origin,
login: login,
fields: fields
fields: fields,
autoSubmit: getSetting("autoSubmit", login, settings)
};
// fill form via injected script
@@ -224,6 +226,25 @@ function getLocalSettings() {
return settings;
}
/**
* Get most relevant setting value
*
* @param string key Setting key
* @param object login Login object
* @param object settings Settings object
* @return object Setting value
*/
function getSetting(key, login, settings) {
if (typeof login.settings[key] !== "undefined") {
return login.settings[key];
}
if (typeof settings.stores[login.store.id].settings[key] !== "undefined") {
return settings.stores[login.store.id].settings[key];
}
return settings[key];
}
/**
* Deep copy an object
*
@@ -488,6 +509,9 @@ async function parseFields(settings, login) {
login: ["login", "username", "user", "email"],
url: ["url", "uri", "website", "site", "link", "launch"]
};
login.settings = {
autoSubmit: { name: "autosubmit", type: "bool" }
};
var lines = login.raw.split(/[\r\n]+/).filter(line => line.trim().length > 0);
lines.forEach(function(line) {
// split key / value
@@ -509,6 +533,22 @@ async function parseFields(settings, login) {
break;
}
}
// assign to settings
for (var key in login.settings) {
if (
typeof login.settings[key].type !== "undefined" &&
login.settings[key].name.indexOf(parts[0].toLowerCase()) >= 0
) {
if (login.settings[key].type === "bool") {
login.settings[key] = ["true", "yes"].includes(parts[1].toLowerCase());
} else {
login.settings[key] = parts[1];
}
break;
}
}
});
// clean up unassigned fields
@@ -519,10 +559,15 @@ async function parseFields(settings, login) {
} else if (key == "login") {
login.fields[key] = login.login.match(/([^\/]+)$/)[1];
} else {
login.fields[key] = null;
delete login.fields[key];
}
}
}
for (var key in login.settings) {
if (typeof login.settings[key].type !== "undefined") {
delete login.settings[key];
}
}
}
/**
@@ -573,9 +618,18 @@ async function receiveMessage(message, sender, sendResponse) {
settings.stores.default = {
id: "default",
name: "pass",
path: response.data.defaultStore.path,
settings: response.data.defaultStore.settings
path: response.data.defaultStore.path
};
var fileSettings = JSON.parse(response.data.defaultStore.settings);
if (typeof settings.stores.default.settings !== "object") {
settings.stores.default.settings = {};
}
var storeSettings = settings.stores.default.settings;
for (var settingKey in fileSettings) {
if (!storeSettings.hasOwnProperty(settingKey)) {
storeSettings[settingKey] = fileSettings[settingKey];
}
}
}
// Fill recent data

View File

@@ -78,7 +78,6 @@
* @return object result of filling a form
*/
function fillLogin(request) {
var autoSubmit = false;
var result = {
filledFields: [],
foreignFill: undefined
@@ -139,14 +138,14 @@
// Try to submit the form, or focus on the submit button (based on user settings)
if (submit) {
if (autoSubmit) {
if (request.autoSubmit) {
submit.click();
} else {
submit.focus();
}
} else {
// There is no submit button. Try to submit the form itself.
if (autoSubmit && loginForm) {
if (request.autoSubmit && loginForm) {
loginForm.submit();
}
// We need to keep focus somewhere within the form, so that Enter hopefully submits the form.