Kanka Map Path Helper

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

  1. // ==UserScript==
  2. // @name Kanka Map Path Helper
  3. // @namespace http://tampermonkey.net/
  4. // @license MIT
  5. // @version 4
  6. // @description Helps turn polygon markers on Kanka maps into lines to represent paths.
  7. // @author Salvatos
  8. // @match https://app.kanka.io/*/maps/*/map_markers*
  9. // @match https://app.kanka.io/*/maps/*/explore*
  10. // @icon https://www.google.com/s2/favicons?domain=kanka.io
  11. // @run-at document-end
  12. // ==/UserScript==
  13.  
  14. // Locate form field
  15. const coordbox = document.querySelector('#marker-poly textarea[name="custom_shape"]');
  16.  
  17. // Create button
  18. 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>`;
  19. var pathMakerBtn = `
  20. <button type="button" id="path-helper" class="note-btn btn btn-default" title="Make into path">
  21. Make into path
  22. </button>`;
  23. coordbox.insertAdjacentHTML("afterend", pathMakerBtn);
  24. coordbox.insertAdjacentHTML("afterend", pathMakerInfo);
  25.  
  26. // Add click event to button
  27. document.getElementById('path-helper').addEventListener('click', function () {
  28. // Extract all coordinates from input into array
  29. var coords1 = coordbox.value.trim().split(" ");
  30.  
  31. // Remove duplicates (for successive button clicks, prolonging existing paths, etc.)
  32. var coords2 = uniq(coords1);
  33. function uniq(a) {
  34. return Array.from(new Set(a));
  35. }
  36.  
  37. // Start a fresh array with the unique coords in the initial order, then reverse our copy
  38. let coords = [].concat(coords2);
  39. coords2.reverse();
  40.  
  41. // Iterate through coords backwards and append them
  42. coords2.forEach(function (item) {
  43. if (item != coords2[0]) { // Omit extremity
  44. coords.push(item);
  45. }
  46. });
  47.  
  48. // Push new coords to textarea
  49. coordbox.value = coords.join (" ");
  50. });