Kanka Map Path Helper

Helps turn polygon markers on Kanka maps into lines to represent paths.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Kanka Map Path Helper
// @namespace    http://tampermonkey.net/
// @license      MIT
// @version      4
// @description  Helps turn polygon markers on Kanka maps into lines to represent paths.
// @author       Salvatos
// @match        https://app.kanka.io/*/maps/*/map_markers*
// @match        https://app.kanka.io/*/maps/*/explore*
// @icon         https://www.google.com/s2/favicons?domain=kanka.io
// @run-at       document-end
// ==/UserScript==

// Locate form field
const coordbox = document.querySelector('#marker-poly textarea[name="custom_shape"]');

// Create button
var pathMakerInfo = `<label style="margin-top: 10px;">Path Helper<sup> beta</sup></label><p style="color: var(--text-help); margin-bottom: 5px;">Use the button below to turn your coordinates into a continuous line, for example to represent roads or itineraries. Duplicate points will be omitted, which may cause errors if your path visits the same (exact) point multiple times. To prolong an existing path, simply click the new coordinates, activate the button and those points will be added from the previous end of the path. Remember to set your stroke options to make it visible.</p>`;
var pathMakerBtn = `
        <button type="button" id="path-helper" class="note-btn btn btn-default" title="Make into path">
        Make into path
        </button>`;
coordbox.insertAdjacentHTML("afterend", pathMakerBtn);
coordbox.insertAdjacentHTML("afterend", pathMakerInfo);

// Add click event to button
document.getElementById('path-helper').addEventListener('click', function () {
    // Extract all coordinates from input into array
    var coords1 = coordbox.value.trim().split(" ");

    // Remove duplicates (for successive button clicks, prolonging existing paths, etc.)
    var coords2 = uniq(coords1);
    function uniq(a) {
        return Array.from(new Set(a));
    }

    // Start a fresh array with the unique coords in the initial order, then reverse our copy
    let coords = [].concat(coords2);
    coords2.reverse();

    // Iterate through coords backwards and append them
    coords2.forEach(function (item) {
        if (item != coords2[0]) { // Omit extremity
            coords.push(item);
        }
    });

    // Push new coords to textarea
    coordbox.value = coords.join (" ");
});