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-07-31 提交的版本,查看 最新版本

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