Skip to content

Commit

Permalink
wip: try out header signals
Browse files Browse the repository at this point in the history
  • Loading branch information
dbajpeyi committed Dec 19, 2024
1 parent 5a95320 commit a08905c
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 32 deletions.
39 changes: 34 additions & 5 deletions dist/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 34 additions & 5 deletions dist/autofill.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 52 additions & 12 deletions src/Form/FormAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,55 @@ class FormAnalyzer {
});
}


updateFormHeaderSignals() {

const isVisuallyBeforeForm = (el) => el.getBoundingClientRect().top < this.form.getBoundingClientRect().top;

const isHeaderSized = (el) => {
if (el instanceof HTMLHeadingElement) {
return true;
}

const computedStyle = window.getComputedStyle(el);
const fontWeight = computedStyle.fontWeight;
const isRelativelyTall = parseFloat(computedStyle.height) / this.form.clientHeight > 0.1;
if (fontWeight === 'bold' || parseFloat(fontWeight) >= 700 || isRelativelyTall) {
return true
}
}

const allSiblings = Array.from(this.form.parentElement?.children ?? [])
.filter((element) => element !== this.form)
if (allSiblings.length === 0) return false;


allSiblings.forEach((sibling) => {
if (sibling instanceof HTMLElement && sibling.childElementCount === 1 && isVisuallyBeforeForm(sibling) && isHeaderSized(sibling)) {
const string = sibling.textContent?.trim();
if (string) {
if (safeRegexTest(/^(sign[- ]?in|log[- ]?in)$/, string)) {
return this.decreaseSignalBy(3, 'Strong login header before form');
} else if (safeRegexTest(/^(sign[- ]?up)$/, string)) {
return this.increaseSignalBy(3, 'Strong signup header before form');
}
}
}
});
}

hasPasswordHints() {
return Array.from(this.form.querySelectorAll('div, span'))
.filter(
(div) =>
div.textContent != null &&
div.textContent.trim() !== '' &&
window.getComputedStyle(div).display !== 'none' &&
window.getComputedStyle(div).visibility !== 'hidden',
)
.some((div) => div.textContent && safeRegexTest(this.matching.getDDGMatcherRegex('passwordHintsRegex'), div.textContent));
}

/**
* Function that checks if the element is an external link or a custom web element that
* encapsulates a link.
Expand Down Expand Up @@ -336,19 +385,10 @@ class FormAnalyzer {
this.increaseSignalBy(relevantFields.length * 1.5, 'many fields: it is probably not a login');
}

// If the form contains password hints, it's highly likely a signup form.
const hasPasswordHints = Array.from(this.form.querySelectorAll('div, span'))
.filter(
(div) =>
div.textContent != null &&
div.textContent.trim() !== '' &&
window.getComputedStyle(div).display !== 'none' &&
window.getComputedStyle(div).visibility !== 'hidden',
)
.some((div) => div.textContent && safeRegexTest(this.matching.getDDGMatcherRegex('passwordHintsRegex'), div.textContent));
this.updateFormHeaderSignals();

if (hasPasswordHints) {
this.increaseSignalBy(6, 'Password hints');
if (this.hasPasswordHints()) {
this.increaseSignalBy(3, 'Password hints');
}

// If we can't decide at this point, try reading page headings
Expand Down
39 changes: 34 additions & 5 deletions swift-package/Resources/assets/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 34 additions & 5 deletions swift-package/Resources/assets/autofill.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a08905c

Please sign in to comment.