No Google Maps Crap

Removes google maps crap replacing it with OSM

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         No Google Maps Crap
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Removes google maps crap replacing it with OSM
// @author       Anonim
// @match        https://*.google.com/maps/*
// @match        https://maps.google.com/*
// @match        https://maps.app.goo.gl/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Display "redirecting..." message
    document.documentElement.innerHTML = '<html><body><h1>Redirecting...</h1></body></html>';

    // Redirect to OpenStreetMap
    function redirectToOSM(url) {
        setTimeout(() => {
            window.location.replace(url);
        }, 1000); // Delay to reduce bugs
    }

    // Parser
    function getQueryParams(url) {
        let params = {};
        let parser = new URL(url);
        parser.searchParams.forEach((value, key) => {
            params[key] = value;
        });
        return params;
    }

    // Check for Crap Maps with coordinates in the query string
    if (window.location.href.includes("maps.google.com") || window.location.href.includes("maps.app.goo.gl")) {
        const params = getQueryParams(window.location.href);
        if (params.q) {
            const [lat, lon] = params.q.split(',');
            const osmUrl = `https://www.openstreetmap.org/#map=18/${lat}/${lon}`;
            redirectToOSM(osmUrl);
        }
    }

    // Check for crappy Maps place link
    if (window.location.href.includes("/maps/place/")) {
        const match = window.location.href.match(/@(-?\d+\.\d+),(-?\d+\.\d+)/);
        if (match) {
            const lat = match[1];
            const lon = match[2];
            const osmUrl = `https://www.openstreetmap.org/#map=18/${lat}/${lon}`;
            redirectToOSM(osmUrl);
        }
    }

    // Check for shitty Maps short links (maps.app.goo.gl)
    if (window.location.href.includes("maps.app.goo.gl")) {
        // As short links redirect to longer URLs, set an interval to handle redirection after the URL changes
        const interval = setInterval(() => {
            const match = window.location.href.match(/@(-?\d+\.\d+),(-?\d+\.\d+)/);
            if (match) {
                clearInterval(interval);
                const lat = match[1];
                const lon = match[2];
                const osmUrl = `https://www.openstreetmap.org/#map=18/${lat}/${lon}`;
                redirectToOSM(osmUrl);
            }
        }, 500);
    }
})();