DEV Community

Weather Clock Dash
Weather Clock Dash

Posted on

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

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.

Privacy Considerations

IP geolocation does not require you to send the user's IP to a third party — the user's browser is already making the request to get their own IP info. However, you should:

  1. Disclose this in your privacy policy
  2. Cache the result to avoid hitting the API on every new tab open
  3. Allow users to manually override their city

The Fallback: Let Users Type Their City

The most privacy-friendly approach is to let users type their own city:

// First-run setup
const { city } = await browser.storage.local.get("city");
if (!city) {
  // Show a setup screen asking for city
  showCityInput();
} else {
  loadWeather(city);
}
Enter fullscreen mode Exit fullscreen mode

This is what the Weather & Clock Dashboard does — no automatic location detection, no permissions requested. The user types their city once and it is saved locally.

Which to Choose?

Approach Accuracy Privacy UX
GPS (geolocation API) Precise Requires permission prompt Scary for new tab
IP geolocation City-level No prompt, but uses external API Seamless
Manual input User-specified No external APIs One-time setup friction

For a new tab extension where weather is secondary, manual input respects users the most. For a weather-first extension where accuracy matters, IP geolocation is the right trade-off.

Top comments (0)