OpenInJosm

Open selected element in Josm

当前为 2020-02-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name OpenInJosm
  3. // @namespace https://github.com/infeeeee/userscripts
  4. // @version 0.3
  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. case "note":
  20. window.open('http://127.0.0.1:8111/import?url=https://www.openstreetmap.org/api/0.6/' + id[1] + '/' + id[2] + '/download')
  21. break;
  22.  
  23. case undefined: {
  24. let zll = id[4].split("/")
  25. let bbox = calcBbox(zll)
  26. window.open('http://127.0.0.1:8111/load_and_zoom?left=' + bbox[0] + '&right=' + bbox[1] + '&top=' + bbox[2] + '&bottom=' + bbox[3])
  27. break;
  28. }
  29. default: {
  30. let obj = id[1].split("")[0]
  31. window.open('http://127.0.0.1:8111/load_object?objects=' + obj + id[2])
  32. break;
  33. }
  34. }
  35. } else {
  36. alert('Something is wrong! Please open an issue on Github and include your current url!')
  37. }
  38. }
  39.  
  40. function calcBbox(zll) {
  41. let zoom = parseInt(zll[0])
  42. let x = parseFloat(zll[1])
  43. let y = parseFloat(zll[2])
  44.  
  45. let left = y - 0.005
  46. let right = y + 0.005
  47. let top = x + 0.005
  48. let bottom = x - 0.005
  49.  
  50. return [left.toFixed(4), right.toFixed(4), top.toFixed(4), bottom.toFixed(4)]
  51.  
  52. }
  53.  
  54. (function () {
  55. 'use strict';
  56.  
  57. const element = document.createElement("a")
  58. element.innerHTML = "Open in JOSM"
  59.  
  60. 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;";
  61. element.style.zIndex = 99999
  62. element.onmouseenter = () => {
  63. element.style.background = "#7ebc6f"
  64. element.style.color = "#fff"
  65. }
  66. element.onmouseleave = () => {
  67. element.style.background = "#fff"
  68. element.style.color = "#7ebc6f"
  69. }
  70. element.onclick = () => {
  71. openJosm()
  72. }
  73.  
  74. document.getElementsByClassName("primary")[0].appendChild(element)
  75.  
  76. })()