-
Notifications
You must be signed in to change notification settings - Fork 0
feat(form-assistant): local Grammarly-like field assistant (Phase 1) #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
apps/extension/src/content/form-assistant/field-detector.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /** | ||
| * Content script — DOM → RawFieldInput → FieldDescriptor (sanitisé + classifié). | ||
| * | ||
| * Lit uniquement des métadonnées (label/placeholder/type/required), jamais la | ||
| * valeur courante du champ. Délègue la sanitisation + classification au Core | ||
| * (sanitizeFieldDescriptor). Aucune logique métier ici. | ||
| */ | ||
| import type { | ||
| FieldDescriptor, | ||
| FieldInputType, | ||
| RawFieldInput, | ||
| } from '../../lib/core/form-assistant/types'; | ||
| import { sanitizeFieldDescriptor } from '../../lib/core/form-assistant'; | ||
|
|
||
| const MAX_TEXT_LEN = 200; | ||
|
|
||
| function trimText(value: string, max = MAX_TEXT_LEN): string { | ||
| return value.replace(/\s+/g, ' ').trim().slice(0, max); | ||
| } | ||
|
|
||
| /** | ||
| * Mappe un élément DOM vers un FieldInputType connu, ou `null` si le champ | ||
| * n'est pas éligible (mot de passe, checkbox, hidden, date, range, …). | ||
| */ | ||
| function resolveInputType(el: HTMLElement): FieldInputType | null { | ||
| const tag = el.tagName.toLowerCase(); | ||
| if (tag === 'textarea') { | ||
| return 'textarea'; | ||
| } | ||
| if (el.isContentEditable) { | ||
| return 'contenteditable'; | ||
| } | ||
| if (tag !== 'input') { | ||
| return null; | ||
| } | ||
|
|
||
| const rawType = (el as HTMLInputElement).type.toLowerCase(); | ||
| switch (rawType) { | ||
| case 'email': | ||
| return 'email'; | ||
| case 'tel': | ||
| return 'tel'; | ||
| case 'url': | ||
| return 'url'; | ||
| case 'search': | ||
| return 'search'; | ||
| case 'text': | ||
| case '': | ||
| return 'text'; | ||
| default: | ||
| // password, checkbox, radio, hidden, date, number, range, file, color, … | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Résout le libellé humain d'un champ, par ordre de fiabilité décroissante. | ||
| * Ne lève jamais ; retourne une chaîne vide si rien n'est trouvé. | ||
| */ | ||
| function resolveLabel(el: HTMLElement): string { | ||
| if (el.id) { | ||
| const associated = document.querySelector(`label[for="${CSS.escape(el.id)}"]`); | ||
| if (associated?.textContent) { | ||
| return trimText(associated.textContent); | ||
| } | ||
| } | ||
| const ariaLabel = el.getAttribute('aria-label'); | ||
| if (ariaLabel) { | ||
| return trimText(ariaLabel); | ||
| } | ||
| const labelledBy = el.getAttribute('aria-labelledby'); | ||
| if (labelledBy) { | ||
| // `aria-labelledby` peut référencer plusieurs IDs séparés par des espaces | ||
| // (ex : "field-label field-hint"). On concatène le texte de chacun. | ||
| const ids = labelledBy.trim().split(/\s+/); | ||
| const texts: string[] = []; | ||
| for (const id of ids) { | ||
| if (!id) { | ||
| continue; | ||
| } | ||
| const labeller = document.getElementById(id); | ||
| if (labeller?.textContent) { | ||
| texts.push(trimText(labeller.textContent)); | ||
| } | ||
| } | ||
| if (texts.length > 0) { | ||
| return trimText(texts.join(' ')); | ||
| } | ||
| } | ||
| const wrapping = el.closest('label'); | ||
| if (wrapping?.textContent) { | ||
| return trimText(wrapping.textContent); | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| function resolvePlaceholder(el: HTMLElement): string { | ||
| const ph = (el as HTMLInputElement).placeholder; | ||
| return ph ? trimText(ph) : ''; | ||
| } | ||
|
|
||
| function resolveRequired(el: HTMLElement): boolean { | ||
| return el.hasAttribute('required') || el.getAttribute('aria-required') === 'true'; | ||
| } | ||
|
|
||
| /** | ||
| * Construit le FieldDescriptor sanit-isé pour un élément focalisé, ou `null` | ||
| * si le champ n'est pas éligible au Form Assistant. | ||
| */ | ||
| export function detectFieldDescriptor(target: HTMLElement): FieldDescriptor | null { | ||
| const inputType = resolveInputType(target); | ||
| if (!inputType) { | ||
| return null; | ||
| } | ||
| // Champs non modifiables : rien à proposer. | ||
| if ( | ||
| (target as HTMLInputElement).readOnly || | ||
| (target as HTMLInputElement).disabled || | ||
| target.getAttribute('aria-readonly') === 'true' || | ||
| target.getAttribute('aria-disabled') === 'true' | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| const raw: RawFieldInput = { | ||
| label: resolveLabel(target), | ||
| placeholder: resolvePlaceholder(target), | ||
| inputType, | ||
| required: resolveRequired(target), | ||
| }; | ||
|
|
||
| return sanitizeFieldDescriptor(raw); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.