Open Selected Address in Google Maps

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
    });
})();