我想将我们的浏览器位置更改为美国请帮助我更改firefox位置,即地理定位位置以进行测试。
nmaler提到的两个首选项是定点。此外,您不需要(本地)服务器,数据:-URI也可以正常工作。
例如,如果您使用值设置首选项geo.wifi.uri
(在about:config
):
data:,{"location":{"lat":1.2,"lng":3.4},"accuracy":4000}
然后通过从JS控制台运行以下代码进行测试:
navigator.geolocation.getCurrentPosition(pos => console.log(pos.coords));
然后你会看到恶搞成功了:
Coordinates { latitude: 1.3, longitude: 11, altitude: 0, accuracy: 4000, ... }
如果您需要生成有效数据:-URL with JavaScript(例如,在附加代码中),请使用以下内容。请注意,我使用encodeURIComponent
以防pos
对象以某种方式包含特殊字符,如%
或#
(这不太可能,但比抱歉更安全):
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.
最简单的方法是设置自己的地理定位模拟服务器并更改一些首选项:
geo.provider.testing
,将其设置为true
。这将强制网络提供商(而不是OS级别的地理位置提供商,如果有的话)。geo.wifi.uri
更改为模拟服务器的URI,例如http://localhost:8888/
您可以在about:config
中更改首选项,也可以直接编辑浏览器配置文件的prefs.js
文件。打开配置文件目录的最简单方法是使用about:support
中的相应按钮。
python2中的示例模拟服务器(总是返回白宫的坐标):
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
如果您想欺骗HTML5 Geolocation API的位置,您可以按照以下步骤操作:
about:config
。geo.wifi.uri
。lat
和lng
值决定了您所在位置的纬度和经度。)请注意,如果您想阻止网站从您的IP地址获取位置,则无法在应用层上执行此操作 - 唯一的方法是代理。