Open Selected Address in Google Maps

選択したテキストや住所をGoogleマップで開く

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Open Selected Address in Google Maps
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  選択したテキストや住所をGoogleマップで開く
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // キーボードショートカット(例: Ctrl + M)を押したときに動作
    document.addEventListener('keydown', function(event) {
        if (event.ctrlKey && event.key === 'm') { // Ctrl + M で起動
            openInGoogleMaps();
        }
    });

    // 選択したテキストを取得してGoogleマップで開く関数
    function openInGoogleMaps() {
        let selectedText = window.getSelection().toString().trim();

        if (selectedText) {
            // Googleマップの検索URLを生成
            let mapsUrl = `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(selectedText)}`;
            // 新しいタブで開く
            window.open(mapsUrl, '_blank');
        } else {
            alert('住所またはテキストを選択してください!');
        }
    }

    // オプション: 右クリックメニューに項目を追加(ブラウザの制限で動作しない場合あり)
    document.addEventListener('contextmenu', function(event) {
        setTimeout(() => { // 右クリック後に少し待機して選択テキストを確認
            let selectedText = window.getSelection().toString().trim();
            if (selectedText) {
                if (confirm(`"${selectedText}" をGoogleマップで開きますか?`)) {
                    let mapsUrl = `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(selectedText)}`;
                    window.open(mapsUrl, '_blank');
                }
            }
        }, 100);
    });
})();