Skip to content

Linux + iPhone Continuity / iMessage / SMS

testing browser extension

Zack Bartel382a5dda5651
diff --git a/extension/src/content/autofill.js b/extension/src/content/autofill.js
index d1daf22..0b58ca2 100644
--- a/extension/src/content/autofill.js
+++ b/extension/src/content/autofill.js
@@ -1,7 +1,7 @@
// Content script for browsers (Chrome/Firefox)
// This runs in the context of webpages and looks for OTP input fields
-function findOtpInputs(doc) {
+export function findOtpInputs(doc) {
const inputs = [...doc.querySelectorAll('input')];
return inputs.filter(input => {
@@ -15,7 +15,7 @@ function findOtpInputs(doc) {
// Tier 3: attribute keyword matching
const attrs = [input.id, input.name, input.placeholder, input.className]
.join(' ').toLowerCase();
- if (/otp|passcode|verif|code|token|pin|2fa/.test(attrs)) return true;
+ if (/\b(otp|passcode|verif|token|pin|2fa)\b/.test(attrs)) return true;
// Tier 4: maxlength in OTP range (4–8)
const ml = parseInt(input.maxLength);
@@ -25,7 +25,7 @@ function findOtpInputs(doc) {
});
}
-function scoreInput(input) {
+export function scoreInput(input) {
let score = 0;
// Look at nearby label text
const label = input.labels?.[0]?.textContent
@@ -39,7 +39,7 @@ function scoreInput(input) {
return score;
}
-function detectSplitOtp(doc) {
+export function detectSplitOtp(doc) {
const singles = [...doc.querySelectorAll('input[maxlength="1"]')]
.filter(i => i.type === 'text' || i.type === 'tel' || i.type === 'number' || !i.type);
@@ -50,6 +50,12 @@ function detectSplitOtp(doc) {
return null;
}
+// ---------------------------------------------------------------------------
+// Browser-only code - only runs when loaded in actual browser context
+// ---------------------------------------------------------------------------
+if (typeof document === 'undefined') {
+ // Test environment: skip browser-only code
+} else {
let otpInterval = null;
function requestOtp() {
@@ -118,3 +124,4 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
}
}
});
+}
diff --git a/extension/test/autofill.test.js b/extension/test/autofill.test.js
new file mode 100644
index 0000000..d199bf2
--- /dev/null
+++ b/extension/test/autofill.test.js
@@ -0,0 +1,113 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { JSDOM } from 'jsdom';
+import * as fs from 'fs';
+import * as path from 'path';
+
+const loadFixture = (filename) => {
+ const html = fs.readFileSync(path.join(__dirname, 'fixtures', filename), 'utf8');
+ return new JSDOM(html).window.document;
+};
+
+global.chrome = {
+ runtime: {
+ sendMessage: vi.fn(),
+ onMessage: {
+ addListener: vi.fn()
+ }
+ }
+};
+
+describe('OTP input detection', () => {
+ let findOtpInputs, scoreInput, detectSplitOtp;
+
+ beforeEach(async () => {
+ const autofill = await import('../src/content/autofill.js');
+ findOtpInputs = autofill.findOtpInputs;
+ scoreInput = autofill.scoreInput;
+ detectSplitOtp = autofill.detectSplitOtp;
+ });
+
+ describe('findOtpInputs', () => {
+ it('detects standard autocomplete attribute', () => {
+ const doc = loadFixture('std-autocomplete.html');
+ const inputs = findOtpInputs(doc);
+ expect(inputs.length).toBe(1);
+ expect(inputs[0].autocomplete).toBe('one-time-code');
+ });
+
+ it('detects split OTP inputs', () => {
+ const doc = loadFixture('split-otp.html');
+ const inputs = detectSplitOtp(doc);
+ expect(inputs).toHaveLength(6);
+ });
+
+ it('detects by placeholder/name keywords', () => {
+ const doc = loadFixture('google-otp.html');
+ const inputs = findOtpInputs(doc);
+ expect(inputs.length).toBe(1);
+ });
+
+ it('detects numeric inputmode', () => {
+ const doc = loadFixture('numeric-otp.html');
+ const inputs = findOtpInputs(doc);
+ expect(inputs.length).toBe(1);
+ });
+ });
+
+ describe('scoreInput', () => {
+ it('scores high for label with code keywords', () => {
+ const doc = loadFixture('std-autocomplete.html');
+ const input = doc.querySelector('input');
+ const score = scoreInput(input);
+ expect(score).toBeGreaterThan(0);
+ });
+
+ it('scores low for generic fields', () => {
+ const doc = new JSDOM('<input type="text" name="foo">').window.document;
+ const input = doc.querySelector('input');
+ const score = scoreInput(input);
+ expect(score).toBe(0);
+ });
+ });
+});
+
+describe('OTP autofill insertion', () => {
+ let detectSplitOtp, findOtpInputs;
+
+ beforeEach(async () => {
+ const autofill = await import('../src/content/autofill.js');
+ detectSplitOtp = autofill.detectSplitOtp;
+ findOtpInputs = autofill.findOtpInputs;
+ });
+
+ describe('split OTP filling', () => {
+ it('fills each character into separate fields', () => {
+ const doc = loadFixture('split-otp.html');
+ const inputs = detectSplitOtp(doc);
+
+ const otp = '123456';
+ for (let i = 0; i < otp.length; i++) {
+ inputs[i].value = otp[i];
+ inputs[i].dispatchEvent(new doc.defaultView.Event('input', { bubbles: true }));
+ inputs[i].dispatchEvent(new doc.defaultView.Event('change', { bubbles: true }));
+ }
+
+ expect(inputs[0].value).toBe('1');
+ expect(inputs[1].value).toBe('2');
+ expect(inputs[5].value).toBe('6');
+ });
+ });
+
+ describe('single OTP filling', () => {
+ it('fills full OTP into single field', () => {
+ const doc = loadFixture('std-autocomplete.html');
+ const inputs = findOtpInputs(doc);
+
+ inputs[0].value = '789012';
+ inputs[0].dispatchEvent(new doc.defaultView.Event('input', { bubbles: true }));
+ inputs[0].dispatchEvent(new doc.defaultView.Event('change', { bubbles: true }));
+
+ expect(inputs[0].value).toBe('789012');
+ });
+ });
+});
\ No newline at end of file
diff --git a/extension/test/fixtures/google-otp.html b/extension/test/fixtures/google-otp.html
new file mode 100644
index 0000000..9eac314
--- /dev/null
+++ b/extension/test/fixtures/google-otp.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+<head><title>Google-style OTP</title></head>
+<body>
+ <form>
+ <div class="entry">
+ <label>Verification code</label>
+ <input type="text" name="code" id="code" placeholder="Enter code" maxlength="6">
+ </div>
+ </form>
+</body>
+</html>
\ No newline at end of file
diff --git a/extension/test/fixtures/numeric-otp.html b/extension/test/fixtures/numeric-otp.html
new file mode 100644
index 0000000..1b1428d
--- /dev/null
+++ b/extension/test/fixtures/numeric-otp.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head><title>Numeric OTP Input</title></head>
+<body>
+ <form>
+ <label for="otp">One-Time PIN</label>
+ <input type="numeric" inputmode="numeric" id="otp" maxlength="6">
+ </form>
+</body>
+</html>
\ No newline at end of file
diff --git a/extension/test/fixtures/postal-code.html b/extension/test/fixtures/postal-code.html
new file mode 100644
index 0000000..5b278b7
--- /dev/null
+++ b/extension/test/fixtures/postal-code.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<head><title>Hidden OTP - false positive</title></head>
+<body>
+ <form>
+ <label>ZIP code</label>
+ <input type="text" name="postcode" id="postcode" maxlength="8" placeholder="e.g. 12345">
+ <button type="submit">Submit</button>
+ </form>
+</body>
+</html>
\ No newline at end of file
diff --git a/extension/test/fixtures/split-otp.html b/extension/test/fixtures/split-otp.html
new file mode 100644
index 0000000..379c681
--- /dev/null
+++ b/extension/test/fixtures/split-otp.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+<head><title>Split OTP Input</title></head>
+<body>
+ <form>
+ <p>Enter the 6-digit code</p>
+ <input type="text" maxlength="1" aria-label="First digit">
+ <input type="text" maxlength="1" aria-label="Second digit">
+ <input type="text" maxlength="1" aria-label="Third digit">
+ <input type="text" maxlength="1" aria-label="Fourth digit">
+ <input type="text" maxlength="1" aria-label="Fifth digit">
+ <input type="text" maxlength="1" aria-label="Sixth digit">
+ <button type="submit">Verify</button>
+ </form>
+</body>
+</html>
\ No newline at end of file
diff --git a/extension/test/fixtures/std-autocomplete.html b/extension/test/fixtures/std-autocomplete.html
new file mode 100644
index 0000000..02a905f
--- /dev/null
+++ b/extension/test/fixtures/std-autocomplete.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<head><title>Standard OTP Form</title></head>
+<body>
+ <form>
+ <label for="otp">Verification Code</label>
+ <input type="text" id="otp" name="otp" autocomplete="one-time-code" maxlength="6" placeholder="Enter 6-digit code">
+ <button type="submit">Verify</button>
+ </form>
+</body>
+</html>
\ No newline at end of file