Waze Editor Profile Enhancements

Pulls the correct forum post count - changed to red to signify the value as pulled from the forum by the script

当前为 2018-07-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Waze Editor Profile Enhancements
  3. // @namespace http://tampermonkey.net/
  4. // @version 2018.07.07.01
  5. // @description Pulls the correct forum post count - changed to red to signify the value as pulled from the forum by the script
  6. // @author JustinS83
  7. // @include https://www.waze.com/*user/editor*
  8. // @include https://beta.waze.com/*user/editor*
  9. // @grant GM_xmlhttpRequest
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. var nawkts, rowwkts, ilwkts = [];
  16. var combinedNAWKT, combinedROWWKT, combinedILWKT= "";
  17. var naMA, rowMA, ilMA;
  18.  
  19. function bootstrap(tries) {
  20. tries = tries || 1;
  21.  
  22. if (W &&
  23. W.EditorProfile &&
  24. $) {
  25. init();
  26. } else if (tries < 1000) {
  27. setTimeout(function () {bootstrap(tries++);}, 200);
  28. }
  29. }
  30.  
  31. bootstrap();
  32.  
  33. async function init(){
  34. $.get('https://www.waze.com/forum/memberlist.php?username=' + W.EditorProfile.data.username, function(forumResult){
  35. var re = 0;
  36. var matches = forumResult.match(/<a.*?"Search user’s posts">(\d+)<\/a>/);
  37. if(matches && matches.length > 0)
  38. re = matches[1];
  39. var WazeVal = $('#header > div > div.user-info > div > div.user-highlights > div > div:nth-child(3) > div.user-stats-value')[0].innerHTML.trim();
  40. var userForumID = forumResult.match(/<a href="\.\/memberlist\.php\?mode=viewprofile&amp;u=(\d+)"/)[1];
  41.  
  42. $('#header > div > div.user-info > div > div.user-highlights > div > div:nth-child(3) > div.highlight-title').css('position', 'relative');
  43.  
  44. if(WazeVal !== re.toString()){
  45. $('#header > div > div.user-info > div > div.user-highlights > div > div:nth-child(3) > div.user-stats-value')[0].innerHTML = re;
  46. $('#header > div > div.user-info > div > div.user-highlights > div > div:nth-child(3) > div.user-stats-value').css('color','red');
  47. $('#header > div > div.user-info > div > div.user-highlights > div > div:nth-child(3) > div.user-stats-value').prop('title', 'Waze reported value: ' + WazeVal);
  48. }
  49.  
  50. $('#header > div > div.user-info > div > div.user-highlights > div > div:nth-child(3)').wrap('<a href="https://www.waze.com/forum/search.php?author_id=' + userForumID + '&sr=posts" targ="_blank"></a>');
  51.  
  52. $('#header > div > div.user-info > div > div.user-highlights > a').prepend('<a href="https://www.waze.com/forum/memberlist.php?mode=viewprofile&u=' + userForumID +'" target="_blank" style="margin-right:5px;"><button class="message s-modern-button s-modern"><i class="fa fa-user"></i><span>Forum Profile</span></button></a>');
  53. });
  54.  
  55. var count = 0;
  56. W.EditorProfile.data.editingActivity.forEach(function(x) { if(x !== 0) count++; });
  57. $('#editing-activity > div > h3').append(" (" + count + " of last 91 days)");
  58.  
  59. await getManagedAreas();
  60.  
  61. if(naMA.managedAreas.length > 0){
  62. $('#header > div > div.user-info > div > div.user-highlights > a').append('<a href="#" title="View editor\'s managed areas in WKT format"><button class="message s-modern-button s-modern" id="userMA"><i class="fa fa-map-o" aria-hidden="true"></i></button></a>');
  63.  
  64. $('#userMA').click(function(){
  65. if($('#wpeWKT').css('visibility') === 'visible')
  66. $('#wpeWKT').css({'visibility': 'hidden'});
  67. else
  68. $('#wpeWKT').css({'visibility': 'visible'});
  69. });
  70.  
  71. var result = buildWKTArray(naMA);
  72. nawkts = result.wktArr;
  73. combinedNAWKT = result.combinedWKT;
  74.  
  75. result = buildWKTArray(rowMA);
  76. rowwkts = result.wktArr;
  77. combinedROWWKT = result.combinedWKT;
  78.  
  79. result = buildWKTArray(ilMA);
  80. ilwkts = result.wktArr;
  81. combinedILWKT = result.combinedWKT;
  82.  
  83. var $section = $("<div>", {style:"padding:8px 16px"});
  84. $section.html([
  85. '<div id="wpeWKT" style="padding:8px 16px; position:fixed; border-radius:10px; box-shadow:5px 5px 10px 4px Silver; top:25%; left:40%; background-color:white; visibility:hidden;">', //Main div
  86. '<div style="float:right; cursor:pointer;" id="wpeClose"><i class="fa fa-window-close" aria-hidden="true"></i></div>',
  87. '<ul class="nav nav-tabs">',
  88. `${naMA.managedAreas.length > 0 ? '<li class="active"><a data-toggle="pill" href="#naAreas">NA</a></li>' : ''}`,
  89. `${rowMA.managedAreas.length > 0 ? '<li><a data-toggle="pill" href="#rowAreas">ROW</a></li>' : ''}`,
  90. `${ilMA.managedAreas.length > 0 ? '<li><a data-toggle="pill" href="#ilAreas">IL</a></li>' : ''}`,
  91. '</ul>',
  92. '<div class="tab-content">',
  93. '<div id="naAreas" class="tab-pane fade in active">',
  94. '<div id="wpenaAreas" style="float:left;"><h3 style="float:left; left:50%;">Editor Areas</h3><br>' + buildAreaList(nawkts,"na") + '</div>',
  95. '<div id="wpenaPolygons" style="float:left; padding-left:15px;"><h3 style="position:relative; float:left; left:40%;">Area WKT</h3><br><textarea rows="7" cols="55" id="wpenaAreaWKT" style="height:auto;"></textarea></div>',
  96. '</div>',//naAreas
  97. '<div id="rowAreas" class="tab-pane fade">',
  98. '<div id="wperowAreas" style="float:left;"><h3 style="float:left; left:50%;">Editor Areas</h3><br>' + buildAreaList(rowwkts, "row") + '</div>',
  99. '<div id="wperowPolygons" style="float:left; padding-left:15px;"><h3 style="position:relative; float:left; left:40%;">Area WKT</h3><br><textarea rows="7" cols="55" id="wperowAreaWKT" style="height:auto;"></textarea></div>',
  100. '</div>',//rowAreas
  101. '<div id="ilAreas" class="tab-pane fade">',
  102. '<div id="wpeilAreas" style="float:left;"><h3 style="float:left; left:50%;">Editor Areas</h3><br>' + buildAreaList(ilwkts, "il") + '</div>',
  103. '<div id="wpeilPolygons" style="float:left; padding-left:15px;"><h3 style="position:relative; float:left; left:40%;">Area WKT</h3><br><textarea rows="7" cols="55" id="wpeilAreaWKT" style="height:auto;"></textarea></div>',
  104. '</div>',//ilAreas
  105. '<div id="wpeFooter" style="clear:both; margin-top:10px;">View the areas by entering the WKT at <a href="http://map.wazedev.com" target="_blank">http://map.wazedev.com</a></div>',
  106. '</div>', //tab-content
  107. '</div>' //end main div
  108. ].join(' '));
  109.  
  110. $('body').append($section.html());
  111.  
  112. $('[id^="wpenaAreaButton"]').click(function(){
  113. let index = parseInt($(this)[0].id.replace("wpenaAreaButton", ""));
  114. $('#wpenaAreaWKT').text(nawkts[index]);
  115. $('#wpenaPolygons > h3').text(`Area ${index+1} WKT`);
  116. });
  117.  
  118. $('[id^="wperowAreaButton"]').click(function(){
  119. let index = parseInt($(this)[0].id.replace("wperowAreaButton", ""));
  120. $('#wperowAreaWKT').text(rowwkts[index]);
  121. $('#wperowPolygons > h3').text(`Area ${index+1} WKT`);
  122. });
  123.  
  124. $('[id^="wpeilAreaButton"]').click(function(){
  125. let index = parseInt($(this)[0].id.replace("wpeilAreaButton", ""));
  126. $('#wpeilAreaWKT').text(ilwkts[index]);
  127. $('#wpeilPolygons > h3').text(`Area ${index+1} WKT`);
  128. });
  129.  
  130. $('#wpenaCombinedAreaButton').click(function(){
  131. $('#wpenaAreaWKT').text(combinedNAWKT);
  132. $('#wpenaPolygons > h3').text(`Combined Area WKT`);
  133. });
  134.  
  135. $('#wperowCombinedAreaButton').click(function(){
  136. $('#wperowAreaWKT').text(combinedROWWKT);
  137. $('#wperowPolygons > h3').text(`Combined Area WKT`);
  138. });
  139.  
  140. $('#wpeilCombinedAreaButton').click(function(){
  141. $('#wpeilAreaWKT').text(combinedILWKT);
  142. $('#wpeilPolygons > h3').text(`Combined Area WKT`);
  143. });
  144.  
  145. $('#wpeClose').click(function(){
  146. if($('#wpeWKT').css('visibility') === 'visible')
  147. $('#wpeWKT').css({'visibility': 'hidden'});
  148. else
  149. $('#wpeWKT').css({'visibility': 'visible'});
  150. });
  151. }
  152. }
  153.  
  154. function buildAreaList(wkts, server){
  155. let html = "";
  156. for(let i=0; i<wkts.length; i++){
  157. html +=`<button id="wpe${server}AreaButton${i}" class="btn btn-outline-primary" style="margin-bottom:5px;">Area ${i+1}</button><br>`;
  158. }
  159. if(wkts.length > 1)
  160. html +=`<button id="wpe${server}CombinedAreaButton" class="btn btn-outline-primary" style="margin-bottom:5px;">Combined</button><br>`;
  161. return html;
  162. }
  163.  
  164. function buildWKTArray(wktObj){
  165. let wkt = "";
  166. let combined = "";
  167. let wktArr = [];
  168. for(let i=0; i<wktObj.managedAreas.length; i++){
  169. if(i>0)
  170. combined += ",";
  171. wkt = "";
  172. combined += "(";
  173. for(let j=0; j<wktObj.managedAreas[i].coordinates.length; j++){
  174. if(j>0){
  175. wkt += ",";
  176. combined += ",";
  177. }
  178. combined += "(";
  179. wkt +="(";
  180. for(let k=0; k<wktObj.managedAreas[i].coordinates[j].length; k++){
  181. if(k > 0){
  182. wkt+=", ";
  183. combined += ",";
  184. }
  185. wkt += round(parseFloat(wktObj.managedAreas[i].coordinates[j][k][0])).toString() + " " + round(parseFloat(wktObj.managedAreas[i].coordinates[j][k][1])).toString();
  186. combined += round(parseFloat(wktObj.managedAreas[i].coordinates[j][k][0])).toString() + " " + round(parseFloat(wktObj.managedAreas[i].coordinates[j][k][1])).toString();
  187. }
  188. combined += ")";
  189. wkt += ")";
  190. }
  191. combined += ")";
  192. wkt = `POLYGON${wkt}`;
  193. wktArr.push(wkt);
  194. }
  195. if(naMA.managedAreas.length > 1)
  196. combined = `MULTIPOLYGON(${combined})` ;
  197. else
  198. combined = `POLYGON${combined}`;
  199.  
  200. return {wktArr: wktArr, combinedWKT: combined};
  201. }
  202.  
  203. function round(val){
  204. return Math.round(val*1000000)/1000000;
  205. }
  206.  
  207. async function getManagedAreas(){
  208. naMA = await $.get(`https://www.waze.com/Descartes/app/UserProfile/Areas?userID=${W.EditorProfile.data.userID}`);
  209. rowMA = await $.get(`https://www.waze.com/row-Descartes/app/UserProfile/Areas?userID=${W.EditorProfile.data.userID}`);
  210. ilMA = await $.get(`https://www.waze.com/il-Descartes/app/UserProfile/Areas?userID=${W.EditorProfile.data.userID}`);
  211.  
  212. /*return await new W.EditorProfile.Models.ManagedAreas([],{
  213. lastEditEnv: 'na',
  214. userId: W.EditorProfile.data.userID
  215. }).fetch();*/
  216. }
  217. })();