OpenInJosm

Open selected element in Josm

  1. // ==UserScript==
  2. // @name OpenInJosm
  3. // @namespace https://github.com/infeeeee/userscripts
  4. // @version 0.4
  5. // @description Open selected element in Josm
  6. // @author infeeeee
  7. // @match *://*.openstreetmap.org/*
  8. // @exclude *://*.openstreetmap.org/id*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12.  
  13. function openJosm() {
  14. let id = location.href.match(/www\.openstreetmap\.org\/(?:(relation|node|way|changeset|note)\/(\d+))?(?:.*(\#map=)(\d{1,2}(?:\/-?[\d\.]+){2}))?/)
  15. console.log(id)
  16. if (id) {
  17. switch (id[1]) {
  18. case "changeset":
  19. window.open('http://127.0.0.1:8111/import?url=https://www.openstreetmap.org/api/0.6/' + id[1] + '/' + id[2] + '/download')
  20. break;
  21.  
  22. case "note":
  23. window.open('http://127.0.0.1:8111/import?url=https://www.openstreetmap.org/api/0.6/notes/' + id[2])
  24. break;
  25.  
  26. default: {
  27. if (id[4]) {
  28. let zll = id[4].split("/")
  29. let bbox = calcBbox(zll)
  30. if (id[1]) {
  31. window.open('http://127.0.0.1:8111/load_and_zoom?left=' + bbox[0] + '&right=' + bbox[1] + '&top=' + bbox[2] + '&bottom=' + bbox[3] + '&select=' + id[1] + id[2])
  32. } else {
  33. window.open('http://127.0.0.1:8111/load_and_zoom?left=' + bbox[0] + '&right=' + bbox[1] + '&top=' + bbox[2] + '&bottom=' + bbox[3])
  34. }
  35. } else {
  36. let obj = id[1].split("")[0]
  37. window.open('http://127.0.0.1:8111/load_object?objects=' + obj + id[2])
  38. }
  39. break;
  40. }
  41. }
  42. } else {
  43. alert('Something is wrong! Please open an issue on Github and include your current url!')
  44. }
  45. }
  46.  
  47. function calcBbox(zll) {
  48. let zoom = parseInt(zll[0])
  49. let x = parseFloat(zll[1])
  50. let y = parseFloat(zll[2])
  51.  
  52. let left = y - 0.005
  53. let right = y + 0.005
  54. let top = x + 0.005
  55. let bottom = x - 0.005
  56.  
  57. return [left.toFixed(4), right.toFixed(4), top.toFixed(4), bottom.toFixed(4)]
  58.  
  59. }
  60.  
  61. (function () {
  62. 'use strict';
  63.  
  64. const element = document.createElement("a")
  65. element.innerHTML = "Open in JOSM"
  66.  
  67. element.style.cssText = "height: 100%; margin-left: 1rem; padding: 0 0.75rem; cursor: pointer; border: 1px solid #7ebc6f; border-radius: 3px; background: white; color: #7ebc6f; vertical-align: middle; display: inline-flex; align-items: center;";
  68. element.style.zIndex = 99999
  69. element.onmouseenter = () => {
  70. element.style.background = "#7ebc6f"
  71. element.style.color = "#fff"
  72. }
  73. element.onmouseleave = () => {
  74. element.style.background = "#fff"
  75. element.style.color = "#7ebc6f"
  76. }
  77. element.onclick = () => {
  78. openJosm()
  79. }
  80.  
  81. document.getElementsByClassName("primary")[0].appendChild(element)
  82.  
  83. })()