Waze Edit Count Monitor (pt-BR)

Owner is MapOMatic. Translation by me. Mostra a contagem diária de suas edições realizadas no WME. Alerta se está acelerado. Original link: https://greasyfork.org/en/scripts/19941-waze-edit-count-monitor

  1. // ==UserScript==
  2. // @name Waze Edit Count Monitor (pt-BR)
  3. // @namespace
  4. // @version 0.9.2
  5. // @description Owner is MapOMatic. Translation by me. Mostra a contagem diária de suas edições realizadas no WME. Alerta se está acelerado. Original link: https://greasyfork.org/en/scripts/19941-waze-edit-count-monitor
  6. // @author GabiruDriverX
  7. // @include https://beta.waze.com/*editor/*
  8. // @include https://www.waze.com/*editor/*
  9. // @exclude https://www.waze.com/*user/editor/*
  10. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
  11. // @grant GM_xmlhttpRequest
  12. // @connect www.waze.com
  13. // @icon https://wiki.waze.com/wiki/images/3/31/Wazer-with-premission.png
  14.  
  15. // original link: https://greasyfork.org/en/scripts/19941-waze-edit-count-monitor
  16.  
  17. // ==/UserScript==
  18.  
  19. function WECM_Injected() {
  20. var debugLevel = 0;
  21. var $outputElem = null;
  22. var $outputElemContainer = null;
  23. var pollingTime = 1000; // Time between checking for saves (msec).
  24. var lastEditCount = null;
  25. var lastCanSave = true;
  26. var userName = null;
  27. var savesWithoutIncrease = 0;
  28. var lastURCount = null;
  29. var tooltipText = 'Contagem diária de suas edições. Clique para abrir seu perfil.';
  30.  
  31. function log(message, level) {
  32. if (message && level <= debugLevel) {
  33. console.log('Monitor de contagem de edição: ' + message);
  34. }
  35. }
  36.  
  37. function checkForSave() {
  38. var canSave = W.model.actionManager.canSave();
  39. var canRedo = W.model.actionManager.canRedo();
  40. if (lastCanSave && !canSave && !canRedo) {
  41. window.postMessage(JSON.stringify(['wecmGetCounts',userName]),'*');
  42. }
  43. lastCanSave = canSave;
  44. }
  45.  
  46. // This is a hack, because I haven't had time to figure out how to listen for a 'save' event yet.
  47. function loopCheck() {
  48. checkForSave();
  49. setTimeout(loopCheck, pollingTime);
  50. }
  51.  
  52. function updateEditCount(editCount, urCount) {
  53. var textColor;
  54. var bgColor;
  55. var tooltipTextColor;
  56.  
  57. log('edit count = ' + editCount + ', UR count = ' + urCount.count, 1);
  58. if (lastEditCount !== editCount || lastURCount !== urCount.count) {
  59. savesWithoutIncrease = 0;
  60. } else {
  61. savesWithoutIncrease += 1;
  62. }
  63.  
  64. switch (savesWithoutIncrease) {
  65. case 0:
  66. case 1:
  67. textColor = '';
  68. bgColor = '';
  69. tooltipTextColor = 'white';
  70. break;
  71. case 2:
  72. textColor = '';
  73. bgColor = 'yellow';
  74. tooltipTextColor = 'black';
  75. break;
  76. default:
  77. textColor = 'white';
  78. bgColor = 'red';
  79. tooltipTextColor = 'white';
  80. }
  81. $outputElemContainer.css('background-color', bgColor);
  82. $outputElem.css('color', textColor).html('Edições:&nbsp;' + editCount);
  83. var urCountText = "<div style='margin-top:8px;padding:3px;'>UR's&nbsp;fechadas:&nbsp;" + urCount.count + "&nbsp;&nbsp;\n(desde&nbsp;" + (new Date(urCount.since)).toLocaleDateString() + ")</div>";
  84. var warningText = (savesWithoutIncrease > 0) ? "<div style='border-radius:8px;padding:3px;margin-top:8px;margin-bottom:5px;color:"+ tooltipTextColor + ";background-color:" + bgColor + ";'>" + savesWithoutIncrease + ' Consecutivos saves sem aumentar. (Você está acelerado?)</div>' : '';
  85. $outputElem.attr('data-original-title', tooltipText + urCountText + warningText);
  86. lastEditCount = editCount;
  87. lastURCount = urCount.count;
  88. }
  89.  
  90. function receiveMessage(event) {
  91. var msg;
  92. try {
  93. msg = JSON.parse(event.data);
  94. }
  95. catch (err) {
  96. // Do nothing
  97. }
  98.  
  99. if (msg && msg[0] === "wecmUpdateUi") {
  100. var editCount = msg[1][0];
  101. var urCount = msg[1][1];
  102. updateEditCount(editCount, urCount);
  103. }
  104. }
  105.  
  106. function init() {
  107. 'use strict';
  108.  
  109. userName = W.loginManager.user.userName;
  110. $outputElemContainer = $('<div>', {style:'border-radius: 23px; height: 23px; display: inline; float: right; padding-left: 10px; padding-right: 10px; margin: 9px 5px 8px 5px; font-weight: bold; font-size: medium;'});
  111. $outputElem = $('<a>', {id: 'wecm-count',
  112. href:'https://www.waze.com/user/editor/' + userName.toLowerCase(),
  113. target: "_blank",
  114. style:'text-decoration:none',
  115. 'data-original-title': tooltipText});
  116. $outputElemContainer.append($outputElem);
  117. $('.waze-icon-place').parent().prepend($outputElemContainer);
  118. $outputElem.tooltip({
  119. placement: 'auto top',
  120. delay: {show: 100, hide: 100},
  121. html: true,
  122. template: '<div class="tooltip" role="tooltip" style="opacity:0.95"><div class="tooltip-arrow"></div><div class="my-tooltip-header"><b></b></div><div class="my-tooltip-body tooltip-inner" style="font-weight: 600; !important"></div></div>'
  123. });
  124.  
  125. window.addEventListener('message', receiveMessage);
  126.  
  127. loopCheck();
  128.  
  129. log('Initialized.',0);
  130. }
  131.  
  132. function bootstrap()
  133. {
  134. if (W &&
  135. W.loginManager &&
  136. W.loginManager.events &&
  137. W.loginManager.events.register &&
  138. W.map &&
  139. W.loginManager.isLoggedIn()) {
  140. log('Inicializando...', 0);
  141. init();
  142. } else {
  143. log('Bootstrap falhou. Tente novamente...', 0);
  144. window.setTimeout(function () {
  145. bootstrap();
  146. }, 1000);
  147. }
  148. }
  149.  
  150. bootstrap();
  151. }
  152.  
  153.  
  154. /* Code that is NOT injected into the page */
  155. (function(){
  156. var alertUpdate = true;
  157. var wecmVersion = GM_info.script.version;
  158. var wecmChangesHeader = "Waze Edit Count Monitor foi atualizado.\nv" + wecmVersion + "\n\nO que há de novo\n-------------------------";
  159. var wecmChanges = wecmChangesHeader + "\n- Agora deve funcionar em FF Greasemonkey e em WME beta.";
  160.  
  161. function getEditorProfileFromSource(source) {
  162. var match = source.match(/W.EditorProfile.data\s=\s+JSON.parse\('(.*?)'\)/i);
  163. return JSON.parse(match[1]);
  164. }
  165.  
  166. function getEditCountFromProfile(profile) {
  167. var editingActivity = profile.editingActivity;
  168. return editingActivity[editingActivity.length-1];
  169. }
  170.  
  171. function getURCountFromProfile(profile) {
  172. var editsByType = profile.editsByType;
  173. for (i=0; i < editsByType.length; i++) {
  174. if (editsByType[i].key == 'mapUpdateRequest') {
  175. return editsByType[i].value;
  176. }
  177. }
  178. return -1;
  179. }
  180.  
  181. // Listen for a message from the page.
  182. function receiveMessage(event) {
  183. var msg;
  184. try {
  185. msg = JSON.parse(event.data);
  186. }
  187. catch (err) {
  188. // Do nothing
  189. }
  190.  
  191. if (msg && msg[0] === "wecmGetCounts") {
  192. var userName = msg[1];
  193. GM_xmlhttpRequest({
  194. method: "GET",
  195. url: 'https://www.waze.com/user/editor/' + userName,
  196. onload: function(res) {
  197. var profile = getEditorProfileFromSource(res.responseText);
  198. window.postMessage(JSON.stringify(['wecmUpdateUi',[getEditCountFromProfile(profile), getURCountFromProfile(profile)]]),'*');
  199. }
  200. });
  201. }
  202. }
  203.  
  204. $(document).ready(function() {
  205. /* Check version and alert on update */
  206. if (alertUpdate && ('undefined' === window.localStorage.wecmVersion ||
  207. wecmVersion !== window.localStorage.wecmVersion)) {
  208. alert(wecmChanges);
  209. window.localStorage.wecmVersion = wecmVersion;
  210. }
  211. });
  212.  
  213. var WECM_Injected_script = document.createElement("script");
  214. WECM_Injected_script.textContent = "" + WECM_Injected.toString() + " \n" + "WECM_Injected();";
  215. WECM_Injected_script.setAttribute("type", "application/javascript");
  216. document.body.appendChild(WECM_Injected_script);
  217. window.addEventListener('message', receiveMessage);
  218.  
  219. })();