Menu

Post image 1
Post image 2
1 / 2
0

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

DEV Community·Weather Clock Dash·29 days ago
#TYCqDYCX
#privacy#firefox#webdev#city#user#geolocation
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 . timezone , }; } Enter fullscreen mode Exit fullscreen mode This requires no permissions, no user prompt, and works in any browser extension context.…

Continue reading — create a free account

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

Read More