Menu

1 / 2

Auto-Detecting Location in a Firefox Extension Without Asking for GPS

DEV Community·Weather Clock Dash·7 months ago
#TYCqDYCX
#privacy#firefox#webdev#city#user
Reading 0:00
15s threshold

Auto-Detecting Location Without Asking for GPS

For the Weather & Clock Dashboard Firefox extension, I needed the user's city without requiring the geolocation permission (which triggers a scary browser prompt).

Here is the approach I used.

The Problem with navigator.geolocation

The geolocation permission triggers a modal asking the user to share their precise location. For a new tab weather extension, this feels invasive. The user just wanted weather — they did not expect a location permission prompt.

Solution: IP-Based Geolocation

For weather purposes, city-level accuracy is enough. IP geolocation gives you the city without any user prompt:

async function getLocationFromIP () { const resp = await fetch ( " https://ipapi.co/json/ " ); const data = await resp . json (); return { city : data . city , country : data . country_name , timezone : data .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.