Last active
January 17, 2026 03:18
-
-
Save wilmtang/4832e10b79a893d7487780b1242b08e1 to your computer and use it in GitHub Desktop.
Google Maps – Street View Toggle Shortcut (Ctrl+S)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Google Maps – Reliable Street View Toggle (Ctrl+S, Updated Label) | |
| // @namespace http://tampermonkey.net/ | |
| // @version 1.2 | |
| // @description Toggle the Street View layer on Google Maps by simulating a click (Ctrl+S) | |
| // @match https://www.google.com/maps/* | |
| // @grant none | |
| // ==/UserScript== | |
| // This tampermonkey registers shortcut (ctrl + s) to quickly turn on/off google streetview. If you wanna it work on windows system you need to change the bind key | |
| // I find it pretty helpful when I do bike route research in cities, where I need to turn streetview on/off quite often to see if a street has separate bike lanes | |
| (function() { | |
| 'use strict'; | |
| function findStreetViewButton() { | |
| return document.querySelector('button[aria-label="Browse Street View images"]'); | |
| } | |
| function simulateClick(btn) { | |
| btn.dispatchEvent(new MouseEvent("mousedown", { bubbles: true })); | |
| btn.dispatchEvent(new MouseEvent("mouseup", { bubbles: true })); | |
| btn.dispatchEvent(new MouseEvent("click", { bubbles: true })); | |
| } | |
| let svButton = null; | |
| // Periodic scan (self-healing) | |
| setInterval(() => { | |
| if (!svButton) { | |
| const found = findStreetViewButton(); | |
| if (found) { | |
| svButton = found; | |
| console.log("Street View button detected (interval)."); | |
| } | |
| } | |
| }, 500); | |
| // Mutation observer for dynamic UI | |
| const observer = new MutationObserver(() => { | |
| if (!svButton) { | |
| const found = findStreetViewButton(); | |
| if (found) { | |
| svButton = found; | |
| console.log("Street View button detected (observer)."); | |
| } | |
| } | |
| }); | |
| observer.observe(document.body, { childList: true, subtree: true }); | |
| // Ctrl+S handler | |
| document.addEventListener("keydown", (e) => { | |
| if (e.ctrlKey && e.key.toLowerCase() === "s") { | |
| e.preventDefault(); // block "Save Page" | |
| e.stopPropagation(); | |
| // Recheck DOM in case button appears late | |
| if (!svButton) svButton = findStreetViewButton(); | |
| if (svButton) { | |
| simulateClick(svButton); | |
| console.log("Street View toggled via Ctrl+S."); | |
| } else { | |
| console.log("Street View button not ready."); | |
| } | |
| } | |
| }, true); // capture phase | |
| console.log("Userscript loaded. Press Ctrl+S to toggle the Street View layer."); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment