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 // default settings
var defaultSettings = { var defaultSettings = {
autoSubmit: false,
gpgPath: null, gpgPath: null,
stores: {}, stores: {},
foreignFills: {} foreignFills: {}
@@ -168,7 +169,8 @@ async function fillFields(settings, login, fields) {
var fillRequest = { var fillRequest = {
origin: new URL(settings.tab.url).origin, origin: new URL(settings.tab.url).origin,
login: login, login: login,
fields: fields fields: fields,
autoSubmit: getSetting("autoSubmit", login, settings)
}; };
// fill form via injected script // fill form via injected script
@@ -224,6 +226,25 @@ function getLocalSettings() {
return settings; 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 * Deep copy an object
* *
@@ -488,6 +509,9 @@ async function parseFields(settings, login) {
login: ["login", "username", "user", "email"], login: ["login", "username", "user", "email"],
url: ["url", "uri", "website", "site", "link", "launch"] 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); var lines = login.raw.split(/[\r\n]+/).filter(line => line.trim().length > 0);
lines.forEach(function(line) { lines.forEach(function(line) {
// split key / value // split key / value
@@ -509,6 +533,22 @@ async function parseFields(settings, login) {
break; 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 // clean up unassigned fields
@@ -519,10 +559,15 @@ async function parseFields(settings, login) {
} else if (key == "login") { } else if (key == "login") {
login.fields[key] = login.login.match(/([^\/]+)$/)[1]; login.fields[key] = login.login.match(/([^\/]+)$/)[1];
} else { } 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 = { settings.stores.default = {
id: "default", id: "default",
name: "pass", name: "pass",
path: response.data.defaultStore.path, path: response.data.defaultStore.path
settings: response.data.defaultStore.settings
}; };
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 // Fill recent data

View File

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