图寻复盘工具

将图寻复盘链接转换成谷歌或百度街景链接,显示地址信息和拍摄时间以及百度街景的panoId

12.07.2024 itibariyledir. En son verisyonu görün.

// ==UserScript==
// @name         图寻复盘工具
// @namespace    https://greasyfork.org/users/1179204
// @version      1.0.4
// @description  将图寻复盘链接转换成谷歌或百度街景链接,显示地址信息和拍摄时间以及百度街景的panoId
// @match        *://tuxun.fun/replay-pano?gameId=*&round=*
// @icon         data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgNDggNDgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbD0iIzAwMDAwMCI+PGcgaWQ9IlNWR1JlcG9fYmdDYXJyaWVyIiBzdHJva2Utd2lkdGg9IjAiPjwvZz48ZyBpZD0iU1ZHUmVwb190cmFjZXJDYXJyaWVyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiPjwvZz48ZyBpZD0iU1ZHUmVwb19pY29uQ2FycmllciI+PHRpdGxlPjcwIEJhc2ljIGljb25zIGJ5IFhpY29ucy5jbzwvdGl0bGU+PHBhdGggZD0iTTI0LDEuMzJjLTkuOTIsMC0xOCw3LjgtMTgsMTcuMzhBMTYuODMsMTYuODMsMCwwLDAsOS41NywyOS4wOWwxMi44NCwxNi44YTIsMiwwLDAsMCwzLjE4LDBsMTIuODQtMTYuOEExNi44NCwxNi44NCwwLDAsMCw0MiwxOC43QzQyLDkuMTIsMzMuOTIsMS4zMiwyNCwxLjMyWiIgZmlsbD0iI2ZmOTQyNyI+PC9wYXRoPjxwYXRoIGQ9Ik0yNS4zNywxMi4xM2E3LDcsMCwxLDAsNS41LDUuNUE3LDcsMCwwLDAsMjUuMzcsMTIuMTNaIiBmaWxsPSIjZmZmZmZmIj48L3BhdGg+PC9nPjwvc3ZnPg==
// @author       KaKa
// @grant        GM_setClipboard
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @require      https://cdn.jsdelivr.net/npm/sweetalert2@11
// @copyright    KaKa
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    GM_addStyle(`
    #coordinates-container {
        position: fixed;
        top: 100px;
        left: 10px;
        padding: 10px;
        border-radius: 20px !important;
        z-index: 1000;
        display: flex;
        flex-direction: column;
        width: 180px;
    }

    #coordinates-container button {
        cursor: pointer;
        width: 100% !important;
        font-weight: bold !important;
        border: 8px solid #000000 !important;
        text-align: left !important;
        padding-left: 8px !important;
        padding-right: 8px !important;
        backdrop-filter: blur(10px);
        margin-bottom: 5px;
        border-radius: 4px;
        background-color: #000000 !important;
        color: #A0A0A0 !important;
    };

    #conversion-mode {
        font-family: Arial, sans-serif !important;
        color: #000000 !important;
        text-shadow: -1px -1px 0 #A0A0A0, 1px -1px 0 #A0A0A0, -1px 1px 0 #A0A0A0, 1px 1px 0 #A0A0A0 !important;
        margin-bottom: 5px !important;
    }

    .link-button {
        background: none!important;
        border: none;
        padding: 0!important;
        color: #FFCC00 !important;
        text-decoration: underline;
        cursor: pointer;
    }

    .link-button:hover {
        color: #FFCC00 !important;
    }
`);
    let api_key=JSON.parse(localStorage.getItem('api_key'));
    if(!api_key){
        Swal.fire({
            title: '请输入您的高德地图 API 密钥',
            input: 'text',
            inputPlaceholder: '',
            showCancelButton: true,
            confirmButtonText: '保存',
            cancelButtonText: '取消',
            preConfirm: (inputValue) => {
                if (!inputValue) {
                    Swal.fire('未获取到API密钥', '如果没有高德地图API密钥,脚本将无法显示地址信息!','warning')
                } else {
                    localStorage.setItem('api_key', JSON.stringify(inputValue));
                    return inputValue;
                }
            }
        }).then((result) => {
            if (result.isConfirmed) {
                api_key=JSON.parse(localStorage.getItem('api_key'));
                Swal.fire('保存成功!', '您的API密钥已保存,请刷新页面。', 'success');
            }
        });
    }
    const container = document.createElement('div');
    container.id = 'coordinates-container';
    document.body.appendChild(container);

    const conversionModeLabel = document.createElement('div');
    conversionModeLabel.id = 'conversion-mode';
    conversionModeLabel.style.display='none'
    container.appendChild(conversionModeLabel);


    const openButton = document.createElement('button');
    openButton.textContent = '在地图中打开';
    container.appendChild(openButton);

    const copyButton = document.createElement('button');
    copyButton.textContent = '复制街景链接';
    container.appendChild(copyButton);

    let currentLink = '';
    let globalPanoId=null
    openButton.onclick = () => window.open(currentLink, '_blank');
    copyButton.onclick = () => {
        GM_setClipboard(currentLink, 'text');
        setTimeout(function() {
            copyButton.textContent='复制成功!'
        }, 100)
        setTimeout(function() {
            copyButton.textContent='复制街景链接'
        }, 1600)
    };


    const areaButton = document.createElement('button');
    areaButton.textContent = 'Area';
    container.appendChild(areaButton);

    const streetButton = document.createElement('button');
    streetButton.textContent = 'Street';
    container.appendChild(streetButton);

    const timeButton = document.createElement('button');
    timeButton.textContent = 'Time';
    container.appendChild(timeButton);

    const panoIdButton = document.createElement('button');
    timeButton.textContent = 'PanoId';
    container.appendChild(panoIdButton);

    let globalTimeInfo = null;
    let globalAreaInfo = null;
    let globalStreetInfo = null;

    function updateButtonContent() {
        areaButton.textContent = globalAreaInfo ? `${globalAreaInfo}` : 'Area';
        streetButton.textContent = globalStreetInfo ? `${globalStreetInfo}` : 'Street';
        timeButton.textContent = globalTimeInfo ? `${globalTimeInfo}` : 'Time';
        panoIdButton.textContent=globalPanoId ? `${globalPanoId.substring(6,10)}, ${globalPanoId.substring(25,27)}` : 'Unknown'
        panoIdButton.style.fontSize='15px'
    }

    setInterval(updateButtonContent, 1000);
    var realSend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function(value) {
        this.addEventListener('load', function() {
            if (this._url && this._url.includes('https://tuxun.fun/api/v0/tuxun/mapProxy/getGooglePanoInfoPost')) {
                const responseText = this.responseText;
                const coordinatePattern = /\[\[null,null,(-?\d+\.\d+),(-?\d+\.\d+)\],\[\d+\.\d+\],\[\d+\.\d+,\d+\.\d+,\d+\.\d+\]\]|\[\s*null,\s*null,\s*(-?\d+\.\d+),\s*(-?\d+\.\d+)\s*\]/;
                const coordinateMatches = coordinatePattern.exec(responseText);
                if (coordinateMatches) {
                    const latitude = coordinateMatches[1] || coordinateMatches[3];
                    const longitude = coordinateMatches[2] || coordinateMatches[4];
                    if (latitude && longitude) {
                        currentLink = `https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=${latitude},${longitude}`;
                    }
                }

                const countryPattern = /,\s*"([A-Z]{2})"\s*\],null,\[/;
                const countryMatches = countryPattern.exec(responseText);
                let countryCode = countryMatches ? countryMatches[1] : '未知国家';

                const areaPattern = /\[\[\s*"([^"]+)",\s*"[a-z]{2}"\s*\],\s*\["([^"]+)",\s*"zh"\s*\]\]/;
                const areaMatches = areaPattern.exec(responseText);
                if (areaMatches && areaMatches.length >= 3) {
                    globalAreaInfo = `${countryCode}, ${areaMatches[2]}`;
                }


                const fullAddressPattern = /\[\s*null,\s*null,\s*\[\s*\["([^"]+)",\s*"[a-z]{2}"\s*\]\]/;
                const addressMatches = fullAddressPattern.exec(responseText);

                if (addressMatches && addressMatches.length > 1) {
                    globalStreetInfo = addressMatches[1];
                } else {
                    globalStreetInfo = '未知地址';
                }

                const timePattern = /\[\d+,\d+,\d+,null,null,\[null,null,"launch",\[\d+\]\],null,\[(\d{4}),(\d{1,2})\]\]/;
                const timeMatches = timePattern.exec(responseText);
                if (timeMatches) {
                    globalTimeInfo = `${timeMatches[1]}年${timeMatches[2]}月`;
                } else {
                    globalTimeInfo = '未知时间';
                }
            }
            if (this._url && this._url.includes('mapProxy/getPanoInfo')) {
                streetButton.style.display='none'
                var responseData
                const responseText = this.responseText;
                if (responseText) responseData=JSON.parse(responseText)
                if(responseData){

                    const latitude = responseData.data.lat
                    const longitude =responseData.data.lng
                    globalPanoId=responseData.data.pano
                    if (globalPanoId){
                        const year=parseInt(globalPanoId.substring(10,12))
                        const month=parseInt(globalPanoId.substring(12,14))
                        const day=parseInt(globalPanoId.substring(14,16))
                        globalTimeInfo = `20${year}年${month}月${day}日`;
                    }
                    else{
                        globalTimeInfo = '未知时间';
                    }
                    const heading=responseData.data.centerHeading
                    if (latitude && longitude) {
                        currentLink = `https://map.baidu.com/@12707848.16,2573405.14,21z,87t,-139.74h#panoid=${globalPanoId}&panotype=street&heading=${heading}&pitch=0&l=21&tn=B_NORMAL_MAP&sc=0&newmap=1&shareurl=1&pid=${globalPanoId}`;
                    }
                    if (api_key){
                        getAddressFromApi(latitude,longitude) .then(address => {
                            if (address) {
                                globalAreaInfo=address
                            }
                        })
                            .catch(error => {
                            console.error('获取地址时发生错误:', error);
                        });
                    }
                }
            }
        }, false);

        realSend.call(this, value);

        function getAddressFromApi(latitude, longitude) {
            return new Promise((resolve, reject) => {
                const apiUrl = `https://restapi.amap.com/v3/geocode/regeo?output=json&location=${longitude},${latitude}&key=${api_key}&radius=5`;
                GM_xmlhttpRequest({
                    method: "GET",
                    url: apiUrl,
                    onload: function(response) {
                        if (response.status === 200) {
                            const data = JSON.parse(response.responseText);
                            if (data.status === '1' && data.regeocode) {

                                const province=data.regeocode.addressComponent.province
                                const city=data.regeocode.addressComponent.city
                                const district=data.regeocode.addressComponent.district
                                const township=data.regeocode.addressComponent.township
                                const cityCode=data.regeocode.addressComponent.citycode
                                const addressInfo={province,city,district,township,cityCode}
                                var formatted_address= '中国'
                                for (const key in addressInfo) {
                                    if (addressInfo[key]) {
                                        if (addressInfo[key]!='') {
                                            formatted_address+=`, ${addressInfo[key]} `}
                                    }
                                }
                                resolve(formatted_address);
                            } else {
                                localStorage.removeItem('api_key')
                                Swal.fire('无效的API密钥','请刷新页面并重新输入正确的高德地图API密钥','error');
                                reject(new Error('Request failed: ' + data.info));
                            }
                        } else {
                            reject(new Error('Request failed with status: ' + response.status));

                        }
                    },
                    onerror: function(error) {
                        console.error('Error fetching address:', error);
                        reject(error);
                    }
                });
            });
        }
    }
    XMLHttpRequest.prototype.realOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
        this._url = url;
        this.realOpen(method, url, async, user, pass);
    };

    window.addEventListener('popstate', function(event) {
        const container = document.getElementById('coordinates-container');
        if (container) {
            container.remove();
        }
    });

})();