Geoguessr Map-Making Auto-Tag

tag by streetview date

当前为 2023-09-23 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Geoguessr Map-Making Auto-Tag
// @namespace    http://tampermonkey.net/
// @version      1.15
// @description  tag by streetview date
// @author       KaKa
// @match        https://map-making.app/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_setClipboard
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    async function runScript() {
        var api_key = GM_getValue("api_key");
        if (!api_key) {
            api_key = prompt("Please enter your API key");
            GM_setValue("api_key", api_key);
        }
        var text = await navigator.clipboard.readText();
        var data = JSON.parse(text);
        var newData = [];

        function get_Meta(id) {
            var url = "https://maps.googleapis.com/maps/api/streetview/metadata?pano=" + id + "&key=" + api_key;
            return fetch(url)
                .then(function(response) {
                    return response.json();
                })
                .then(function(data) {
                    console.log(data);
                    if (data.status == "OK") {
                        var date = data.date;
                        var cr = data.copyright;
                        var year = 'nodate';
                        var match = date.match(/\d{4}/);
                        if (match) {
                            year = match[0];
                        }

                        var panoType = 'unofficial';
                        if (cr.includes('Google')) {
                            panoType = 'official';
                        }

                        return [year, panoType, cr];
                    } else {
                        console.log("Error: " + data.status);
                    }
                })
                .catch(function(error) {
                    console.log(error);
                });
        }

        function search_Meta(lat, lng) {
            var url = "https://maps.googleapis.com/maps/api/streetview/metadata?location=" + lat + "," + lng + "&key=" + api_key;
            return fetch(url)
                .then(function(response) {
                    return response.json();
                })
                .then(function(data) {
                    console.log(data);
                    if (data.status == "OK") {
                        var date = data.date;
                        var cr = data.copyright;
                        var year = 'nodate';
                        var match = date.match(/\d{4}/);
                        if (match) {
                            year = match[0];
                        }

                        var panoType = 'unofficial';
                        if (cr.includes('Google')) {
                            panoType = 'official';
                        }

                        return [year, panoType, cr];
                    } else {
                        console.log("Error: " + data.status);
                    }
                })
                .catch(function(error) {
                    console.log(error);
                });
        }

        var promises = [];

        for (let i in data.customCoordinates) {
            if (!data.customCoordinates[i].extra) {
                data.customCoordinates[i].extra = {};
            }
            if (!data.customCoordinates[i].extra.tags) {
                data.customCoordinates[i].extra.tags = [];
            }
            if (data.customCoordinates[i].panoId) {
                promises.push(get_Meta(data.customCoordinates[i].panoId).then(function(meta) {
                    if (meta && meta.length >= 2) {
                        var year_tag = meta[0];
                        var type_tag = meta[1];
                        data.customCoordinates[i].extra.tags.push(year_tag);
                        data.customCoordinates[i].extra.tags.push(type_tag);
                        newData.push(data.customCoordinates[i]);
                    }
                }));
            } else {
                promises.push(search_Meta(data.customCoordinates[i].lat,data.customCoordinates[i].lng).then(function(meta) {
                    if (meta && meta.length >= 2) {
                        var year_tag = meta[0];
                        var type_tag = meta[1];
                        data.customCoordinates[i].extra.tags.push(year_tag);
                        data.customCoordinates[i].extra.tags.push(type_tag);
                        newData.push(data.customCoordinates[i]);
                    }
                }));
            }
        }

        Promise.all(promises).then(function() {
            GM_setClipboard(JSON.stringify(newData));
            alert("New JSON data has been copied to the clipboard!");
        });
    }

    var button = document.createElement('button');
    button.textContent = 'Auto-Tag';
    button.addEventListener('click', runScript);
    document.body.appendChild(button);
})();