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

// ==UserScript==
// @name     		Google Maps Coordinates Grabber
// @version  		0.2.1
// @namespace   https://greasyfork.org/en/users/250979-mrmike
// @author      MrAmericanMike
// @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.
// @grant 		  none
// @include  		http*://*google*/maps/*
// @run-at   		document-end
// ==/UserScript==


let theLatLng;
let theLat;
let theLng;
let tempCard;


document.addEventListener("keypress", (event) => {
  if(event.key == "l"){
    let theLink = location.href;
    
    let values = theLink.substring(theLink.indexOf("@")+1, theLink.length);
    values = values.split(",");
    let coordinates = values[0] + ", " + values[1];
    copyTextToClipboard(coordinates);
  }
});


//Look for card every X time
setInterval(() => {
    
  //Get the card
  let theCard = document.getElementById("reveal-card");

  //Is the card not null?
  if (theCard != null) {
    
    //Get card lat-lng
    let newLatLng = theCard.getElementsByClassName("widget-reveal-card-lat-lng");

    //Are childs not null and have some content?
    if (newLatLng != null && newLatLng.length >= 0) {
            
      //Is the Lat Lng line different from a previous one?
      if (theLatLng != newLatLng[0].innerText && newLatLng[0].innerText != "") {
                
        //Set the new line in a temporal vaiable
        theLatLng = newLatLng[0].innerText;
        
        //Split last line by comma and remove spaces
        let theCoords = theLatLng.replace(" ", "").split(",");
        
        //Did we get 2 values?
        if (theCoords.length == 2) {
          
          theLat = theCoords[0];
          theLng = theCoords[1];
          
          //Are both values numbers?
          if (!isNaN(theLat) && !isNaN(theLng)) {
            
            //Copy this data to clipboard
            copyTextToClipboard(theLatLng);
          }
        }
      }
    }
  }
}, 1000);


function doDisplay(theData) {
	let theInput = document.getElementById("omnibox");
	let inBold = document.createElement("div");

	inBold.innerHTML = "<b style='font-size:20px'>Copied " + theData + "</b>";
	inBold.style.backgroundColor = "#FFFFFFCC";
	inBold.style.color = "#000099";
	inBold.style.width = "100vw";
	inBold.style.textAlign = "center";

	theInput.parentNode.insertBefore(inBold, theInput.nextSibling);

	setTimeout(() => {
		inBold.style.display = "none";
	}, 5000);
}

function fallbackCopyTextToClipboard(text, element) {
    var textArea = document.createElement("textarea");
    textArea.value = text;
    document.body.appendChild(textArea);
    textArea.focus();
    textArea.select();
  
    try {
      var successful = document.execCommand('copy');
      if (successful) {
        doDisplay(text);
      }
    } catch (err) {
    }
  
    document.body.removeChild(textArea);
}

function copyTextToClipboard(text, element) {
    if (!navigator.clipboard) {
      fallbackCopyTextToClipboard(text, element);
      return;
    }
    navigator.clipboard.writeText(text)
      .then(() => {
	      doDisplay(text);
	    }, (err) => {});
}