Map-Making Switcher

use shortcut to switch panoramas

As of 2024-08-26. See the latest version.

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         Map-Making Switcher
// @namespace    https://greasyfork.org/users/1179204
// @description  use shortcut to switch panoramas
// @version      1.1.1
// @license      BSD
// @author       KaKa
// @match        *://map-making.app/maps/*
// @icon         https://www.svgrepo.com/show/521871/switch.svg
// ==/UserScript==
(function() {
    let editor,selections,currentIndex,map,mapListener,isApplied=false,customZoom=0.43,isCustom=false

    function getEditor() {
        editor = unsafeWindow.editor
        const activeSelections = editor.selections;
        const locations=unsafeWindow.locations
        selections = activeSelections.length > 0 ? activeSelections.flatMap(selection => selection.locations) : locations;
    }
    function switchLoc(locs) {

        if (!currentIndex) {
            currentIndex =1;
        } else {
            currentIndex +=1
            if (currentIndex>locs.length){
                currentIndex=1
            }
        }
        editor.openLocation(locs[currentIndex-1]);
        focusOnLoc(locs[currentIndex-1])
    }

    function rewindLoc(locs) {

        if (!currentIndex) {
            currentIndex =1;
        }
        else {
            currentIndex -=1
            if (currentIndex<1) currentIndex=selections.length
        }
        editor.openLocation(locs[currentIndex-1]);
        focusOnLoc(locs[currentIndex-1])
    }

    function focusOnLoc(loc){
        map=unsafeWindow.map
        map.setCenter(loc.location)
        map.setZoom(17)
    }

    function deleteLoc(loc){
        editor.closeAndDeleteLocation(loc)
    }

    function rewindSelections(loc){
        currentIndex=1
        editor.openLocation(selections[0])
    }

    function setZoom(z){
        if(z<0)z=0
        if(z>4)z=4
        const svControl=unsafeWindow.streetView
        svControl.setZoom(z)

    }

    function resetDefaultZoom(){
        if(mapListener) return
        map=unsafeWindow.map
        mapListener=function(){
            if(isCustom){
                let intervalId=setInterval(function(){
                    editor = unsafeWindow.editor
                    if(editor.currentLocation){
                        setZoom(customZoom)
                        clearInterval(intervalId)}
                },50);

            }
        }
        map.addListener('click', mapListener);
    }

    function getTag(index){

        const tags=unsafeWindow.editor.tags
        const result = Object.keys(tags).find(tag => tags[tag].order === index - 1)
        if(result){
            return result.trim()}
    }


    let onKeyDown = async (e) => {
        if(!isApplied) return
        if (e.key === 'q' || e.key === 'Q') {
            e.stopImmediatePropagation();
            getEditor()
            switchLoc(selections)
        };
        if (e.key === 'e' || e.key === 'E') {
            e.stopImmediatePropagation();
            getEditor()
            rewindLoc(selections)
        };
        if (e.key === 'c' || e.key === 'C') {
            e.stopImmediatePropagation();
            getEditor()
            deleteLoc(selections[currentIndex-1])
        };
        if (e.key === 'r' || e.key === 'R') {
            e.stopImmediatePropagation();
            getEditor()
            rewindSelections()
        };
        if (e.key === 'g' || e.key === 'G') {
            e.stopImmediatePropagation();
            resetDefaultZoom()
            if(!isCustom) {
                var input = prompt('please enter a zoom value(0-4):');

                var parsedValue = parseFloat(input);

                if (isNaN(parsedValue)) {
                    alert('The input is not a valid zoom! Please try again.');
                    isCustom=false
                } else {
                    customZoom=parsedValue
                    isCustom=true
                    resetDefaultZoom()
                    alert('Custom zoom has been applied!');
                }
            }
            else {
                isCustom=false
                alert('Zoom customizing is cancelled!')}
        }
    }
    document.addEventListener("keydown", onKeyDown);

    var shortCutButton = document.createElement('button');
    shortCutButton.textContent = 'ShortCut Off';
    shortCutButton.style.position = 'fixed';
    shortCutButton.style.top = '12px';
    shortCutButton.style.right = '500px';
    shortCutButton.style.zIndex = '9999';
    shortCutButton.style.borderRadius = "18px";
    shortCutButton.style.padding = "10px 20px";
    shortCutButton.style.border = "none";
    shortCutButton.style.backgroundColor = "#4CAF50";
    shortCutButton.style.color = "white";
    shortCutButton.style.cursor = "pointer";
    shortCutButton.addEventListener('click', function(){
        if(isApplied){
            isApplied=false
            shortCutButton.style.border='none'
            shortCutButton.textContent = 'ShortCut Off';
        }
        else {isApplied=true
              shortCutButton.textContent = 'ShortCut On';
              shortCutButton.style.border='2px solid #fff'}

    });
    document.body.appendChild(shortCutButton)
})();