WME Simple Permalink (from WME KeepMyLayers)

Shortens WME permalinks by removing any layer and filter specifications

  1. // ==UserScript==
  2. // @name WME Simple Permalink (from WME KeepMyLayers)
  3. // @namespace https://greasyfork.org/users/11629-TheLastTaterTot
  4. // @version 2024.02.27.01
  5. // @description Shortens WME permalinks by removing any layer and filter specifications
  6. // @author TheLastTaterTot
  7. // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
  8. // @exclude https://www.waze.com/*user/editor/*
  9. // @grant none
  10. // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
  11. // @run-at document-end
  12. // ==/UserScript==
  13. /* jshint -W097 */
  14.  
  15. /* global W */
  16. /* global OL */
  17. /* ecmaVersion 2017 */
  18. /* global $ */
  19. /* global I18n */
  20. /* global _ */
  21. /* global WazeWrap */
  22. /* global require */
  23. /* eslint curly: ["warn", "multi-or-nest"] */
  24.  
  25. function loadSettings() {
  26. var loadedSettings = $.parseJSON(localStorage.getItem("WMESimplePermalink_Settings"));
  27. var defaultSettings = {
  28. CopyPermalinkShortcut: ''
  29. };
  30. settings = loadedSettings ? loadedSettings : defaultSettings;
  31. for (var prop in defaultSettings){
  32. if (!settings.hasOwnProperty(prop))
  33. settings[prop] = defaultSettings[prop];
  34. }
  35. }
  36.  
  37. function saveSettings() {
  38. if (localStorage) {
  39. var localsettings = {
  40. OrthogonalizeShortcut: settings.CopyPermalinkShortcut
  41. };
  42.  
  43. for (var name in W.accelerators.Actions) {
  44. var TempKeys = "";
  45. if (W.accelerators.Actions[name].group == 'wmesimplepermalink') {
  46. if (W.accelerators.Actions[name].shortcut) {
  47. if (W.accelerators.Actions[name].shortcut.altKey === true)
  48. TempKeys += 'A';
  49. if (W.accelerators.Actions[name].shortcut.shiftKey === true)
  50. TempKeys += 'S';
  51. if (W.accelerators.Actions[name].shortcut.ctrlKey === true)
  52. TempKeys += 'C';
  53. if (TempKeys !== "")
  54. TempKeys += '+';
  55. if (W.accelerators.Actions[name].shortcut.keyCode)
  56. TempKeys += W.accelerators.Actions[name].shortcut.keyCode;
  57. }
  58. else
  59. TempKeys = "-1";
  60. localsettings[name] = TempKeys;
  61. }
  62. }
  63.  
  64. localStorage.setItem("WMESimplePermalink_Settings", JSON.stringify(localsettings));
  65. }
  66. }
  67.  
  68.  
  69. var initSimplePermalink = function() {
  70. if (!document.getElementById('kmlPLPlaceholder')) { var kmlKeyPresses = Array(2);
  71.  
  72. var getKMLPermalink = function(currPl) {
  73. var kmlShortPL = currPl.substr(currPl.lastIndexOf('editor')+6).replace(/&[^&]*Filter=[^&]*|&s=(\d+)/ig,'').replace("/", "");
  74. return location.origin + location.pathname + kmlShortPL;
  75. };
  76.  
  77. var copyPL = function(PLtoCopy){
  78. copyToClipboard(PLtoCopy);
  79.  
  80. $('#kmlPLTooltip')[0].style.display = 'none';
  81. $('#kmlPLTooltipCopied')[0].style.display = 'block';
  82. setTimeout(function() {
  83. $('#kmlPLTooltipCopied')[0].style.display = 'none';
  84. }, 2000);
  85. };
  86.  
  87. var createStitchedPL = function(){
  88. var newPL = $('#aKMLPermalink')[0].href;
  89. var lon = newPL.match(/&lon=(-?\d{1,2}\.\d+)/)[1];
  90. var lat = newPL.match(/&lat=(-?\d{1,2}\.\d+)/)[1];
  91. var zoom = newPL.match(/zoom=\d+/)[1];
  92.  
  93. var centroid = W.map.getCenter().transform(W.map.projection, W.map.displayProjection);
  94. newPL = newPL.replace(lon, Math.round(centroid.lon * 100000) / 100000);
  95. newPL = newPL.replace(lat, Math.round(centroid.lat * 100000) / 100000);
  96. newPL = newPL.replace(zoom, W.map.zoom);
  97.  
  98. let selectedFeatures = WazeWrap.getSelectedFeatures();
  99. if(selectedFeatures.length > 0){
  100. if(selectedFeatures[0].model.type === "mapComment")
  101. newPL += `&mapComments=${selectedFeatures[0].model.attributes.id}`;
  102. else if(selectedFeatures[0].model.type === "venue")
  103. newPL += `&venues=${selectedFeatures[0].model.attributes.id}`;
  104. else if(selectedFeatures[0].model.type === "segment"){
  105. newPL += "&segments=";
  106. for(let i=0; i<selectedFeatures.length; i++){
  107. if((i+1) < selectedFeatures.length)
  108. newPL += `${selectedFeatures[i].model.attributes.id},`;
  109. else
  110. newPL += `${selectedFeatures[i].model.attributes.id}`;
  111. }
  112. }
  113. }
  114.  
  115. copyPL(newPL);
  116.  
  117. };
  118.  
  119. var copyToClipboard = function(str) {
  120. var $temp = $('<input>');
  121. $('body').append($temp);
  122. $temp.val(str).select();
  123. document.execCommand('copy');
  124. $temp.remove();
  125. };
  126.  
  127. var copyPLHotkeyEvent = function(e) {
  128. if (e.metaKey || e.ctrlKey) kmlKeyPresses[0] = true;
  129. if (e.which === 67) kmlKeyPresses[1] = true;
  130. if (kmlKeyPresses[0] && kmlKeyPresses[1]) {
  131. copyPL($('#aKMLPermalink')[0].href);
  132. }
  133. };
  134.  
  135. loadSettings();
  136. new WazeWrap.Interface.Shortcut('CopyPLShortcut', 'Copy Permalink', 'wmesimplepermalink', 'Simple Permalink', settings.CopyPermalinkShortcut, createStitchedPL, null).add();
  137.  
  138. //saves the set keyboard shortcut
  139. window.onbeforeunload = function() {
  140. saveSettings();
  141. };
  142.  
  143. var kmlStyle = document.createElement("style");
  144.  
  145. // Create CSS container element
  146. kmlStyle.type = "text/css";
  147. kmlStyle.id = "kml-css-container";
  148. kmlStyle.innerHTML = `
  149. .kml-pl-container { height: 25px; width: 48px; position: absolute; bottom: 0; right: 0; line-height: 24px; margin-right: 15px; margin-left: -24px; padding-left: 2px; visibility: visible; pointer-events: auto; }
  150. .kml-pl-container a:link, .kml-pl-container a:active, .kml-pl-container a:focus { text-decoration: none; background-image: none; outline: 0; -webkit-box-shadow: none; box-shadow: none; }
  151. .kml-pl-container>.fa-stack { height: 25px; width: 24px; margin-left: -2px; line-height: inherit; }
  152. .kml-pl-container>.fa-stack .fa-circle { font-size: 26px; line-height: 25px; bottom: 0px; }
  153. .kml-pl-container>.fa-stack .fa-link { font-size: 16px; line-height: 25px; bottom: 0px; }
  154. .kml-pl-tooltipbox { max-width: 99%; right: 0; white-space: normal; word-wrap: break-word; word-break: break-all; bottom: 25px; visibility: visible; margin-right: 15px; color: white; font-size: 8pt; background-color: transparent; pointer-events: none; position: absolute; }
  155. .kml-pl-tooltipbox>div { padding: 5px; border-radius: 5px; background-color: black; }
  156. .street-view-mode .kml-pl-container, .street-view-mode .kml-pl-tooltipbox { right: 50% !important; }`;
  157.  
  158. document.head.appendChild(kmlStyle);
  159.  
  160. var wazePermalinkEl = document.querySelector('.WazeControlPermalink>a.permalink'),
  161. wazeCopyPlNote = wazePermalinkEl.getAttribute('title'),
  162. kmlCurrentPl = getKMLPermalink(wazePermalinkEl.getAttribute('href')),
  163. wazeControlPermalinkEl = wazePermalinkEl.parentNode,
  164. kmlMapPLContainer = document.createElement('div'),
  165. kmlPLPlaceholder = document.createElement('div'),
  166. kmlPermalink;
  167.  
  168. wazePermalinkEl.id = 'wazePermalink';
  169.  
  170. kmlMapPLContainer.id = 'kmlPL';
  171. kmlMapPLContainer.style.position = 'absolute';
  172. kmlMapPLContainer.style.width = '100%';
  173. kmlMapPLContainer.style.bottom = '0px';
  174. kmlMapPLContainer.style.right = '0px';
  175. kmlMapPLContainer.style.visibility = 'hidden';
  176. kmlMapPLContainer.style.pointerEvents = 'none';
  177. kmlMapPLContainer.style.zIndex = 4;
  178. kmlMapPLContainer.innerHTML = '<div class="kml-pl-tooltipbox"><div id="kmlPLTooltip" style="display: none;"></div><div id="kmlPLTooltipCopied" style="display: none;"></div></div>' +
  179. '<div class="kml-pl-container" style="overflow: hidden; width: 25px;"><div id="kmlPermalink" class="fa-stack"></div></div>';
  180. document.getElementById('map').appendChild(kmlMapPLContainer);
  181.  
  182. kmlPLPlaceholder.id = 'kmlPLPlaceholder';
  183. kmlPLPlaceholder.style.float = 'right';
  184. kmlPLPlaceholder.style.position = 'relative';
  185. kmlPLPlaceholder.style.bottom = '0px';
  186. kmlPLPlaceholder.style.right = '0px';
  187. kmlPLPlaceholder.style.height = '25px';
  188. kmlPLPlaceholder.style.width = '25px';
  189. kmlPLPlaceholder.style.marginRight = '-4px';
  190. kmlPLPlaceholder.style.marginLeft = '-24px';
  191. kmlPLPlaceholder.style.backgroundColor = '#E9E9E9';
  192. wazeControlPermalinkEl.appendChild(kmlPLPlaceholder);
  193.  
  194. //-------------------------
  195. kmlPermalink = document.getElementById('kmlPermalink');
  196. kmlPermalink.innerHTML = '<a id="aKMLPermalink" href="' + kmlCurrentPl + '"><span class="fa fa-circle fa-stack-1x" style="color: #59899E;"></span><span class="fa fa-link fa-stack-1x fa-inverse"></span></a>';
  197. //-------------------------
  198. // PL address popup
  199. document.getElementById('kmlPLTooltip').innerHTML = '<span id="tooltipKMLPermalink">' + kmlCurrentPl + '</span><p></p><b>' + wazeCopyPlNote + '</b>';
  200. // "Copied" popup
  201. document.getElementById('kmlPLTooltipCopied').innerHTML = '<b>' + I18n.translations[I18n.locale].footer.link_copied + '</b>';
  202.  
  203. //------------------------------------------------------------------
  204. kmlPermalink.addEventListener('mouseenter', function(e) {
  205. var changedThisPl = getKMLPermalink(wazePermalinkEl.getAttribute('href'));
  206.  
  207. document.getElementById('tooltipKMLPermalink').innerHTML = changedThisPl;
  208. document.getElementById('aKMLPermalink').setAttribute('href', changedThisPl);
  209. document.getElementById('kmlPLTooltip').style.display = 'block';
  210. window.addEventListener('keydown', copyPLHotkeyEvent, false);
  211. }, false);
  212.  
  213. kmlPermalink.addEventListener('mouseleave', function() {
  214. kmlKeyPresses = Array(2);
  215. document.getElementById('kmlPLTooltip').style.display = 'none';
  216. document.getElementById('kmlPLTooltipCopied').style.display = 'none';
  217. window.removeEventListener('keydown', copyPLHotkeyEvent);
  218. }, false);
  219.  
  220. try {
  221. // Hide WME permalink, but allow TB to overrule with display: none;
  222. wazePermalinkEl.style.visibility = 'hidden';
  223. document.getElementsByClassName('livemap-link')[0].style.paddingRight = '50px';
  224. } catch (err) {}
  225. }
  226. };
  227.  
  228. function bootstrap(tries = 1) {
  229. if (W && W.map && W.model && $ && $('.WazeControlPermalink').length > 0 && WazeWrap.Ready)
  230. initSimplePermalink();
  231. else if (tries < 1000)
  232. setTimeout(function () {bootstrap(tries++);}, 200);
  233. }
  234.  
  235. bootstrap();