Geoguessr Map-Making Auto-Tag

for map-making tag

目前为 2023-09-23 提交的版本。查看 最新版本

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

(function() {
    'use strict';

    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].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);
})();