The code assumed some pairs could be optional, but as far as i can tell
this can only be specified when querying the database. Now instead look
for the form with most matching keys.
It may make sense to limit the match count to at least two to avoid
filling a completely unrelated form, but it is possible to have a
single-input form, so hopefully the on-demand nature of auto-filling is
enough.
---
Apologies, defaulting to first form was a leftover from an earlier
variant. Question still stands: any thoughts how to prevent filling an
unrelated form?
content/content.js | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/content/content.js b/content/content.js
index 73b657d..1852843 100644
--- a/content/content.js+++ b/content/content.js
@@ -16,6 +16,9 @@ function handleAutofill(msg) {
return;
};
const input = findInput(form, pair.key);
+ if (input === null) {+ return;+ } input.click();
input.focus();
input.value = pair.value;
@@ -30,26 +33,25 @@ function handleAutofill(msg) {
function findForm(key) {
const forms = document.querySelectorAll("form");
+ let bestForm = null;+ let bestHits = 0; for (let i = 0; i < forms.length; i++) {
const form = forms[i];
- let valid = true;+ let hits = 0; key.map(pair => {
if (pair.key === "proto" || pair.key === "host") {
return;
};
- if (pair.optional) {- return;- } const input = findInput(form, pair.key);
- if (input === null) {- valid = false;+ if (input !== null) {+ hits++; }
});
- if (valid) {- return form;+ if (hits > bestHits) {+ bestForm = form; }
}
- return null;+ return bestForm;}
function findInput(elem, key) {
--
2.46.0