Greasy Fork

Greasy Fork is available in English.

Google Street View Panorama Info

Displays the country name, coordinates, and panoId for a given Google Street View panorama

目前为 2025-01-21 提交的版本,查看 最新版本

// ==UserScript==
// @name         Google Street View Panorama Info
// @description  Displays the country name, coordinates, and panoId for a given Google Street View panorama
// @version      1.4
// @match        https://www.google.*/maps/*
// @grant        GM_setClipboard
// @author       ZecaGeo
// @license      MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=geohints.com
// @namespace    http://greasyfork.icu/users/1340965
// ==/UserScript==

(function () {
    'use strict'

    let panoramaInfo = {
        country: 'Country not found',
        lat: 0,
        lng: 0,
        panoId: ''
    }

    const regex = /@(-?\d+\.\d+),(-?\d+\.\d+).*!1s(.+)!2e/
    const match = window.location.href.match(regex)
    if (!match) {
        console.error('Coordinates not parsed.')
        window.alert(panoramaInfo.country)
        return
    }

    panoramaInfo.lat = match[1]
    panoramaInfo.lng = match[2]
    panoramaInfo.panoId = match[3]

    function updateTitleCard() {
        const addressElement = document.querySelector('.pB8Nmf div')
        if (addressElement) {
            observer.disconnect()

            let countryElement = addressElement.cloneNode(true)
            addressElement.parentNode.insertBefore(countryElement, addressElement.nextSibling)

            let latitudeElement = addressElement.cloneNode(true)
            latitudeElement.querySelector('h2').innerText = panoramaInfo.lat
            latitudeElement.style.cursor = "pointer"
            latitudeElement.onclick = () => GM_setClipboard(panoramaInfo.lat)
            addressElement.parentNode.insertBefore(latitudeElement, countryElement.nextSibling)

            let longitudeElement = addressElement.cloneNode(true)
            longitudeElement.querySelector('h2').innerText = panoramaInfo.lng
            longitudeElement.style.cursor = "pointer"
            longitudeElement.onclick = () => GM_setClipboard(panoramaInfo.lng)
            addressElement.parentNode.insertBefore(longitudeElement, latitudeElement.nextSibling)

            let panoIdElement = addressElement.cloneNode(true)
            panoIdElement.querySelector('h2').innerText = panoramaInfo.panoId
            panoIdElement.style.cursor = "pointer"
            panoIdElement.onclick = () => GM_setClipboard(panoramaInfo.panoId)
            addressElement.parentNode.insertBefore(panoIdElement, longitudeElement.nextSibling)

            getCountry({ lat: panoramaInfo.lat, lon: panoramaInfo.lng })
                .then(country => {
                    countryElement.querySelector('h2').innerText = panoramaInfo.country = country
                })
        }
    }

    function getCountry({ lat, lon }) {
        return fetch(`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`)
            .then(response => response.json())
            .then(data => data?.address?.country ?? "Country not found")
            .catch(error => {
                console.error(error.message)
                return "Country not found"
            })
    }

    const observer = new MutationObserver(updateTitleCard)
    observer.observe(document.body, { childList: true, subtree: true })

})();