Geoguessr Map-Making Auto-Tag

tag by streetview date

目前為 2023-09-23 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();