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.

当前为 2020-10-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Google Maps Coordinates Grabber
  3. // @version 0.2.3
  4. // @namespace https://greasyfork.org/en/users/250979-mrmike
  5. // @author MrAmericanMike
  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.
  7. // @grant none
  8. // @include http*://*google*/maps/*
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. // Leave empty to get just the values separated by a comma
  13. // Example: `"lat": {{lat}}, "lng": {{lng}}`
  14. const FORMAT = ``;
  15.  
  16. let theLatLng;
  17. let theLat;
  18. let theLng;
  19. let tempCard;
  20.  
  21.  
  22. document.addEventListener("keypress", (event) => {
  23. if(event.key == "l"){
  24. let theLink = location.href;
  25. let values = theLink.substring(theLink.indexOf("@")+1, theLink.length);
  26. values = values.split(",");
  27. let coordinates = values[0] + ", " + values[1];
  28. copyTextToClipboard(coordinates);
  29. }
  30. });
  31.  
  32.  
  33. //Look for card every X time
  34. setInterval(() => {
  35. //Get the card
  36. let theCard = document.getElementById("reveal-card");
  37.  
  38. //Is the card not null?
  39. if (theCard != null) {
  40. //Get card lat-lng
  41. let newLatLng = theCard.getElementsByClassName("widget-reveal-card-lat-lng");
  42.  
  43. //Are childs not null and have some content?
  44. if (newLatLng != null && newLatLng.length > 0) {
  45. //Is the Lat Lng line different from a previous one?
  46. if (theLatLng != newLatLng[0].innerText && newLatLng[0].innerText != "") {
  47. //Set the new line in a temporal vaiable
  48. theLatLng = newLatLng[0].innerText;
  49. //Split last line by comma and remove spaces
  50. let theCoords = theLatLng.replace(" ", "").split(",");
  51. //Did we get 2 values?
  52. if (theCoords.length == 2) {
  53. theLat = theCoords[0];
  54. theLng = theCoords[1];
  55. //Are both values numbers?
  56. if (!isNaN(theLat) && !isNaN(theLng)) {
  57. //Copy this data to clipboard
  58. if(FORMAT != ""){
  59. let out = FORMAT;
  60. copyTextToClipboard(out.replace("{{lat}}", theLat).replace("{{lng}}", theLng));
  61. }
  62. else{
  63. copyTextToClipboard(theLatLng);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }, 1000);
  71.  
  72.  
  73. function doDisplay(theData) {
  74. let theInput = document.getElementById("omnibox");
  75. let inBold = document.createElement("div");
  76.  
  77. inBold.innerHTML = "<b style='font-size:20px'>Copied " + theData + "</b>";
  78. inBold.style.backgroundColor = "#FFFFFFCC";
  79. inBold.style.color = "#000099";
  80. inBold.style.width = "100vw";
  81. inBold.style.textAlign = "center";
  82.  
  83. theInput.parentNode.insertBefore(inBold, theInput.nextSibling);
  84.  
  85. setTimeout(() => {
  86. inBold.style.display = "none";
  87. }, 5000);
  88. }
  89.  
  90. function fallbackCopyTextToClipboard(text, element) {
  91. var textArea = document.createElement("textarea");
  92. textArea.value = text;
  93. document.body.appendChild(textArea);
  94. textArea.focus();
  95. textArea.select();
  96. try {
  97. var successful = document.execCommand('copy');
  98. if (successful) {
  99. doDisplay(text);
  100. }
  101. } catch (err) {
  102. }
  103. document.body.removeChild(textArea);
  104. }
  105.  
  106. function copyTextToClipboard(text, element) {
  107. if (!navigator.clipboard) {
  108. fallbackCopyTextToClipboard(text, element);
  109. return;
  110. }
  111. navigator.clipboard.writeText(text)
  112. .then(() => {
  113. doDisplay(text);
  114. }, (err) => {});
  115. }