nameZee

Simple Tool to Help Name Undeployed Munzees Quicker

当前为 2017-11-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name nameZee
  3. // @namespace Rynee
  4. // @author hugosoft
  5. // @version 1.0
  6. // @include http://www.munzee.com/*
  7. // @include https://www.munzee.com/*
  8. // @grant unsafeWindow
  9. // @grant GM_xmlhttpRequest
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_deleteValue
  13. // @description Simple Tool to Help Name Undeployed Munzees Quicker
  14. // ==/UserScript==
  15. //
  16. //
  17. //
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. function go() {
  20. var version = "1.2";
  21. var baseURL = "https://www.munzee.com";
  22. var virtualImg = 'virtual';
  23. var currentURL = null;
  24. var username = null;
  25. var logonUsername = null;
  26. var ignoreChars = ['#', '(', ')', '[', ']', '{', '}'];
  27. /**
  28. * is value numeric?
  29. */
  30. function isNumeric(val){
  31. return !isNaN(val);
  32. }
  33. console.log("nameZee Version: " + version);
  34. Number.decPoint = '.';
  35. Number.thousand_sep = ',';
  36. /**
  37. * endsWith
  38. */
  39. String.prototype.endsWith = function(suffix) {
  40. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  41. };
  42. /**
  43. * startsWith
  44. * */
  45. String.prototype.startsWith = function( prefix ) {
  46. return this.substring( 0, prefix.length ) === prefix;
  47. }
  48.  
  49. /**
  50. * replaceAll
  51. */
  52. String.prototype.replaceAll = function (find, replace) {
  53. var str = this;
  54. return str.replace(new RegExp(find, 'g'), replace);
  55. };
  56. /**
  57. * contains
  58. * */
  59. String.prototype.contains = function() {
  60. return String.prototype.indexOf.apply( this, arguments ) !== -1;
  61. };
  62. /**
  63. * extract logonUsername from profile link
  64. * */
  65. function getLogonUsername() {
  66. try {
  67. //search menu (ul)
  68. var container = $($(".user-menu"));
  69. //first li
  70. container = $(container.children().first());
  71. //first a
  72. container = $(container.children().first());
  73. //attribute href
  74. container = container.attr('href');
  75. //console.log(container);
  76. //token with username in href
  77. var res = container.split("/");
  78. //console.log(res);
  79. var logonUsername = res[res.length-2];
  80. return logonUsername;
  81. } catch (e) {
  82. console.log(e);
  83. }
  84. }
  85. $(document).ready(function() {
  86. //checkForUpdate();
  87. currentURL = new String(document.URL);
  88. if (!currentURL.endsWith("/")) {
  89. currentURL = currentURL + "/";
  90. }
  91. username = $(".avatar-username").text();
  92. if (username==undefined || username.length<1) {
  93. username = null;
  94. }
  95. logonUsername = getLogonUsername();
  96. if (logonUsername==undefined || logonUsername.length<1) {
  97. logonUsername = null;
  98. }
  99. //rename undeploys
  100. if (username!=null && logonUsername==username && currentURL.toLowerCase().startsWith(baseURL + '/m/' + username.toLowerCase() + "/undeploys/")) {
  101. //new action button
  102. var container = $('.page-header');
  103. container = $(container).find('.pull-right');
  104. var buttonCode = '<div class="pull-right"><a id="renumberUndeploys" class="btn green" style="margin-left:10px;margin-right:10px">renumber</a></div>';
  105. var optionCode = '<div class="pull-right"><select id="selBracket" class="form-control"><option value="curlyBracket">{ }</option><option value="squareBracket">[ ]</option><option value="roundBracket">( )</option><option selected value="hash">#</option><option value="blank"> </option></select></div>';
  106. $(container).append(optionCode+buttonCode);
  107. //button event
  108. $("#renumberUndeploys").click(function() {
  109. doRenumber();
  110. });
  111. }
  112. //rename deploys
  113. /*if (username!=null && logonUsername==username && currentURL.toLowerCase().startsWith(baseURL + '/m/' + username.toLowerCase() + "/deploys/")) {
  114. //new action button
  115. var container = $('.page-header');
  116. container = $(container).find('h2');
  117. $(container).append(' <div class="pull-right"><a id="renumberDeploys" class="btn green" style="margin-left:10px">renumber</a></div><div class="clearfix"></div>');
  118. //button event
  119. $("#renumberDeploys").click(function() {
  120. doRenumber();
  121. });
  122. }*/
  123. });
  124. /**
  125. * prefix for number
  126. * */
  127. function getPrefix() {
  128. if ($( "#selBracket" ).val()=='hash') {
  129. return "#";
  130. }
  131. if ($( "#selBracket" ).val()=='curlyBracket') {
  132. return '{';
  133. }
  134. if ($( "#selBracket" ).val()=='squareBracket') {
  135. return '[';
  136. }
  137. if ($( "#selBracket" ).val()=='roundBracket') {
  138. return '(';
  139. }
  140. if ($( "#selBracket" ).val()=='blank') {
  141. return ' ';
  142. }
  143. return '[';
  144. }
  145. /**
  146. * suffix for number
  147. * */
  148. function getSuffix() {
  149. if ($( "#selBracket" ).val()=='hash') {
  150. return "";
  151. }
  152. if ($( "#selBracket" ).val()=='curlyBracket') {
  153. return '}';
  154. }
  155. if ($( "#selBracket" ).val()=='squareBracket') {
  156. return ']';
  157. }
  158. if ($( "#selBracket" ).val()=='roundBracket') {
  159. return ')';
  160. }
  161. if ($( "#selBracket" ).val()=='blank') {
  162. return '';
  163. }
  164. return '';
  165. }
  166. /**
  167. * rename undeploys and deploys
  168. * */
  169. function doRenumber() {
  170. var sections = $("section").get();
  171. var count=0;
  172. //inspect each section
  173. for ( var i = 0; i < sections.length; i++ ) {
  174. if(isRenumber(getFriendlyName(sections[i]), getLfdNr(sections[i])) && !isVirtual(sections[i])) {
  175. parseSectionTimeoutWrapper(count++, sections[i]);
  176. } else {
  177. console.log("IGNRORED::" + getFriendlyName(sections[i]) + " virtual=" + isVirtual(sections[i]));
  178. }
  179. }
  180. }
  181. /** just a wrapper for parseSection with timeout*/
  182. function parseSectionTimeoutWrapper(i, section) {
  183. setTimeout(function() {
  184. parseSection(i, section);
  185. }, 1500*i);
  186. }
  187. /**
  188. * parseSection of munzees and call post-method for renumbering
  189. */
  190. function parseSection(i, section) {
  191. var lfdNr = getLfdNr(section);
  192. var originalFriendlyName = getFriendlyName(section);
  193. if(isRenumber(originalFriendlyName, getLfdNr(section)) && !isVirtual(section)) {
  194. var adminURL = 'https://www.munzee.com/m/' + username + '/' + lfdNr + "/admin/";
  195. var oldFriendlyName = removeGeneratedNumber(originalFriendlyName);
  196. var newFriendlyName = oldFriendlyName + " " + getPrefix() + lfdNr + getSuffix();
  197. console.log(adminURL + ":: " + originalFriendlyName + "->" + newFriendlyName);
  198. $.post(adminURL, { friendly_name: ""+newFriendlyName, notes:""}, function(result) {
  199. //console.log(result);
  200. });
  201. }
  202. }
  203. /**
  204. * remove the generated Number of friendly name
  205. */
  206. function removeGeneratedNumber(friendlyName) {
  207. while (friendlyName.length>0 && isNumeric(friendlyName[friendlyName.length-1])) {
  208. friendlyName = friendlyName.substr(0,friendlyName.length-1);
  209. console.log(friendlyName);
  210. }
  211. return friendlyName;
  212. }
  213. /**
  214. * is value numeric?
  215. */
  216. function isNumeric(val){
  217. return !isNaN(val);
  218. }
  219.  
  220. /** get lfdNr of current section*/
  221. function getLfdNr(section) {
  222. var munzeeURL = baseURL + $(section).find('a').attr('href');
  223. arr = munzeeURL.split("/");
  224. return arr[5];
  225. }
  226. /** get friendlyName of current section*/
  227. function getFriendlyName(section) {
  228. var friendlyName = $(section).find("a")[1];
  229. return $(friendlyName).text();
  230. }
  231. /** renumbering permitted? */
  232. function isRenumber(friendlyName, munzeeNumber) {
  233. friendlyName = friendlyName.trim();
  234. //for (i=0; i <ignoreChars.length; i++) {
  235. if (friendlyName.contains(munzeeNumber)) {
  236. return false;
  237. }
  238. //}
  239. return true;
  240. }
  241. function isVirtual(section) {
  242. var imgageSrc = $(section).find(".pin").attr('src');
  243. return imgageSrc.contains(virtualImg);
  244. }
  245.  
  246. } // end go
  247. // jQuery workaround for Chrome
  248. // a function that loads jQuery and calls a callback function when jQuery has finished loading
  249. function addJQuery(callback) {
  250. var script = document.createElement("script");
  251. script.textContent = "(" + callback.toString() + ")();";
  252. document.body.appendChild(script);
  253. }
  254. // load jQuery and execute
  255. addJQuery(go);