WazeMY

WME script for WazeMY editing moderation

当前为 2020-06-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WazeMY
  3. // @namespace http://junyianl.net/
  4. // @version 2020.06.03.01
  5. // @description WME script for WazeMY editing moderation
  6. // @author junyianl
  7. // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
  8. // @require https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. /* global W */
  13. /* global WazeWrap */
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. var tooltipDiv = null;
  19. var tooltipOffset = {
  20. x: 15,
  21. y: 15
  22. };
  23.  
  24. function initTooltip() {
  25. tooltipDiv = document.createElement("div");
  26. tooltipDiv.id = "wazemyTooltip";
  27. tooltipDiv.style.height = "auto";
  28. tooltipDiv.style.width = "auto";
  29. tooltipDiv.style.background = "rgba(0, 0, 0, 0.5)";
  30. tooltipDiv.style.color = "white";
  31. tooltipDiv.style.borderRadius = "5px";
  32. tooltipDiv.style.padding = "5px";
  33. tooltipDiv.style.position = "absolute";
  34. tooltipDiv.style.top = "0px";
  35. tooltipDiv.style.left = "0px";
  36. tooltipDiv.style.visibility = "hidden";
  37. tooltipDiv.style.zIndex = 10000;
  38. document.body.appendChild(tooltipDiv);
  39. }
  40.  
  41. function WazeMY_init() {
  42. // add WazeMY tab in sidebar
  43. let username = WazeWrap.User.Username();
  44. let rank = WazeWrap.User.Rank();
  45. let tabhtml = '<div>Version: ' + GM_info.script.version + '</div><br>';
  46. tabhtml += '<div>' + username + '(' + rank + ')</div>';
  47. new WazeWrap.Interface.Tab('WazeMY', tabhtml, null)
  48.  
  49. // initialize tooltip for mouse over segments and places
  50. initTooltip();
  51. WazeWrap.Events.register("mousemove", null, wazemy_showMapTooltip);
  52.  
  53. // initialize keyboard shortcut for copying lat/lon
  54. new WazeWrap.Interface.Shortcut('WazeMY_latloncopy', 'Copies lat/lon of mouse position', 'wazemy', 'WazeMY', 'CA+c', wazemy_copyLatLon, null).add();
  55. // WazeWrap.Events.register("moveend", null, wazemy_moveend);
  56. // WazeWrap.Events.register("zoomend", null, wazemy_zoomend);
  57. // WazeWrap.Events.register("changelayer", null, wazemy_changelayer);
  58. // WazeWrap.Events.register("selectionchanged", null, wazemy_selectionchanged);
  59. }
  60. function wazemy_moveend(arg1){
  61. console.log("WazeMY: moveend");
  62. }
  63. function wazemy_zoomend(arg1){
  64. console.log("WazeMY: zoomend");
  65. }
  66. function wazemy_showMapTooltip(e){
  67. var landmark = null;
  68. var segment = null;
  69. var result = "";
  70. var showTooltip = false;
  71. // landmark = W.map.landmarkLayer.getFeatureBy("renderIntent", "highlight");
  72. landmark = W.map.venueLayer.getFeatureBy("renderIntent", "highlight");
  73. segment = W.map.segmentLayer.getFeatureBy("renderIntent", "highlight");
  74. if (landmark != null) {
  75. // console.log(landmark.model);
  76. result = "<b>"+landmark.model.attributes.name+"</b><br>";
  77. var address = landmark.model.getAddress();
  78. try {
  79. result += (address.attributes.houseNumber?(address.attributes.houseNumber+", "):"") + (address.attributes.street.name?address.attributes.street.name:"No street") + "<br>";
  80. result += address.attributes.city.attributes.name + ", " + address.attributes.state.name + "<br>";
  81. }
  82. catch {
  83. result += "No address<br>";
  84. }
  85. result += "<b>Lock:</b> " + (landmark.model.getLockRank() + 1);
  86. showTooltip = true;
  87. }
  88. else if (segment != null) {
  89. // console.log(segment.model);
  90. var segmentId = segment.model.attributes.id;
  91. var primaryStreetId = WazeWrap.Model.getPrimaryStreetID(segmentId);
  92. var address = segment.model.getAddress();
  93. result = "<b>" + (address.attributes.street.name?address.attributes.street.name:"No street") + "</b><br>";
  94. result += address.attributes.city.attributes.name + ", " + address.attributes.state.name + "<br>";
  95. result += "<b>ID:</b> " + segmentId + "<br>";
  96. result += "<b>Lock:</b> " + (segment.model.getLockRank() + 1);
  97. showTooltip = true;
  98. }
  99. if (showTooltip == true) { // adjust tooltip position and make it visible
  100. var tw = tooltipDiv.clientWidth;
  101. var th = tooltipDiv.clientHeight;
  102. var tooltipX = e.clientX + window.scrollX + tooltipOffset.x;
  103. var tooltipY = e.clientY + window.scrollY + tooltipOffset.y;
  104. if ((tooltipX + tw) > W.map.$map.innerWidth()) { // adjust tooltip position to keep within map window
  105. tooltipX -= tw + 20; // 20 = scroll bar size
  106. if (tooltipX < 0) tooltipX = 0;
  107. }
  108. if ((tooltipY + th) > W.map.$map.innerHeight()) {
  109. tooltipY -= th + 20;
  110. if (tooltipY < 0) tooltipY = 0;
  111. }
  112. tooltipDiv.style.top = tooltipY + "px";
  113. tooltipDiv.style.left = tooltipX + "px";
  114. tooltipDiv.innerHTML = result;
  115. tooltipDiv.style.visibility = "visible";
  116. }
  117. else { // hide tooltip
  118. tooltipDiv.style.visibility = "hidden";
  119. }
  120. }
  121. function wazemy_copyLatLon(){
  122. let elMousepos = document.getElementsByClassName("mouse-position");
  123. copyToClipboard(elMousepos[0].innerText);
  124. }
  125. function wazemy_changelayer(arg1){
  126. console.log("WazeMY: changelayer");
  127. }
  128. function wazemy_selectionchanged(arg1){
  129. console.log("WazeMY: selectionchanged");
  130. }
  131. // courtesy of PIE
  132. var copyToClipboard = function(str) {
  133. var $temp = $('<input>');
  134. $('body').append($temp);
  135. $temp.val(str).select();
  136. document.execCommand('copy');
  137. $temp.remove();
  138. };
  139.  
  140. function wazemy_bootstrap(tries) {
  141. if (W && W.map && W.model && WazeWrap.Ready) {
  142. WazeMY_init();
  143. // WazeWrap.Alerts.info(GM_info.script.name, "Loaded...");
  144. }
  145. else if (tries < 1000) {
  146. // console.log(GM_info.script.name, "wazemy_bootstrap failed. Trying again...");
  147. window.setTimeout(wazemy_bootstrap, 200, ++tries);
  148. }
  149. else {
  150. WazeWrap.Alerts.error(GM_info.script.name, "Bootstrap timed out.");
  151. }
  152. }
  153. wazemy_bootstrap(1);
  154. })();