Greasy Fork

来自缓存

Greasy Fork is available in English.

Google Street View Panorama Info

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

当前为 2025-01-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         Google Street View Panorama Info
// @description  Displays the country name and coordinates for a Google Street View panorama
// @version      1.1
// @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
    }

    const regex = /@(-?\d+\.\d+),(-?\d+\.\d+)/
    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]

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

        if (addressElement) {
            observer.disconnect()

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

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

            console.log(addressElement.parentNode)

            getCountry({ lat: panoramaInfo.lat, lon: panoramaInfo.lng })
                .then(country => {
                    panoramaInfo.country = country
                    const output = `Country: ${country}\nLatitude: ${panoramaInfo.lat}\nLongitude: ${panoramaInfo.lng}`
                    GM_setClipboard(output)
                    console.log(output)

                    countryElement.querySelector('h2').innerText = country
                    coordinatesElement.querySelector('h2').innerText = `${panoramaInfo.lat}, ${panoramaInfo.lng}`
                })
        }
    }

    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 })

})();