WME Simple Permalink (from WME KeepMyLayers)

Shortens WME permalinks by removing any layer and filter specifications

当前为 2018-05-29 提交的版本,查看 最新版本

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