Waze Edit Count Monitor

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

当前为 2023-03-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Waze Edit Count Monitor
  3. // @namespace https://greasyfork.org/en/users/45389-mapomatic
  4. // @version 2023.03.13.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; margin-left: 4px;' });
  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. $('#save-button').prepend(_$outputElemContainer);
  66. _$outputElem.tooltip({
  67. placement: 'auto top',
  68. delay: { show: 100, hide: 100 },
  69. html: true,
  70. template: '<div class="tooltip" role="tooltip" style="opacity:0.95"><div class="tooltip-arrow"></div>'
  71. + '<div class="my-tooltip-header" style="display:block;"><b></b></div>'
  72. + '<div class="my-tooltip-body tooltip-inner" style="display:block; !important; min-width: fit-content"></div></div>'
  73. });
  74. }
  75.  
  76. // log('edit count = ' + editCount + ', UR count = ' + urCount.count);
  77. if (_lastEditCount !== editCount || _lastURCount.count !== urCount.count || _lastMPCount.count !== mpCount.count) {
  78. _savesWithoutIncrease = 0;
  79. } else if (!noIncrement) {
  80. _savesWithoutIncrease++;
  81. }
  82.  
  83. let textColor;
  84. let bgColor;
  85. let tooltipTextColor;
  86. if (_savesWithoutIncrease < 5) {
  87. textColor = '#354148';
  88. bgColor = 'white';
  89. tooltipTextColor = 'black';
  90. } else if (_savesWithoutIncrease < 10) {
  91. textColor = '#354148';
  92. bgColor = 'yellow';
  93. tooltipTextColor = 'black';
  94. } else {
  95. textColor = 'white';
  96. bgColor = 'red';
  97. tooltipTextColor = 'white';
  98. }
  99. _$outputElemContainer.css('background-color', bgColor);
  100. _$outputElem.css('color', textColor).html(editCount);
  101. const urCountText = `<div style="margin-top:8px;padding:3px;">URs&nbsp;Closed:&nbsp;${urCount.count.toLocaleString()}&nbsp;&nbsp;(since&nbsp;${
  102. (new Date(urCount.since)).toLocaleDateString()})</div>`;
  103. const purCountText = `<div style="margin-top:0px;padding:0px 3px;">PURs&nbsp;Closed:&nbsp;${purCount.count.toLocaleString()}&nbsp;&nbsp;(since&nbsp;${(
  104. new Date(purCount.since)).toLocaleDateString()})</div>`;
  105. const mpCountText = `<div style="margin-top:0px;padding:0px 3px;">MPs&nbsp;Closed:&nbsp;${mpCount.count.toLocaleString()}&nbsp;&nbsp;(since&nbsp;${(
  106. new Date(mpCount.since)).toLocaleDateString()})</div>`;
  107. let warningText = '';
  108. if (_savesWithoutIncrease) {
  109. warningText = `<div style="border-radius:8px;padding:3px;margin-top:8px;margin-bottom:5px;color:${
  110. tooltipTextColor};background-color:${bgColor};">${_savesWithoutIncrease} ${
  111. (_savesWithoutIncrease > 1) ? 'consecutive saves' : 'save'} without an increase. ${
  112. (_savesWithoutIncrease >= 5) ? '(Are you throttled?)' : ''}</div>`;
  113. }
  114. _$outputElem.attr('data-original-title', TOOLTIP_TEXT + urCountText + purCountText + mpCountText + warningText);
  115. _lastEditCount = editCount;
  116. _lastURCount = urCount;
  117. _lastMPCount = mpCount;
  118. }
  119.  
  120. function receiveMessage(event) {
  121. let msg;
  122. try {
  123. msg = JSON.parse(event.data);
  124. } catch (err) {
  125. // Do nothing
  126. }
  127.  
  128. if (msg && msg[0] === 'wecmUpdateUi') {
  129. const editCount = msg[1][0];
  130. const urCount = msg[1][1];
  131. const purCount = msg[1][2];
  132. const mpCount = msg[1][3];
  133. updateEditCount(editCount, urCount, purCount, mpCount);
  134. }
  135. }
  136.  
  137. function errorHandler(callback) {
  138. try {
  139. callback();
  140. } catch (ex) {
  141. console.error('Edit Count Monitor:', ex);
  142. }
  143. }
  144.  
  145. async function init() {
  146. _userName = W.loginManager.user.userName;
  147. // Listen for events from sandboxed code.
  148. window.addEventListener('message', receiveMessage);
  149. // Listen for Save events.
  150.  
  151. $('head').append(
  152. $('<link/>', {
  153. rel: 'stylesheet',
  154. type: 'text/css',
  155. href: 'https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css'
  156. }),
  157. $('<style type="text/css">#toast-container {position: absolute;} #toast-container > div {opacity: 0.95;} .toast-top-center {top: 30px;}</style>')
  158. );
  159. await $.getScript(TOASTR_URL);
  160. toastr.options = {
  161. target: '#map',
  162. timeOut: 9999999999,
  163. positionClass: 'toast-top-right',
  164. closeOnHover: false,
  165. closeDuration: 0,
  166. showDuration: 0,
  167. closeButton: true
  168. // preventDuplicates: true
  169. };
  170. W.model.actionManager.events.register('afterclearactions', null, () => errorHandler(checkEditCount));
  171.  
  172. // Update the edit count first time.
  173. checkEditCount();
  174. log('Initialized.');
  175. }
  176.  
  177. function bootstrap() {
  178. if (W && W.loginManager && W.loginManager.events && W.loginManager.events.register && W.map && W.loginManager.user) {
  179. log('Initializing...');
  180. init();
  181. } else {
  182. log('Bootstrap failed. Trying again...');
  183. setTimeout(bootstrap, 1000);
  184. }
  185. }
  186.  
  187. bootstrap();
  188. }
  189.  
  190. // Code that is NOT injected into the page.
  191. // Note that jQuery may or may not be available, so don't rely on it in this part of the script.
  192.  
  193. function getEditCountFromProfile(profile) {
  194. 'use strict';
  195.  
  196. const { editingActivity } = profile;
  197. return editingActivity[editingActivity.length - 1];
  198. }
  199.  
  200. function getEditCountByTypeFromProfile(profile, type) {
  201. 'use strict';
  202.  
  203. const edits = profile.editsByType.find(editsEntry => editsEntry.key === type);
  204. return edits ? edits.value : -1;
  205. }
  206.  
  207. // Handle messages from the page.
  208. function receivePageMessage(event) {
  209. 'use strict';
  210.  
  211. let msg;
  212. try {
  213. msg = JSON.parse(event.data);
  214. } catch (err) {
  215. // Ignore errors
  216. }
  217.  
  218. if (msg && msg[0] === 'wecmGetCounts') {
  219. const userName = msg[1];
  220. GM_xmlhttpRequest({
  221. method: 'GET',
  222. url: `https://www.waze.com/Descartes/app/UserProfile/Profile?username=${userName}`,
  223. onload: res => {
  224. const profile = JSON.parse(res.responseText);
  225. window.postMessage(JSON.stringify(['wecmUpdateUi', [
  226. getEditCountFromProfile(profile),
  227. getEditCountByTypeFromProfile(profile, 'mapUpdateRequest'),
  228. getEditCountByTypeFromProfile(profile, 'venueUpdateRequest'),
  229. getEditCountByTypeFromProfile(profile, 'machineMapProblem')
  230. ]]), '*');
  231. }
  232. });
  233. }
  234. }
  235.  
  236. GM_addElement('script', {
  237. textContent: `${wecmInjected.toString()} \nwecmInjected();`
  238. });
  239.  
  240. // Listen for events coming from the page script.
  241. window.addEventListener('message', receivePageMessage);