Waze Edit Count Monitor

Displays your daily edit count in the WME toolbar. Warns if you might be throttled.

当前为 2023-04-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Waze Edit Count Monitor
  3. // @namespace https://greasyfork.org/en/users/45389-mapomatic
  4. // @version 2023.04.25.001
  5. // @description Displays your daily edit count in the WME toolbar. Warns if you might be throttled.
  6. // @author MapOMatic
  7. // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
  8. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
  9. // @license GNU GPLv3
  10. // @contributionURL https://github.com/WazeDev/Thank-The-Authors
  11. // @grant GM_xmlhttpRequest
  12. // @grant GM_addElement
  13. // @connect www.waze.com
  14. // ==/UserScript==
  15.  
  16. /* global W */
  17. /* global toastr */
  18.  
  19. // This function is injected into the page to allow it to run in the page's context.
  20. function wecmInjected() {
  21. 'use strict';
  22.  
  23. const TOASTR_URL = 'https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js';
  24. const TOASTR_SETTINGS = {
  25. remindAtEditCount: 100,
  26. warnAtEditCount: 150,
  27. wasReminded: false,
  28. wasWarned: false
  29. };
  30. const TOOLTIP_TEXT = 'Your daily edit count from your profile. Click to open your profile.';
  31.  
  32. let _$outputElem = null;
  33. let _$outputElemContainer = null;
  34. let _lastEditCount = null;
  35. let _userName = null;
  36. let _savesWithoutIncrease = 0;
  37. let _lastURCount = null;
  38. let _lastMPCount = null;
  39.  
  40. function log(message) {
  41. console.log('Edit Count Monitor:', message);
  42. }
  43.  
  44. function checkEditCount() {
  45. window.postMessage(JSON.stringify(['wecmGetCounts', _userName]), '*');
  46. TOASTR_SETTINGS.wasReminded = false;
  47. TOASTR_SETTINGS.wasWarned = false;
  48. toastr.remove();
  49. }
  50.  
  51. function updateEditCount(editCount, urCount, purCount, mpCount, noIncrement) {
  52. // Add the counter div if it doesn't exist.
  53. if ($('#wecm-count').length === 0) {
  54. _$outputElemContainer = $('<div>', { class: 'toolbar-button', style: 'font-weight: bold; font-size: 16px; border-radius: 10px;' });
  55. const $innerDiv = $('<div>', { class: 'item-container', style: 'padding-left: 10px; padding-right: 10px; cursor: default;' });
  56. _$outputElem = $('<a>', {
  57. id: 'wecm-count',
  58. href: `https://www.waze.com/user/editor/${_userName.toLowerCase()}`,
  59. target: '_blank',
  60. style: 'text-decoration:none',
  61. 'data-original-title': TOOLTIP_TEXT
  62. });
  63. $innerDiv.append(_$outputElem);
  64. _$outputElemContainer.append($innerDiv);
  65. // 4/5/2023 - added this to fix issue in beta WME. Can be removed once the layout reaches production.
  66. if (document.querySelector('#save-button')?.parentElement.id !== 'edit-buttons') {
  67. $('#toolbar > div > div.secondary-toolbar > div.secondary-toolbar-actions > div.secondary-toolbar-actions-edit').after(_$outputElemContainer);
  68. }
  69. _$outputElem.tooltip({
  70. placement: 'auto top',
  71. delay: { show: 100, hide: 100 },
  72. html: true,
  73. template: '<div class="tooltip" role="tooltip" style="opacity:0.95"><div class="tooltip-arrow"></div>'
  74. + '<div class="my-tooltip-header" style="display:block;"><b></b></div>'
  75. + '<div class="my-tooltip-body tooltip-inner" style="display:block; !important; min-width: fit-content"></div></div>'
  76. });
  77. }
  78.  
  79. // log('edit count = ' + editCount + ', UR count = ' + urCount.count);
  80. if (_lastEditCount !== editCount || _lastURCount.count !== urCount.count || _lastMPCount.count !== mpCount.count) {
  81. _savesWithoutIncrease = 0;
  82. } else if (!noIncrement) {
  83. _savesWithoutIncrease++;
  84. }
  85.  
  86. let textColor;
  87. let bgColor;
  88. let tooltipTextColor;
  89. if (_savesWithoutIncrease < 5) {
  90. textColor = '#354148';
  91. bgColor = 'white';
  92. tooltipTextColor = 'black';
  93. } else if (_savesWithoutIncrease < 10) {
  94. textColor = '#354148';
  95. bgColor = 'yellow';
  96. tooltipTextColor = 'black';
  97. } else {
  98. textColor = 'white';
  99. bgColor = 'red';
  100. tooltipTextColor = 'white';
  101. }
  102. _$outputElemContainer.css('background-color', bgColor);
  103. _$outputElem.css('color', textColor).html(editCount);
  104. const urCountText = `<div style="margin-top:8px;padding:3px;">URs&nbsp;Closed:&nbsp;${urCount.count.toLocaleString()}&nbsp;&nbsp;(since&nbsp;${
  105. (new Date(urCount.since)).toLocaleDateString()})</div>`;
  106. const purCountText = `<div style="margin-top:0px;padding:0px 3px;">PURs&nbsp;Closed:&nbsp;${purCount.count.toLocaleString()}&nbsp;&nbsp;(since&nbsp;${(
  107. new Date(purCount.since)).toLocaleDateString()})</div>`;
  108. const mpCountText = `<div style="margin-top:0px;padding:0px 3px;">MPs&nbsp;Closed:&nbsp;${mpCount.count.toLocaleString()}&nbsp;&nbsp;(since&nbsp;${(
  109. new Date(mpCount.since)).toLocaleDateString()})</div>`;
  110. let warningText = '';
  111. if (_savesWithoutIncrease) {
  112. warningText = `<div style="border-radius:8px;padding:3px;margin-top:8px;margin-bottom:5px;color:${
  113. tooltipTextColor};background-color:${bgColor};">${_savesWithoutIncrease} ${
  114. (_savesWithoutIncrease > 1) ? 'consecutive saves' : 'save'} without an increase. ${
  115. (_savesWithoutIncrease >= 5) ? '(Are you throttled?)' : ''}</div>`;
  116. }
  117. _$outputElem.attr('data-original-title', TOOLTIP_TEXT + urCountText + purCountText + mpCountText + warningText);
  118. _lastEditCount = editCount;
  119. _lastURCount = urCount;
  120. _lastMPCount = mpCount;
  121. }
  122.  
  123. function receiveMessage(event) {
  124. let msg;
  125. try {
  126. msg = JSON.parse(event.data);
  127. } catch (err) {
  128. // Do nothing
  129. }
  130.  
  131. if (msg && msg[0] === 'wecmUpdateUi') {
  132. const editCount = msg[1][0];
  133. const urCount = msg[1][1];
  134. const purCount = msg[1][2];
  135. const mpCount = msg[1][3];
  136. updateEditCount(editCount, urCount, purCount, mpCount);
  137. }
  138. }
  139.  
  140. function errorHandler(callback) {
  141. try {
  142. callback();
  143. } catch (ex) {
  144. console.error('Edit Count Monitor:', ex);
  145. }
  146. }
  147.  
  148. async function init() {
  149. _userName = W.loginManager.user.userName;
  150. // Listen for events from sandboxed code.
  151. window.addEventListener('message', receiveMessage);
  152. // Listen for Save events.
  153.  
  154. $('head').append(
  155. $('<link/>', {
  156. rel: 'stylesheet',
  157. type: 'text/css',
  158. href: 'https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css'
  159. }),
  160. $('<style type="text/css">#toast-container {position: absolute;} #toast-container > div {opacity: 0.95;} .toast-top-center {top: 30px;}</style>')
  161. );
  162. await $.getScript(TOASTR_URL);
  163. toastr.options = {
  164. target: '#map',
  165. timeOut: 9999999999,
  166. positionClass: 'toast-top-right',
  167. closeOnHover: false,
  168. closeDuration: 0,
  169. showDuration: 0,
  170. closeButton: true
  171. // preventDuplicates: true
  172. };
  173. W.model.actionManager.events.register('afterclearactions', null, () => errorHandler(checkEditCount));
  174.  
  175. // Update the edit count first time.
  176. checkEditCount();
  177. log('Initialized.');
  178. }
  179.  
  180. function bootstrap() {
  181. if (W && W.loginManager && W.loginManager.events && W.loginManager.events.register && W.map && W.loginManager.user) {
  182. log('Initializing...');
  183. init();
  184. } else {
  185. log('Bootstrap failed. Trying again...');
  186. setTimeout(bootstrap, 1000);
  187. }
  188. }
  189.  
  190. bootstrap();
  191. }
  192.  
  193. // Code that is NOT injected into the page.
  194. // Note that jQuery may or may not be available, so don't rely on it in this part of the script.
  195.  
  196. function getEditCountFromProfile(profile) {
  197. 'use strict';
  198.  
  199. const { editingActivity } = profile;
  200. return editingActivity[editingActivity.length - 1];
  201. }
  202.  
  203. function getEditCountByTypeFromProfile(profile, type) {
  204. 'use strict';
  205.  
  206. const edits = profile.editsByType.find(editsEntry => editsEntry.key === type);
  207. return edits ? edits.value : -1;
  208. }
  209.  
  210. // Handle messages from the page.
  211. function receivePageMessage(event) {
  212. 'use strict';
  213.  
  214. let msg;
  215. try {
  216. msg = JSON.parse(event.data);
  217. } catch (err) {
  218. // Ignore errors
  219. }
  220.  
  221. if (msg && msg[0] === 'wecmGetCounts') {
  222. const userName = msg[1];
  223. GM_xmlhttpRequest({
  224. method: 'GET',
  225. url: `https://www.waze.com/Descartes/app/UserProfile/Profile?username=${userName}`,
  226. onload: res => {
  227. const profile = JSON.parse(res.responseText);
  228. window.postMessage(JSON.stringify(['wecmUpdateUi', [
  229. getEditCountFromProfile(profile),
  230. getEditCountByTypeFromProfile(profile, 'mapUpdateRequest'),
  231. getEditCountByTypeFromProfile(profile, 'venueUpdateRequest'),
  232. getEditCountByTypeFromProfile(profile, 'machineMapProblem')
  233. ]]), '*');
  234. }
  235. });
  236. }
  237. }
  238.  
  239. GM_addElement('script', {
  240. textContent: `${wecmInjected.toString()} \nwecmInjected();`
  241. });
  242.  
  243. // Listen for events coming from the page script.
  244. window.addEventListener('message', receivePageMessage);