Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix(utils): accept tel links with a one-digit country code, parameters or percent-encoding (#4336)

SwastikTripathi69487eac9184
diff --git a/packages/utils/src/internals/social.ts b/packages/utils/src/internals/social.ts
index 9bec60c18..0cb983e37 100644
--- a/packages/utils/src/internals/social.ts
+++ b/packages/utils/src/internals/social.ts
@@ -156,8 +156,17 @@ export function phonesFromUrls(urls: string[]): string[] {
if (!url) continue;
if (!PHONE_URL_PREFIX_REGEX.test(url)) continue;
- const phone = url.replace(PHONE_URL_PREFIX_REGEX, '').trim();
- if (PHONE_REGEX.test(phone)) phones.push(phone);
+ // Per RFC 3966, parameters such as `;ext=89` can follow the number, and `-`, `.`, `(` and `)`
+ // are only visual separators. They are checked without separators too, so a one-digit country
+ // code like in `tel:+1-555-123-4567` is accepted.
+ const [number] = url.replace(PHONE_URL_PREFIX_REGEX, '').split(';');
+ let phone = number.trim();
+ try {
+ phone = decodeURIComponent(phone).trim();
+ } catch {
+ // Keep the raw value if it isn't valid percent-encoding
+ }
+ if (PHONE_REGEX.test(phone) || PHONE_REGEX.test(phone.replace(/[-.() ]/g, ''))) phones.push(phone);
}
return phones;
}
diff --git a/test/utils/social.test.ts b/test/utils/social.test.ts
index 4b93c4e60..49dd5e35a 100644
--- a/test/utils/social.test.ts
+++ b/test/utils/social.test.ts
@@ -359,6 +359,27 @@ describe('utils.social', () => {
]),
).toEqual(['+42012345678', '+420.123.456']);
});
+
+ test('handles country codes, separators, parameters and percent-encoding', () => {
+ expect(
+ phonesFromUrls([
+ 'tel:+1-555-123-4567',
+ 'tel:+1 555 123 4567',
+ 'tel:+1.555.123.4567',
+ 'tel:+1 (555) 123-4567',
+ 'tel:%2B1%20555%20123%204567',
+ 'tel:5551234567;ext=89',
+ 'tel:+1-555',
+ ]),
+ ).toEqual([
+ '+1-555-123-4567',
+ '+1 555 123 4567',
+ '+1.555.123.4567',
+ '+1 (555) 123-4567',
+ '+1 555 123 4567',
+ '5551234567',
+ ]);
+ });
});
describe('parseHandlesFromHtml()', () => {