nameZee

Simple Tool to Help Name Undeployed Munzees Quicker

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