Kanka Map Path Helper

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

目前为 2023-06-28 提交的版本。查看 最新版本

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