r/webdev • u/SoilAlarmed7898 • 26d ago
Showoff Saturday Detecting 2FA codes inside Gmail's DOM without false positives on dates and order numbers (MV3 side project)
I got tired of selecting and retyping verification codes from Gmail, so I built a small Manifest V3 extension for myself that puts a "Copy code" button next to the code. The fun part turned out to be detection, not the button.
How it works
- A content script runs only on
mail.google.com. No host permissions beyond that, no background server, nothing leaves the page. - Gmail is an SPA, so a
MutationObserverwatches for the opened message body instead of relying on page loads. - Candidates: 4–8 digit runs,
123 456/123-456splits, and short alphanumeric codes likeAB12CD. - To avoid false positives, I score each candidate by nearby keywords ("code", "verification", "código", "OTP", "one-time") within the same block, and penalize things that look like dates, times, prices, phone numbers, or order IDs (prefixes like
#, "order", "pedido"). - The highest score wins. If nothing passes the threshold, no button appears. Showing no button beats copying the wrong number.
What surprised me
- Alphanumeric codes are much harder than digits. Random uppercase words in subject lines ("NEW", "SALE") kept matching until I required at least one digit and the keyword-proximity check.
- Some senders put the code in an image or a styled table, and those I can't catch.
It's free with no account, UI in EN/PT/ES. I'd love technical feedback, especially if you know of a smarter heuristic than keyword proximity, or senders whose format breaks it.
https://chromewebstore.google.com/detail/mpglgifleaaollcggdpcfhjjgmcageph
0
Upvotes
6
u/fropirate 26d ago
Doesn't gmail do this automatically now?
3
u/sinthorius 25d ago
Sure, always wanted a thirdparty from a trust-me-dude to intercept my second factor
2
u/AshleyJSheridan 25d ago
Given that this whole post is clearly written by AI, one has to question how much of this thing "you" built, and how much of it was vibe coded?
1
u/kdjayakody 26d ago
One edge case I'd test is a message with both an OTP and a nearby order number or date, plus a localized template. I'd keep detection scoped to the opened message body, not inbox rows, and show nothing when confidence is low. That's where avoiding a false copy matters most.
1
u/RequirementGuide 25d ago
I've hit this exact problem before with OTP parsing, and keyword proximity alone kept tripping me up on order numbers too. What helped me a lot was looking at the shape of the text around the candidate as well, like whether the digits sit on their own line or in their own little element with nothing else in it. Real codes usually do, order numbers and dates almost never do. I also kept a small test set of real mails from the senders I actually use and checked my precision against it before trusting a new tweak, because it is so easy to fix one sender and break three others. Nice little project by the way, the no button beats the wrong button call is spot on.
1
u/Crazyboreddeveloper 25d ago
Pass. I’d rather keep the security