OpenStreetMap IP Geolocation

Overrides the "Show My Location" button to provide location from IP geolocation

As of 2025-01-12. See the latest version.

// ==UserScript==
// @name        OpenStreetMap IP Geolocation
// @namespace   Violentmonkey Scripts
// @match       *://www.openstreetmap.org/*
// @grant       GM.xmlhttpRequest
// @connect     ip-api.com
// @version     1.0
// @author      CyrilSLi
// @description Overrides the "Show My Location" button to provide location from IP geolocation
// @license     MIT
// ==/UserScript==

GM.xmlhttpRequest({
  method: "GET",
  url: "http://ip-api.com/json/",
  onload: function(response) {
    ipJSON = JSON.parse(response.responseText);
    geoButton = document.getElementsByClassName("icon geolocate")[0].parentElement;
    geoButton.parentNode.replaceChild(geoButton.cloneNode(true), geoButton);
    geoButton = document.getElementsByClassName("icon geolocate")[0].parentElement;
    geoButton.addEventListener("click", ev => {
      console.log(ipJSON);
      location.href = `#map=12/${ipJSON["lat"]}/${ipJSON["lon"]}`;
      location.reload(true);
    })
  }
})