Google Maps Coordinates Grabber

Once in Google Maps, click on the map (Do not click on markers) a popup should show at the bottom with the place and the coordinates displayed in the last line. If everything works as expected you should see a line of text on top of the map with the confirmation of the coordinates been copied to clipboard. Press Control + Shift + L while on streetview to get the coordinates from the URL.

当前为 2021-08-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Google Maps Coordinates Grabber
  3. // @version 0.3.1
  4. // @namespace https://greasyfork.org/en/users/250979-mrmike
  5. // @author MrMike
  6. // @description Once in Google Maps, click on the map (Do not click on markers) a popup should show at the bottom with the place and the coordinates displayed in the last line. If everything works as expected you should see a line of text on top of the map with the confirmation of the coordinates been copied to clipboard. Press Control + Shift + L while on streetview to get the coordinates from the URL.
  7. // @grant none
  8. // @include http*://*google*/maps/*
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. // Leave empty to get just the values separated by a comma
  13. // Example: `"lat": {{lat}}, "lng": {{lng}}`
  14. const FORMAT = `{ "lat": {{lat}}, "lng": {{lng}} }`;
  15.  
  16. let theCopiedCoords;
  17. let theCoords;
  18. let theLatLng;
  19. let theLat;
  20. let theLng;
  21. let tempCard;
  22.  
  23. document.addEventListener("keypress", (event) => {
  24. if(event.code == "KeyL" && event.ctrlKey && event.shiftKey && !event.altKey && !event.repeat){
  25. let theLink = location.href;
  26. let values = theLink.substring(theLink.indexOf("@")+1, theLink.length);
  27. values = values.split(",");
  28. theCoords = values[0] + ", " + values[1];
  29. theLat = values[0];
  30. theLng = values[1];
  31. if(FORMAT != ""){
  32. let out = FORMAT;
  33. copyTextToClipboard(out.replace("{{lat}}", theLat).replace("{{lng}}", theLng));
  34. }
  35. else{
  36. copyTextToClipboard(theCoords);
  37. }
  38. }
  39. });
  40.  
  41. //Look for card every X time
  42. setInterval(() => {
  43. //Get the card
  44. let theCard = document.getElementById("reveal-card");
  45. //Is the card not null?
  46. if (theCard != null) {
  47. let theDialog = theCard.querySelector("div[role='dialog']");
  48. if (theDialog != null) {
  49. theCoords = theDialog.lastElementChild.innerText;
  50. theLatLng = theCoords.replace(" ", "").split(",");
  51.  
  52. if (theLatLng.length == 2 && theCopiedCoords != theCoords) {
  53. theLat = theLatLng[0];
  54. theLng = theLatLng[1];
  55. if (!isNaN(theLat) && !isNaN(theLng)) {
  56. //Copy this data to clipboard
  57. if(FORMAT != ""){
  58. let out = FORMAT;
  59. copyTextToClipboard(out.replace("{{lat}}", theLat).replace("{{lng}}", theLng));
  60. theCopiedCoords = theCoords;
  61. }
  62. else{
  63. copyTextToClipboard(theCoords);
  64. theCopiedCoords = theCoords;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }, 1000);
  71.  
  72. function doDisplay(theData) {
  73. let theInput = document.getElementById("omnibox");
  74. let inBold = document.createElement("div");
  75.  
  76. inBold.innerHTML = "<b style='font-size:20px'>Copied " + theData + "</b>";
  77. inBold.style.backgroundColor = "#202030CC";
  78. inBold.style.color = "#000099";
  79. inBold.style.width = "100vw";
  80. inBold.style.textAlign = "center";
  81.  
  82. theInput.parentNode.insertBefore(inBold, theInput.nextSibling);
  83.  
  84. setTimeout(() => {
  85. inBold.style.display = "none";
  86. }, 5000);
  87. }
  88.  
  89. function fallbackCopyTextToClipboard(text, element) {
  90. var textArea = document.createElement("textarea");
  91. textArea.value = text;
  92. document.body.appendChild(textArea);
  93. textArea.focus();
  94. textArea.select();
  95. try {
  96. var successful = document.execCommand('copy');
  97. if (successful) {
  98. doDisplay(text);
  99. }
  100. } catch (err) {
  101. }
  102. document.body.removeChild(textArea);
  103. }
  104.  
  105. function copyTextToClipboard(text, element) {
  106. if (!navigator.clipboard) {
  107. fallbackCopyTextToClipboard(text, element);
  108. return;
  109. }
  110. navigator.clipboard.writeText(text)
  111. .then(() => {
  112. doDisplay(text);
  113. }, (err) => {});
  114. }