Skip to content

Commit 9c87929

Browse files
author
Lalit Sharma
committed
feat: add support for parsing short links with JSON lat/lng fields in HTML
1 parent abc0840 commit 9c87929

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

apps/mobile/src/utils/sharedMapLink.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ const GOOGLE_CENTER_ARRAY_RE = new RegExp(
6666
"i",
6767
);
6868

69+
// Browser headers to avoid consent pages
70+
const BROWSER_HEADERS = {
71+
"user-agent":
72+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
73+
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
74+
"accept-language": "en-GB,en;q=0.9",
75+
};
76+
6977
type ExpandedShortMapUrlResult = {
7078
expandedUrl: string;
7179
responseText: string | null;
@@ -336,15 +344,30 @@ function parseGoogleDecimalPairFromText(input: string): { lat: number; lon: numb
336344
}
337345

338346
function parseGoogleCoordinatesFromText(input: string): { lat: number; lon: number } | null {
339-
return (
340-
parseGooglePreviewCoordinatesFromText(input) ??
341-
parseGoogleStateCoordinatesFromText(input) ??
342-
parseGoogleStaticMapCenterFromText(input) ??
347+
// Match gmaps-coords.ts pattern priority order:
348+
// 1. JSON lat/lng fields (most specific)
349+
// 2. Center array (specific)
350+
// 3. Mobile-specific patterns (PB, state, staticmap)
351+
// 4. Decimal pair scan (last resort - can have false positives)
352+
const result =
343353
parseGoogleJsonLatLngFromText(input) ??
344354
parseGoogleJsonLngLatFromText(input) ??
345355
parseGoogleCenterArrayFromText(input) ??
346-
parseGoogleDecimalPairFromText(input)
347-
);
356+
parseGooglePreviewCoordinatesFromText(input) ??
357+
parseGoogleStateCoordinatesFromText(input) ??
358+
parseGoogleStaticMapCenterFromText(input) ??
359+
parseGoogleDecimalPairFromText(input);
360+
361+
if (result) {
362+
console.info("[share.debug] parse_success_from_html", result);
363+
} else if (input && input.length > 0) {
364+
console.info("[share.debug] parse_all_patterns_failed", {
365+
htmlLength: input.length,
366+
htmlPreview: input.substring(0, 300),
367+
});
368+
}
369+
370+
return result;
348371
}
349372

350373
async function expandShortMapUrlWithResponse(
@@ -378,6 +401,7 @@ async function expandShortMapUrlWithResponse(
378401
fetchImpl(parsed.toString(), {
379402
method: "GET",
380403
redirect: "follow",
404+
headers: BROWSER_HEADERS,
381405
}),
382406
timeoutMs,
383407
);
@@ -439,6 +463,7 @@ async function fetchMapPageText(
439463
fetchImpl(parsed.toString(), {
440464
method: "GET",
441465
redirect: "follow",
466+
headers: BROWSER_HEADERS,
442467
}),
443468
timeoutMs,
444469
);
@@ -471,6 +496,7 @@ async function fetchMapPageText(
471496
fetchImpl(simplifiedUrl, {
472497
method: "GET",
473498
redirect: "follow",
499+
headers: BROWSER_HEADERS,
474500
}),
475501
timeoutMs,
476502
);

apps/mobile/tests/shared-map-link.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,24 @@ describe("shared map link parser", () => {
300300
const parsed = await parseSharedMapLinkAsync("https://maps.app.goo.gl/abc123", { fetchImpl });
301301
expect(parsed).toBeNull();
302302
});
303+
304+
it("parses short link with JSON lat/lng fields in HTML", async () => {
305+
const fetchImpl = vi.fn(async () => ({
306+
url: "https://www.google.com/maps/place/Somewhere",
307+
text: async () =>
308+
'window.APP_INITIALIZATION_STATE=[null,null,null,null,[null,null,null,null,null,null,null,null,"en",null,"GB"],null,[null,null,[1,29.54214059542,-1.55405635],null,null,null,null,null,null,null,null,null,"uk",[["29.54214059542,-1.55405635"],["29.54214059542","-1.55405635"]]],null,null,null,null,null,[[null,null,null,null,"en_GB"]]];{"lat":29.54214059542,"lng":-1.55405635}',
309+
}));
310+
311+
const parsed = await parseSharedMapLinkAsync("https://maps.app.goo.gl/vXkKY6MxxhQgBZ6R6", {
312+
fetchImpl,
313+
});
314+
315+
expect(parsed).toEqual({
316+
provider: "google",
317+
lat: 29.54214059542,
318+
lon: -1.55405635,
319+
rawUrl: "https://maps.app.goo.gl/vXkKY6MxxhQgBZ6R6",
320+
});
321+
});
303322
});
304323
});

0 commit comments

Comments
 (0)