How to change firefox Location setting

15,485

Solution 1

The two preferences mentioned by nmaler are spot-on. Additionally, you don't need a (local) server, a data:-URI works fine too.

For example, if you set the preference geo.wifi.uri (at about:config) with value:

data:,{"location":{"lat":1.2,"lng":3.4},"accuracy":4000}

and then test by running the following from the JS console:

navigator.geolocation.getCurrentPosition(pos => console.log(pos.coords));

then you will see that the spoof succeeded:

Coordinates { latitude: 1.3, longitude: 11, altitude: 0, accuracy: 4000, ... }

If you need to generate a valid data:-URL with JavaScript (e.g. in add-on code), use the following. Note that I use encodeURIComponent just in case the pos object somehow contains special characters such as % or # (this is unlikely, but better safe than sorry):

var pos = {
    location: {
        lat: 1.2,
        lng: 3.4,
    },
    accuracy: 4000,
};
var geoWifiUrl = `data:,${encodeURIComponent(JSON.stringify(pos))}`;
// TODO: Set geo.wifi.url's pref to geoWifiUrl.

Solution 2

The easiest way would be to setup your own geolocation mock server and change some preferences:

  1. Create (or change) the boolean geo.provider.testing, setting it to true. This will force the network provider on (instead of an OS level geo location provider, if any).
  2. Change the geo.wifi.uri to the URI of your mock server, e.g. http://localhost:8888/
  3. Start your mock server and restart Firefox.
  4. Test that stuff works, e.g.

You can change preferences in about:config or by editing the prefs.js file of your browser profile directly. Easiest way to open the profile directory is using the corresponding button in about:support.

Sample mock server in python2 (returning always the coordinates for the White House):

import json
import BaseHTTPServer
import SocketServer

PORT = 8888
LAT, LNG = 38.894967, -77.034917


class GeoHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "application/json")
        self.end_headers()
        self.wfile.write(json.dumps({
            "location": {
                "lat": LAT,
                "lng": LNG
            },
            "accuracy": 4000
            }))

    def do_POST(self):
        return self.do_GET()

httpd = SocketServer.TCPServer(("", PORT), GeoHandler)
print "serving at port

Solution 3

If you want to spoof your location for the HTML5 Geolocation API you can follow these steps:

  • Go to about:config.
  • Type in geo.wifi.uri.
  • Change the value to something like this:

    data:application/json,{"location": {"lat": 40.7590, "lng": -73.9845}, "accuracy": 27000.0}
    

    (The lat and lng values determine the latitude and longitude of your location.)

  • Congratulations, you're now on Times Square! (You can test the result here.)

Note that if you want to prevent websites from deriving the location from your IP address you can't do that on the application layer - the only way will be a proxy.

Share:
15,485
Charnjeet Singh
Author by

Charnjeet Singh

Crazy about Programming....

Updated on June 15, 2022

Comments

  • Charnjeet Singh
    Charnjeet Singh almost 2 years

    I want to Change our Browser location to usa Please help me changing firefox location i.e geolocation location for testing.

  • nmaier
    nmaier almost 10 years
    @Blagoh OP wanted "Change our Browser location to usa". What is more "USA" than the White House? The WalMart or MacDownloads or CocaCola HQs maybe :p
  • Blagoh
    Blagoh almost 10 years
    I was posting to agree with McDonalads and then on second pass I realized the spelling... ROFL
  • nmaier
    nmaier almost 10 years
    @Blagoh Damn... That's what you get for seeing that crappy MacBook line in front of you all day. :P
  • Blagoh
    Blagoh almost 10 years
    I'm Windoze 7.0!! (No witty spelling on MacBook? :P)
  • Noitidart
    Noitidart over 9 years
    This is fantastic. We need to find a pure javascript way to do this, I recommended this add-on use this: addons.mozilla.org/en-US/firefox/addon/location-guard
  • Noitidart
    Noitidart over 7 years
    Hey @nmaier I just made an addon out of this - addons.mozilla.org/en-US/firefox/addon/fake-location - super basic, someone on reddit requested something like it - reddit.com/r/firefox/comments/59ucfh/… - I used a server, as I couldn't figure out how to do it with pure js :(
  • smammy
    smammy about 5 years
    If you are on macOS, you also need to set geo.provider.use_corelocation to false and restart Firefox. (geo.provider.use_corelocation takes precedence over geo.wifi.uri.) Also, geo.provider.testing doesn't seem to be required, at least on Firefox 66.
  • user1511417
    user1511417 almost 5 years
    I've tested this. It works!! Thanks for sharing this.