OpenStreetMap IP Geolocation

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

  1. // ==UserScript==
  2. // @name OpenStreetMap IP Geolocation
  3. // @namespace Violentmonkey Scripts
  4. // @match *://www.openstreetmap.org/*
  5. // @grant GM.xmlhttpRequest
  6. // @connect ip-api.com
  7. // @version 1.2
  8. // @author CyrilSLi
  9. // @description Overrides the "Show My Location" button to provide location from IP geolocation
  10. // @license MIT
  11. // @require https://update.greatest.deepsurf.us/scripts/533461/1574689/Get%20OpenStreetMap%20Leaflet%20object.js
  12. // ==/UserScript==
  13.  
  14. window.addEventListener("load", () => {
  15. if (!unsafeWindow.userscriptMap) {
  16. return;
  17. }
  18. GM.xmlhttpRequest({
  19. method: "GET",
  20. url: "http://ip-api.com/json/",
  21. onload: function(response) {
  22. ipJSON = JSON.parse(response.responseText);
  23. geoButton = document.getElementsByClassName("icon geolocate")[0].parentElement;
  24. geoButton.parentNode.replaceChild(geoButton.cloneNode(true), geoButton);
  25. tooltips = document.getElementsByClassName("tooltip-inner");
  26. while(tooltips[0]) {
  27. container = tooltips[0].parentNode;
  28. container.parentNode.removeChild(container);
  29. }
  30. geoButton = document.getElementsByClassName("icon geolocate")[0].parentElement;
  31. geoButton.addEventListener("click", ev => {
  32. unsafeWindow.userscriptMap.setView([ipJSON["lat"], ipJSON["lon"]], 12);
  33. })
  34. keyButton = document.getElementsByClassName("control-key")[0].children[0];
  35. keyButton.click();
  36. keyButton.click();
  37. }
  38. });
  39. });