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. (Doesn't work on Firefox)

  1. // ==UserScript==
  2. // @name Google Maps Coordinates Grabber
  3. // @version 0.3.2
  4. // @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. (Doesn't work on Firefox)
  5. // @author MrMike
  6. // @namespace https://greasyfork.org/en/users/250979-mrmike
  7. // @include https://*google*/maps/*
  8. // @grant none
  9. // @run-at document-end
  10. // @noframes
  11. // ==/UserScript==
  12.  
  13. // Leave empty to get just the values separated by a comma
  14. // {{lat}} will be replaced with the latitude value and {{lng}} with the longitude value
  15. // Examples: const FORMAT = `"lat": {{lat}}, "lng": {{lng}}`
  16. // Examples: const FORMAT = ``
  17. // const FORMAT = `{ "lat": {{lat}}, "lng": {{lng}} }`;
  18. const FORMAT = `"lat": {{lat}}, "lng": {{lng}}`;
  19.  
  20. let theCopiedCoords;
  21. let theCoords;
  22. let theLatLng;
  23. let theLat;
  24. let theLng;
  25. let tempCard;
  26. let timer = null;
  27.  
  28. document.addEventListener("keypress", (event) => {
  29. if (event.code == "KeyL" && event.ctrlKey && event.shiftKey && !event.altKey && !event.repeat) {
  30. copyCoordinates();
  31. }
  32. });
  33.  
  34. function doTimer() {
  35. let timer = setInterval(() => {
  36. const PIN = document.querySelector("[jsaction='titlecard.exit']");
  37. if (PIN && !document.querySelector("#my-copy")) {
  38. const COPY = document.createElement("span");
  39. COPY.innerHTML = "📋";
  40. COPY.style.cursor = "pointer";
  41. COPY.style.top = "6px";
  42. COPY.style.left = "6px";
  43. COPY.style.position = "absolute";
  44. COPY.setAttribute("id", "my-copy");
  45. COPY.setAttribute("title", "Copy coordinates to clipboard");
  46. COPY.addEventListener("click", copyCoordinates);
  47. PIN.parentNode.insertBefore(COPY, PIN);
  48. clearInterval(timer);
  49. timer = null;
  50. }
  51. }, 500);
  52. }
  53.  
  54. setInterval(() => {
  55. if (!document.querySelector("#my-copy") && timer == null) {
  56. doTimer();
  57. }
  58. }, 1000);
  59.  
  60.  
  61. //Look for card every X time
  62. setInterval(() => {
  63. //Get the card
  64. let theCard = document.getElementById("reveal-card");
  65. //Is the card not null?
  66. if (theCard != null) {
  67. let theDialog = theCard.querySelector("div[role='dialog']");
  68. if (theDialog != null) {
  69. theCoords = theDialog.lastElementChild.innerText;
  70. theLatLng = theCoords.replace(" ", "").split(",");
  71.  
  72. if (theLatLng.length == 2 && theCopiedCoords != theCoords) {
  73. theLat = theLatLng[0];
  74. theLng = theLatLng[1];
  75. if (!isNaN(theLat) && !isNaN(theLng)) {
  76. //Copy this data to clipboard
  77. if (FORMAT != "") {
  78. let out = FORMAT;
  79. copyTextToClipboard(out.replace("{{lat}}", theLat).replace("{{lng}}", theLng));
  80. theCopiedCoords = theCoords;
  81. }
  82. else {
  83. copyTextToClipboard(theCoords);
  84. theCopiedCoords = theCoords;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }, 1000);
  91.  
  92. function copyCoordinates() {
  93. let theLink = location.href;
  94. let values = theLink.substring(theLink.indexOf("@") + 1, theLink.length);
  95. values = values.split(",");
  96. theCoords = values[0] + ", " + values[1];
  97. theLat = values[0];
  98. theLng = values[1];
  99. if (FORMAT != "") {
  100. let out = FORMAT;
  101. copyTextToClipboard(out.replace("{{lat}}", theLat).replace("{{lng}}", theLng));
  102. }
  103. else {
  104. copyTextToClipboard(theCoords);
  105. }
  106. }
  107.  
  108. function doDisplay(theData) {
  109. let theInput = document.getElementById("omnibox");
  110. let inBold = document.createElement("div");
  111.  
  112. inBold.innerHTML = `<b style="font-size:20px;">Copied ${theData}</b>`;
  113. inBold.style.backgroundColor = "#202030CC";
  114. inBold.style.color = "#000099";
  115. inBold.style.width = "100vw";
  116. inBold.style.textAlign = "center";
  117.  
  118. theInput.parentNode.insertBefore(inBold, theInput.nextSibling);
  119.  
  120. setTimeout(() => {
  121. inBold.style.display = "none";
  122. }, 5000);
  123. }
  124.  
  125. function fallbackCopyTextToClipboard(text) {
  126. var textArea = document.createElement("textarea");
  127. textArea.value = text;
  128. document.body.appendChild(textArea);
  129. textArea.focus();
  130. textArea.select();
  131. textArea.setSelectionRange(0, 999999);
  132. try {
  133. var successful = document.execCommand("copy");
  134. if (successful) {
  135. doDisplay(text);
  136. }
  137. } catch (error) {
  138. doDisplay("Error copying to clipboard");
  139. console.log(error);
  140. }
  141. document.body.removeChild(textArea);
  142. }
  143.  
  144. function copyTextToClipboard(text) {
  145. if (!navigator.clipboard) {
  146. fallbackCopyTextToClipboard(text);
  147. return;
  148. }
  149. navigator.clipboard.writeText(text)
  150. .then(() => {
  151. doDisplay(text);
  152. })
  153. .catch((error) => {
  154. doDisplay("Error copying to clipboard");
  155. console.log(error);
  156. });
  157.  
  158. }
  159.  
  160.  
  161. doTimer();
  162.