WME Simple Permalink (from WME KeepMyLayers)

Shortens WME permalinks by removing any layer and filter specifications

当前为 2016-04-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WME Simple Permalink (from WME KeepMyLayers)
  3. // @namespace https://greasyfork.org/users/11629-TheLastTaterTot
  4. // @version 0.1.3
  5. // @description Shortens WME permalinks by removing any layer and filter specifications
  6. // @author TheLastTaterTot
  7. // @include https://editor-beta.waze.com/*editor/*
  8. // @include https://www.waze.com/*editor/*
  9. // @exclude https://www.waze.com/*user/editor/*
  10. // @grant none
  11. // @run-at document-end
  12. // ==/UserScript==
  13. /* jshint -W097 */
  14.  
  15. var initRegPermalink = function() {
  16. if (!document.getElementById('kmlPLPlaceholder')) {
  17. var kmlKeyPresses = Array(2);
  18.  
  19. var getKMLPermalink = function(currPl) {
  20. var kmlShortPL = currPl.substr(currPl.lastIndexOf('editor')).replace(/&[^&]+Filter=[^&]+\b|&layers=(\d+)/ig,'');
  21. return location.origin + '/' + kmlShortPL;
  22. };
  23.  
  24. var copyToClipboard = function(str) {
  25. var $temp = $('<input>');
  26. $('body').append($temp);
  27. $temp.val(str).select();
  28. document.execCommand('copy');
  29. $temp.remove();
  30. };
  31.  
  32. var copyPlToClipboard = function(e) {
  33. if (e.metaKey || e.ctrlKey) kmlKeyPresses[0] = true;
  34. if (e.which === 67) kmlKeyPresses[1] = true;
  35. if (kmlKeyPresses[0] && kmlKeyPresses[1]) {
  36. copyToClipboard(document.getElementById('aKMLPermalink').getAttribute('href'));
  37.  
  38. document.getElementById('kmlPLTooltip').style.display = 'none';
  39. document.getElementById('kmlPLTooltipCopied').style.display = 'block';
  40. setTimeout(function() {
  41. document.getElementById('kmlPLTooltipCopied').style.display = 'none';
  42. }, 2000);
  43. }
  44. };
  45.  
  46. var kmlStyle = document.createElement("style");
  47.  
  48. // Create CSS container element
  49. kmlStyle.type = "text/css";
  50. kmlStyle.id = "kml-css-container";
  51. kmlStyle.innerHTML = `
  52. .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; }
  53. .kml-pl-container>.fa-stack { height: 25px; width: 23px; margin-left: -2px; line-height: inherit; }
  54. .kml-pl-container>.fa-stack .fa-circle { font-size: 26px; line-height: 25px; bottom: 0px; }
  55. .kml-pl-container>.fa-stack .fa-link { font-size: 16px; line-height: 25px; bottom: 0px; }
  56. .kml-pl-tooltipbox { max-width: 99%; right: 0; white-space: normal; word-wrap: break-word; bottom: 25px; visibility: visible; margin-right: 15px; color: white; font-size: 8pt; background-color: transparent; pointer-events: none; position: absolute; }
  57. .kml-pl-tooltipbox>div { padding: 5px; border-radius: 5px; background-color: black; }
  58. .street-view-mode .kml-pl-container, .street-view-mode .kml-pl-tooltipbox { right: 50% !important; }`;
  59.  
  60. document.head.appendChild(kmlStyle);
  61.  
  62. document.querySelector('.WazeControlPermalink>a.icon-link, .WazeControlPermalink>a.fa-link').id = 'wazePermalink';
  63.  
  64. var wazePermalinkEl = document.getElementById('wazePermalink'),
  65. wazeCopyPlNote = wazePermalinkEl.getAttribute('data-original-title'),
  66. wazeCurrentPl = wazePermalinkEl.getAttribute('href').replace(/&[^&]+Filter=[^&]+\b|&layers=(\d+)/ig,''),
  67. wazeControlPermalinkEl = document.getElementsByClassName('WazeControlPermalink')[0],
  68. kmlMapPLContainer = document.createElement('div'),
  69. kmlPLPlaceholder = document.createElement('div'),
  70. kmlCurrentPl, kmlPLColor;
  71.  
  72. kmlMapPLContainer.id = 'kmlPL';
  73. kmlMapPLContainer.style.position = 'absolute';
  74. kmlMapPLContainer.style.width = '100%';
  75. kmlMapPLContainer.style.bottom = '0px';
  76. kmlMapPLContainer.style.right = '0px';
  77. kmlMapPLContainer.style.visibility = 'hidden';
  78. kmlMapPLContainer.style.pointerEvents = 'none';
  79. kmlMapPLContainer.style.zIndex = 4;
  80. kmlMapPLContainer.innerHTML = `
  81. <div id="kmlTooltipContainer" class="kml-pl-tooltipbox">
  82. <div id="kmlPLTooltip" style="display: none;"></div>
  83. <div id="kmlPLTooltipCopied" style="display: none;"></div>
  84. </div>
  85. <div id="kmlPLContainer" class="kml-pl-container" style="overflow: hidden; width: 23px;">
  86. <div id="kmlPermalink" class="fa-stack"></div>
  87. </div>`;
  88. document.getElementById('map').appendChild(kmlMapPLContainer);
  89.  
  90. kmlPLPlaceholder.id = 'kmlPLPlaceholder';
  91. kmlPLPlaceholder.style.float = 'right';
  92. kmlPLPlaceholder.style.position = 'relative';
  93. kmlPLPlaceholder.style.bottom = '0px';
  94. kmlPLPlaceholder.style.right = '0px';
  95. kmlPLPlaceholder.style.height = '25px';
  96. kmlPLPlaceholder.style.width = '25px';
  97. kmlPLPlaceholder.style.marginRight = '-4px';
  98. kmlPLPlaceholder.style.marginLeft = '-24px';
  99. kmlPLPlaceholder.style.backgroundColor = '#E9E9E9';
  100. wazeControlPermalinkEl.appendChild(kmlPLPlaceholder);
  101.  
  102. kmlPLColor = '#59899E';
  103. //---------------
  104. kmlCurrentPl = getKMLPermalink(wazeCurrentPl);
  105. document.getElementById('kmlPermalink').innerHTML = '<a id="aKMLPermalink" href="' + kmlCurrentPl + '"><span class="fa fa-circle fa-stack-1x" style="color: ' + kmlPLColor + ';"></span><span class="fa fa-link fa-stack-1x fa-inverse"></span></a>';
  106.  
  107. //------------------------------------------------------------------
  108. // PL address popup
  109. document.getElementById('kmlPLTooltip').innerHTML = '<span id="tooltipKMLPermalink">' + kmlCurrentPl + '</span><p></p><b>' + wazeCopyPlNote + '</b>';
  110. // "Copied" popup
  111. document.getElementById('kmlPLTooltipCopied').innerHTML = '<b>' + I18n.translations[I18n.locale].footer.link_copied + '</b>';
  112.  
  113. //------------------------------------------------------------------
  114. document.getElementById('kmlPermalink').addEventListener('mouseenter', function(e) {
  115. var thisPl = document.getElementById('wazePermalink').getAttribute('href').replace(/&[^&]+Filter=[^&]+\b|&layers=(\d+)/ig,''),
  116. changedThisPl = getKMLPermalink(thisPl);
  117.  
  118. document.getElementById('tooltipKMLPermalink').innerHTML = changedThisPl;
  119. document.getElementById('aKMLPermalink').setAttribute('href', changedThisPl);
  120. document.getElementById('kmlPLTooltip').style.display = 'block';
  121. window.addEventListener('keydown', copyPlToClipboard, false);
  122. }, false);
  123.  
  124. document.getElementById('kmlPermalink').addEventListener('mouseleave', function() {
  125. kmlKeyPresses = Array(2);
  126. document.getElementById('kmlPLTooltip').style.display = 'none';
  127. document.getElementById('kmlPLTooltipCopied').style.display = 'none';
  128. window.removeEventListener('keydown', copyPlToClipboard);
  129. }, false);
  130.  
  131. try {
  132. // Hide WME permalink, but allow TB to overrule with display: none;
  133. document.getElementById('wazePermalink').style.visibility = 'hidden';
  134. } catch (err) {}
  135. }
  136. };
  137.  
  138.  
  139. function waitForWazeElement() {
  140. var waitCount = 0,
  141. maxWait = 50; //30++ seconds
  142.  
  143. var tryInit_kmlPL = function() {
  144. try {
  145. if (waitCount < maxWait &&
  146. document.getElementsByClassName('WazeControlPermalink')) {
  147. initRegPermalink();
  148. waitCount++; //for catching returns due to an undetected error
  149. } else if (waitCount++ < maxWait) {
  150. setTimeout(tryInit_kmlPL, 25 * waitCount);
  151. } else {
  152. console.error('WMESPL:',
  153. 'Could not find necessary WME permalink element');
  154. }
  155. } catch (err) {
  156. try { //try again once more in case it failed due to another script or WME hiccup... >:]
  157. setTimeout(tryInit_kmlPL, 500);
  158. } catch (err) {
  159. console.error(
  160. 'WMESPL:',
  161. 'WME KmL Short Permalinks failed to load due to some kind of technical script error. :(');
  162. console.error(err);
  163. }
  164. }
  165. };
  166. tryInit_kmlPL();
  167. }
  168.  
  169.  
  170. setTimeout(waitForWazeElement,2000);