Mint.com Customize Default Categories

Hide specified default built-in mint.com categories

目前为 2015-04-07 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Mint.com Customize Default Categories
  3. // @namespace com.schrauger.mint.js
  4. // @author Stephen Schrauger
  5. // @description Hide specified default built-in mint.com categories
  6. // @homepage https://github.com/schrauger/mint.com-customize-default-categories
  7. // @include https://*.mint.com/*
  8. // @version 1.3.2
  9. // @grant none
  10. // ==/UserScript==
  11. /*jslint browser: true*/
  12. /*global jQuery*/
  13. (function () {
  14. function after_jquery() {
  15. var bit_flags_per_char = 6; // using 01XXXXXX ASCII codes, which allows for 6 flags per character
  16. var unique_id_length = 4; // the bit array starts with '#!1 ' or '#!2 ' or '#!3 '
  17. var categories_per_string = 8; // with 20 characters per string, and 2 characters per category, we can fit 8 (plus the 4-char unique id)
  18. var characters_per_category = 2; // with 6 flags, this allows for 11 sub categories and 1 major category
  19. var number_of_bit_arrays = 3; // use 3 arrays to store all preferences.
  20. var category_id = 20; // save the custom fields in the 'uncategorized' major category, which has the id of 20
  21. var class_hidden = 'sgs-hide-from-mint'; // just a unique class; if an element has it, it will be hidden.
  22. var class_edit_mode = 'mint_edit_mode';
  23.  
  24. var hs_action_hide = 'hide';
  25. var hs_action_show = 'show';
  26. var hs_action_edit = 'edit';
  27.  
  28. // Need to define all categories and subcategories, along with their ID. Create this list dynamically.
  29. function get_default_category_list() {
  30. var categories = [];
  31.  
  32. // loop through each category and create an array of arrays with their info
  33. jQuery('#popup-cc-L1').find('> li').each(function () {
  34. var category_major = [];
  35. category_major.id = jQuery(this).attr('id').replace(/\D/g, ''); // number-only portion (they all start with 'pop-categories-'{number}
  36. //console.log(caconsole.logtegory_major.id);
  37. category_major.name = jQuery(this).children('a').text();
  38. category_major.categories_minor = [];
  39. category_major.is_hidden = jQuery(this).hasClass(class_hidden);
  40. /* get the minor/sub categories. only the :first ul, because the second one is
  41. user-defined custom categories, which they can change with native mint.com controls
  42. */
  43. jQuery(this).find('div.popup-cc-L2 ul:first > li').each(function () {
  44. var category_minor = [];
  45. category_minor.id = jQuery(this).attr('id').replace(/\D/g, '');
  46. category_minor.name = jQuery(this).text();
  47. category_minor.is_hidden = jQuery(this).hasClass(class_hidden);
  48. category_major.categories_minor.push(category_minor);
  49. });
  50. categories.push(category_major);
  51. });
  52.  
  53. return categories;
  54. }
  55.  
  56. // Save any categories the user wants hidden.
  57. // This will be done by creating a custom subcategory in the 'uncategorized' category, where the name of this
  58. // subcategory will define which other categories to hide.
  59. // This way, if the UserScript is installed on multiple computers, the user sees their preferences synced.
  60. // Alternatively, a cookie could be used, but preferences would be specific to that device.
  61.  
  62. /**
  63. * @str_bit_array String A printable-ascii encoded bit array (values that can be saved in a custom category)
  64. * @array_of_eight_categories Array 8 categories with their subcategories and
  65. * ID, name, subcategories and is_hidden members
  66. * for both the category and subcategories
  67. */
  68. function decode_bit_array(str_bit_array_array, array_of_all_categories) {
  69.  
  70. // second, translate any extra characters into their forbidden character
  71. // (double quote is forbidden; use a non-bit-array character to encode)
  72.  
  73. // third, loop through each category and each of its subcategories
  74.  
  75. // use bitwise operators to see if the subcategory minor id (always 1-9) is marked as hidden
  76.  
  77.  
  78. str_bit_array_array.sort();
  79.  
  80. array_of_all_categories.sort(function (a, b) {
  81. return (a.id - b.id); // sort by id, lowest first
  82. });
  83.  
  84.  
  85. var field_count = 0; // 0, 1, or 2; for the 3 unique fields holding the 23 category hidden attributes
  86. var bit_string_category_count = 0; // only 8 categories per string. once this goes past 7, reset and use next string.
  87. var str_bit_array = str_bit_array_array[field_count]; // 3 custom fields with attributes
  88. // remove the first 4 characters (the unique ID plus a space)
  89. str_bit_array = str_bit_array.substring(unique_id_length); // 0-based, meaning start at the fifth character (inclusive)
  90.  
  91. // loop through each major category and its minor categories and mark them as hidden or not
  92. for (var category_major_count = 0, category_major_length = array_of_all_categories.length; category_major_count < category_major_length; category_major_count++) {
  93. var category_major = array_of_all_categories[category_major_count];
  94.  
  95. array_of_all_categories[category_major_count].categories_minor.sort(function (a, b) {
  96. return (a.id - b.id); // sort by id, lowest first
  97. });
  98.  
  99.  
  100. var bit_characters = str_bit_array.substr(bit_string_category_count * characters_per_category, characters_per_category); // grab the 2 characters for this category
  101.  
  102. for (var category_minor_count = 0, category_minor_length = category_major.categories_minor.length; category_minor_count < category_minor_length; category_minor_count++) {
  103.  
  104. var minor_id = category_major.categories_minor[category_minor_count].id.slice(-2); // the last two digits are the minor category; the first two are always the same as the parent major category
  105. // if flag is '1', it is hidden
  106. array_of_all_categories[category_major_count].categories_minor[category_minor_count].is_hidden = is_category_hidden(bit_characters, minor_id);
  107. //console.debug(array_of_all_categories[category_major_count].categories_minor[category_minor_count].name + ' is hidden: '
  108. // + array_of_all_categories[category_major_count].categories_minor[category_minor_count].is_hidden);
  109. }
  110. var last_flag = characters_per_category * bit_flags_per_char; // last flag is used for major category, instead of subcategory #12 (2 * 6);
  111. array_of_all_categories[category_major_count].is_hidden = is_category_hidden(bit_characters, last_flag);
  112. //console.debug(array_of_all_categories[category_major_count].name + ' is hidden: '
  113. // + array_of_all_categories[category_major_count].is_hidden);
  114. bit_string_category_count += 1;
  115. if (bit_string_category_count > (categories_per_string - 1)) {
  116. // each of our custom bit arrays can only hold 8 categories' info. reset the counter and move on to the next bit array
  117. bit_string_category_count = 0;
  118. field_count += 1;
  119.  
  120. str_bit_array = str_bit_array_array[field_count]; // 3 custom fields with attributes
  121. // remove the first 4 characters (the unique ID plus a space)
  122. str_bit_array = str_bit_array.substring(unique_id_length); // 0-based, meaning start at character 5 (inclusive)
  123. }
  124. }
  125. return array_of_all_categories;
  126. }
  127.  
  128. /**
  129. * Returns true if the bit location is set to true.
  130. */
  131. function is_category_hidden(ascii_characters, minor_id) {
  132. var bit_shift_count = ((minor_id - 1) % bit_flags_per_char); // category 1 is stored in last bit flag; cat 2 in the second to last. cat 7 stored in last flag
  133. var bit_to_use = (Math.floor((minor_id - 1) / bit_flags_per_char)); // 1-6 (0-5) in first bit. 7-12 (6-11) stored in second. etc. 7/6 floored is 1.
  134. var bit_character = (ascii_characters.charCodeAt(bit_to_use)); // get binary representation
  135. var is_hidden = ((bit_character >>> bit_shift_count) & 000001); // shift the bits over and mask with '1'. if both are 1, it will return 1 (true) for hidden
  136. //console.log('is_hidden: ' + is_hidden);
  137. return is_hidden;
  138. }
  139.  
  140. function encode_category_hidden(ascii_characters, minor_id, is_hidden) {
  141. var bit_shift_count = ((minor_id - 1) % bit_flags_per_char); // category 1 is stored in last bit flag; cat 2 in the second to last. cat 7 stored in last flag
  142. var bit_to_use = (Math.floor((minor_id - 1) / bit_flags_per_char)); // 1-6 (0-5) in first bit. 7-12 (6-11) stored in second. etc. 7/6 floored is 1.
  143. var mask = ((is_hidden << bit_shift_count)); // move the mask to the proper location
  144.  
  145. var new_char = String.fromCharCode(ascii_characters[bit_to_use].charCodeAt(0) | mask);
  146. ascii_characters = ascii_characters.replaceAt(bit_to_use, new_char); // replace with new character
  147. //console.debug(ascii_characters);
  148. return ascii_characters;
  149. }
  150.  
  151. /**
  152. * Replaces the substituted characters with the 'illegal' characters.
  153. * This way, the script can use bitwise operations in a logical manner.
  154. */
  155. function translate_to_script(string_with_substituted_characters) {
  156. var str_return = string_with_substituted_characters.replace('?', String.fromCharCode(127)); // the delete char (127) is substituted with a question mark when saved at mint
  157. return str_return;
  158. }
  159.  
  160. /**
  161. * Replaces any illegal characters with substituted characters that mint.com allows in text fields.
  162. */
  163. function translate_to_mint(string_with_illegal_characters) {
  164. var str_return = string_with_illegal_characters.replace(String.fromCharCode(127), '?');
  165. return str_return;
  166. }
  167.  
  168. /**
  169. * Extracts the bit arrays from the saved custom category input box and puts all 3 into a string array.
  170. * Also hides the three fields, since the user probably shouldn't mess with them directly (and they look weird).
  171. * @returns {Array}
  172. */
  173. function extract_mint_array() {
  174. var str_bit_array_array = [];
  175. jQuery('#menu-category-' + category_id + ' ul li:contains("#!")').each(function () {
  176. str_bit_array_array.push(jQuery(this).text());
  177. //console.log('processing val is ' + jQuery(this).text());
  178. });
  179. return str_bit_array_array;
  180. }
  181.  
  182. function encode_bit_array(array_of_all_categories) {
  183. // loop through each category, create an ASCII character, and replace illegal characters
  184.  
  185. array_of_all_categories.sort(function (a, b) {
  186. return (a.id - b.id); // sort by id, lowest first
  187. });
  188.  
  189. var field_count = 0; // 0, 1, or 2; for the 3 unique fields holding the 23 category hidden attributes
  190. var bit_string_category_count = 0; // only 8 categories per string. once this goes past 7, reset and use next string.
  191. var str_bit_array_array = [];
  192.  
  193. // loop through each major category and its minor categories and mark them as hidden or not
  194. str_bit_array_array[field_count] = new Array(characters_per_category * categories_per_string + 1).join('@');
  195. for (var category_major_count = 0, category_major_length = array_of_all_categories.length; category_major_count < category_major_length; category_major_count++) {
  196.  
  197. var category_major = array_of_all_categories[category_major_count];
  198.  
  199. array_of_all_categories[category_major_count].categories_minor.sort(function (a, b) {
  200. return (a.id - b.id); // sort by id, lowest first
  201. });
  202.  
  203.  
  204. var bit_characters = new Array(characters_per_category + 1).join('@'); // the @ character is 01000000, so all flags (last 6) start 'off'.
  205. for (var category_minor_count = 0, category_minor_length = category_major.categories_minor.length; category_minor_count < category_minor_length; category_minor_count++) {
  206.  
  207. var minor_id = category_major.categories_minor[category_minor_count].id.slice(-2); // the last two digits are the minor category; the first two are always the same as the parent major category
  208. // if flag is '1', it is hidden
  209.  
  210. bit_characters = encode_category_hidden(bit_characters, minor_id, array_of_all_categories[category_major_count].categories_minor[category_minor_count].is_hidden);
  211. str_bit_array_array[field_count] = str_bit_array_array[field_count].replaceAt(bit_string_category_count * characters_per_category, bit_characters);
  212. }
  213. var last_flag = characters_per_category * bit_flags_per_char; // last flag is used for major category, instead of subcategory #12 (2 * 6);
  214. bit_characters = encode_category_hidden(bit_characters, last_flag, array_of_all_categories[category_major_count].is_hidden);
  215.  
  216. str_bit_array_array[field_count] = str_bit_array_array[field_count].replaceAt(bit_string_category_count * characters_per_category, bit_characters);
  217.  
  218. bit_string_category_count += 1;
  219. if (bit_string_category_count > (categories_per_string - 1)) {
  220. // each of our custom bit arrays can only hold 8 categories' info. reset the counter and move on to the next bit array
  221. bit_string_category_count = 0;
  222. field_count += 1;
  223. str_bit_array_array[field_count] = new Array(characters_per_category * categories_per_string + 1).join('@');
  224. }
  225. }
  226. // now that all 3 bit arrays are creates, tack on the unique id to the string
  227. for (var i = 0; i < number_of_bit_arrays; i++) {
  228. str_bit_array_array[i] = '#!' + (i + 1) + ' ' + str_bit_array_array[i];
  229. str_bit_array_array[i] = translate_to_mint(str_bit_array_array[i]);
  230. //console.log(str_bit_array_array[i]);
  231. }
  232. return str_bit_array_array;
  233. }
  234.  
  235.  
  236. /**
  237. * Will update or insert as needed.
  238. * @param bit_string
  239. */
  240. function upsert_field(bit_string) {
  241. var unique_id = bit_string.substr(0, unique_id_length);
  242. var input_id = jQuery('ul.popup-cc-L2-custom > li > input[value^="' + unique_id + '"]').prev().val();
  243. if (input_id) {
  244. update_field(bit_string);
  245. } else {
  246. insert_field(bit_string);
  247. }
  248. }
  249.  
  250. function insert_field(bit_string) {
  251. var hidden_token = JSON.parse(jQuery('#javascript-user').val()).token;
  252. var data = {
  253. pcatId: category_id,
  254. catId: 0,
  255. category: bit_string,
  256. task: 'C',
  257. token: hidden_token
  258. };
  259. jQuery.ajax(
  260. {
  261. type: "POST",
  262. url: '/updateCategory.xevent',
  263. data: data
  264. }
  265. );
  266. }
  267.  
  268. function update_field(bit_string) {
  269.  
  270. var hidden_token = JSON.parse(jQuery('#javascript-user').val()).token;
  271. var unique_id = bit_string.substr(0, unique_id_length);
  272.  
  273. var input = jQuery('ul.popup-cc-L2-custom > li > input[value^="' + unique_id + '"]');
  274. //console.log('setting input string from ' + input.val() + ' to ' + bit_string);
  275. input.val(bit_string); // set the value on the user's page manually (not needed for ajax, but needed for later processing)
  276. jQuery('#menu-category-' + category_id + ' ul li:contains("' + unique_id + '")').text(bit_string);
  277. /* input.prop('value',bit_string);
  278. input.attr('value',bit_string);*/
  279. var input_id = input.prev().val();
  280. var data = {
  281. pcatId: category_id,
  282. catId: input_id,
  283. category: bit_string,
  284. task: 'U',
  285. token: hidden_token
  286. };
  287. jQuery.ajax(
  288. {
  289. type: "POST",
  290. url: '/updateCategory.xevent',
  291. data: data
  292. }
  293. );
  294. }
  295.  
  296. function delete_field(bit_string) {
  297. var hidden_token = JSON.parse(jQuery('#javascript-user').val()).token;
  298. var unique_id = bit_string.substr(0, unique_id_length);
  299. var input_id = jQuery('ul.popup-cc-L2-custom > li > input[value^="' + unique_id + '"]').prev().val();
  300. var data = {
  301. catId: input_id,
  302. task: 'D',
  303. token: hidden_token
  304. };
  305. jQuery.ajax(
  306. {
  307. type: "POST",
  308. url: '/updateCategory.xevent',
  309. data: data
  310. }
  311. );
  312. }
  313.  
  314. /**
  315. * Loop through all the category objects. If any are hidden,
  316. * add the proper CSS to hide the field.
  317. * @param default_categories
  318. */
  319. function process_hidden_categories(default_categories) {
  320. for (var major_count = 0; major_count < default_categories.length; major_count++) {
  321. for (var minor_count = 0; minor_count < default_categories[major_count].categories_minor.length; minor_count++) {
  322. if (default_categories[major_count].categories_minor[minor_count].is_hidden) {
  323. jQuery('#menu-category-' + default_categories[major_count].categories_minor[minor_count].id).addClass(class_hidden);
  324. //console.log('hide minor ' + default_categories[major_count].categories_minor[minor_count].id);
  325. jQuery('#pop-categories-' + default_categories[major_count].categories_minor[minor_count].id).addClass(class_hidden);
  326. }
  327. }
  328. if (default_categories[major_count].is_hidden) {
  329. jQuery('#menu-category-' + default_categories[major_count].id).addClass(class_hidden);
  330. jQuery('#pop-categories-' + default_categories[major_count].id).addClass(class_hidden);
  331.  
  332. }
  333. }
  334. hide_show_category(hs_action_hide);
  335. }
  336.  
  337. /**
  338. *
  339. * @param action
  340. */
  341. function hide_show_category(action) {
  342. var category = jQuery('.' + class_hidden);
  343. if (action == hs_action_hide) {
  344. // hide the categories completely
  345. category.hide();
  346. category.css('text-decoration', 'line-through');
  347. }
  348. if (action == hs_action_show) {
  349. // remove any visible attributes
  350. category.show();
  351. category.css('text-decoration', '');
  352. }
  353. if (action == hs_action_edit) {
  354. category.show();
  355. category.css('text-decoration', 'line-through');
  356. }
  357. }
  358.  
  359. function add_toggle() {
  360. if (!(jQuery('#sgs-toggle').length)) {
  361. var toggle_style = "position: absolute; right: 30px; top: 35px; cursor: pointer";
  362. var toggle_text = "Edit Hidden Categories";
  363. jQuery('#pop-categories-main').prepend('<div id="sgs-toggle" class="" style="' + toggle_style + '">' + toggle_text + '</div>');
  364. jQuery('#sgs-toggle').click(function () {
  365. edit_categories();
  366. });
  367. jQuery('#pop-categories-submit').click(function(){
  368. jQuery('#sgs-toggle').addClass('editing'); // force the current mode to be editing so the edit_categories call saves
  369. edit_categories(); // if user clicks the "I'm done" button, we should also save the categories.
  370. })
  371. }
  372. }
  373.  
  374. function edit_categories() {
  375. var toggle = jQuery('#sgs-toggle');
  376. toggle.toggleClass('editing');
  377. if (toggle.hasClass('editing')) {
  378. toggle.text('Save Hidden Categories');
  379. mint_edit(true); // make all categories clickable; when clicked, add class and strike out
  380. } else {
  381. mint_edit(false); // remove clickable event and strike css; go back to hiding
  382. mint_save();
  383. toggle.text('Edit Hidden Categories');
  384.  
  385. }
  386.  
  387. }
  388.  
  389. function mint_edit(edit_mode) {
  390. if (edit_mode) {
  391. // get the major and minor categories in the popup editor
  392. var minor_categories = jQuery('div.popup-cc-L2 > ul:first-of-type > li'); // second ul is custom categories, so just get first
  393. var major_categories = jQuery('#popup-cc-L1').find('.isL1');
  394.  
  395.  
  396. // display all previously hidden fields (except our three custom fields holding bit arrays)
  397.  
  398.  
  399. // add checkboxes to the categories
  400. minor_categories.each(function () {
  401. add_checkbox(this);
  402. });
  403. major_categories.each(function () {
  404. add_checkbox(this);
  405. })
  406. jQuery('input.hide_show_checkbox').css({
  407. 'position': 'absolute',
  408. 'right': '-18px'
  409. });
  410.  
  411. // add label for minor checkboxes
  412. jQuery('div.popup-cc-L2 > h3:first-of-type').append('<span class="' + class_edit_mode + ' minor_hide_show_label">Hide</span>');
  413. jQuery('span.minor_hide_show_label').css({
  414. 'position': 'absolute',
  415. 'right': '64px',
  416. 'top': '3px',
  417. 'font-size': '13px',
  418. 'font-weight': 'bold'
  419. });
  420.  
  421.  
  422. // add label for major categories
  423. jQuery('#pop-categories-form fieldset').prepend('<span class="' + class_edit_mode + ' major_hide_show_label">Hide</span>');
  424. jQuery('span.major_hide_show_label').css({
  425. 'position': 'absolute',
  426. 'left': '267px',
  427. 'font-size': '13px',
  428. 'font-weight': 'bold'
  429. });
  430. // add checkbox event. when checked add the 'hidden' class (which is scanned on save)
  431. jQuery('input.hide_show_checkbox').click(function () {
  432. parent_id = jQuery(this).parent().attr('id').replace(/\D/g, '');
  433. if (jQuery(this).is(':checked')) {
  434. jQuery('#menu-category-' + parent_id).addClass(class_hidden);
  435. jQuery('#pop-categories-' + parent_id).addClass(class_hidden);
  436. jQuery(this).parent().css('text-decoration', 'line-through');
  437. } else {
  438. jQuery('#menu-category-' + parent_id).removeClass(class_hidden);
  439. jQuery('#pop-categories-' + parent_id).removeClass(class_hidden);
  440. ;
  441. jQuery(this).parent().css('text-decoration', '');
  442.  
  443. }
  444. });
  445. hide_show_category(hs_action_edit);
  446. } else {
  447. // no longer editing (saving), so remove our labels and checkboxes, and re-hide the desired categories
  448. jQuery('.' + class_edit_mode).remove();
  449. hide_show_category(hs_action_hide);
  450. }
  451. }
  452.  
  453. function add_checkbox(element) {
  454. var checked = '';
  455. if (jQuery(element).hasClass(class_hidden)) {
  456. // hidden categories are checked
  457. checked = 'checked="checked"';
  458. }
  459. jQuery(element).append('<input type="checkbox" class="' + class_edit_mode + ' hide_show_checkbox"' + checked + ' />');
  460. }
  461.  
  462. /**
  463. * hooks to the save or cancel button so that hidden categories will be re-hidden after ajax refresh
  464. */
  465. function add_save_hook() {
  466. if ((jQuery('#pop-categories-submit').length && (!(jQuery('#pop-categories-submit').hasClass('sgs-hook-added'))))) {
  467.  
  468. jQuery('#pop-categories-submit').addClass('sgs-hook-added');
  469. jQuery('#pop-categories-submit, #pop-categories-close').click(function () {
  470. mint_refresh();
  471. });
  472.  
  473. }
  474. }
  475.  
  476. /**
  477. * Hides our bit array custom categories permanently so the user won't accidentally mess with them.
  478. */
  479. function hide_bit_array() {
  480. jQuery('input[value^="#!"]').parent().hide();
  481. jQuery('li[id^="menu-category-"] a:contains("#!")').parent().hide();
  482.  
  483. }
  484.  
  485. /**
  486. * Allows immutable strings to have character(s) replaced
  487. * @param index
  488. * @param character
  489. * @returns {string}
  490. */
  491. String.prototype.replaceAt = function (index, character) {
  492. return this.substr(0, index) + character + this.substr(index + character.length);
  493. };
  494.  
  495. /**
  496. * Lets you bind an event and have it run first.
  497. * @param name
  498. * @param fn
  499. */
  500. jQuery.fn.bindFirst = function (name, fn) {
  501. // bind as you normally would
  502. // don't want to miss out on any jQuery magic
  503. this.on(name, fn);
  504.  
  505. // Thanks to a comment by @Martin, adding support for
  506. // namespaced events too.
  507. this.each(function () {
  508. var handlers = jQuery._data(this, 'events')[name.split('.')[0]];
  509. // take out the handler we just inserted from the end
  510. var handler = handlers.pop();
  511. // move it at the beginning
  512. handlers.splice(0, 0, handler);
  513. });
  514. };
  515.  
  516. function mint_refresh() {
  517. add_toggle();
  518. add_save_hook();
  519. // when the popup is opened or closed, re-hide the categories
  520. var str_bit_array_array = extract_mint_array();
  521. var default_categories = get_default_category_list();
  522. default_categories = decode_bit_array(str_bit_array_array, default_categories);
  523. process_hidden_categories(default_categories); // hides the appropriate fields
  524. hide_bit_array(); // comment this out in order to see the bit string data
  525. }
  526.  
  527. /**
  528. * Saves the preferences
  529. */
  530. function mint_save() {
  531. //console.debug('saving');
  532. var default_categories = get_default_category_list();
  533. var bit_array_array = encode_bit_array(default_categories);
  534. for (var i = 0; i < bit_array_array.length; i++) {
  535. upsert_field(bit_array_array[i]);
  536. }
  537. }
  538. function add_dropdown_hook() {
  539. //console.log('start dropdown');
  540. //if ((jQuery('#txnEdit-category_input').length) && (!(jQuery('#txtEdit-category_input').hasClass('sgs-hook-added')))) {
  541. jQuery('#txnEdit-category_input').addClass('sgs-hook-added');
  542. jQuery('#txnEdit-category_input, #txnEdit-category_picker').off('click', mint_refresh);
  543. jQuery('#txnEdit-category_input, #txnEdit-category_picker').on('click', mint_refresh);
  544. //}
  545. }
  546. function google_search_fix(){
  547. jQuery('#txnEdit-toggle').on('click', function(){
  548. // get the text; don't try decoding the partial original search link
  549. plain_search = jQuery('a.desc_link strong var').text();
  550.  
  551. // proper encoding
  552. new_search = encodeURIComponent(plain_search);
  553.  
  554. // encoding changes spaces to %20, but that is deprecated now. urls take '+' instead.
  555. new_search = new_search.replace(/%20/gi, '+');
  556.  
  557. // replace old url with new
  558. jQuery('a.desc_link').attr('href', 'https://www.google.com/#q=' + new_search);
  559. });
  560. }
  561. ////// Start jQuery Addon
  562. if (!(jQuery.fn.arrive)){
  563. /*
  564. * arrive.js
  565. * v2.1.0
  566. * https://github.com/uzairfarooq/arrive
  567. * MIT licensed
  568. *
  569. * Copyright (c) 2014-2015 Uzair Farooq
  570. */
  571. var _arrive_unique_id_=0;
  572. (function(n,u,p){function h(a){return a._shouldBeIgnored===p?-1!=(" "+a.className+" ").indexOf(" ignore-arrive ")?a._shouldBeIgnored=!0:null==a.parentNode?a._shouldBeIgnored=!1:a._shouldBeIgnored=h(a.parentNode):a._shouldBeIgnored}function q(a,c,e){for(var d=0,b;b=a[d];d++)h(b)||(f.matchesSelector(b,c.selector)&&(b._id===p&&(b._id=_arrive_unique_id_++),-1==c.firedElems.indexOf(b._id)&&(c.firedElems.push(b._id),e.push({callback:c.callback,elem:b}))),0<b.childNodes.length&&q(b.childNodes,c,e))}function v(a){for(var c=
  573. 0,e;e=a[c];c++)e.callback.call(e.elem)}function x(a,c){a.forEach(function(a){if(!h(a.target)){var d=a.addedNodes,b=a.target,r=[];null!==d&&0<d.length?q(d,c,r):"attributes"===a.type&&f.matchesSelector(b,c.selector)&&(b._id===p&&(b._id=_arrive_unique_id_++),-1==c.firedElems.indexOf(b._id)&&(c.firedElems.push(b._id),r.push({callback:c.callback,elem:b})));v(r)}})}function y(a,c){a.forEach(function(a){if(!h(a.target)){a=a.removedNodes;var d=[];null!==a&&0<a.length&&q(a,c,d);v(d)}})}function z(a){var c=
  574. {attributes:!1,childList:!0,subtree:!0};a.fireOnAttributesModification&&(c.attributes=!0);return c}function A(a){return{childList:!0,subtree:!0}}function g(a){a.arrive=k.bindEvent;f.addMethod(a,"unbindArrive",k.unbindEvent);f.addMethod(a,"unbindArrive",k.unbindEventWithSelectorOrCallback);f.addMethod(a,"unbindArrive",k.unbindEventWithSelectorAndCallback);a.leave=l.bindEvent;f.addMethod(a,"unbindLeave",l.unbindEvent);f.addMethod(a,"unbindLeave",l.unbindEventWithSelectorOrCallback);f.addMethod(a,"unbindLeave",
  575. l.unbindEventWithSelectorAndCallback)}if(n.MutationObserver&&"undefined"!==typeof HTMLElement){var f=function(){var a=HTMLElement.prototype.matches||HTMLElement.prototype.webkitMatchesSelector||HTMLElement.prototype.mozMatchesSelector||HTMLElement.prototype.msMatchesSelector;return{matchesSelector:function(c,e){return c instanceof HTMLElement&&a.call(c,e)},addMethod:function(a,e,d){var b=a[e];a[e]=function(){if(d.length==arguments.length)return d.apply(this,arguments);if("function"==typeof b)return b.apply(this,
  576. arguments)}}}}(),B=function(){var a=function(){this._eventsBucket=[];this._beforeRemoving=this._beforeAdding=null};a.prototype.addEvent=function(a,e,d,b){a={target:a,selector:e,options:d,callback:b,firedElems:[]};this._beforeAdding&&this._beforeAdding(a);this._eventsBucket.push(a);return a};a.prototype.removeEvent=function(a){for(var e=this._eventsBucket.length-1,d;d=this._eventsBucket[e];e--)a(d)&&(this._beforeRemoving&&this._beforeRemoving(d),this._eventsBucket.splice(e,1))};a.prototype.beforeAdding=
  577. function(a){this._beforeAdding=a};a.prototype.beforeRemoving=function(a){this._beforeRemoving=a};return a}(),w=function(a,c,e){function d(a){"number"!==typeof a.length&&(a=[a]);return a}var b=new B;b.beforeAdding(function(b){var c=b.target,d;if(c===n.document||c===n)c=document.getElementsByTagName("html")[0];d=new MutationObserver(function(a){e.call(this,a,b)});var m=a(b.options);d.observe(c,m);b.observer=d});b.beforeRemoving(function(a){a.observer.disconnect()});this.bindEvent=function(a,e,t){"undefined"===
  578. typeof t&&(t=e,e=c);for(var m=d(this),f=0;f<m.length;f++)b.addEvent(m[f],a,e,t)};this.unbindEvent=function(){var a=d(this);b.removeEvent(function(b){for(var c=0;c<a.length;c++)if(b.target===a[c])return!0;return!1})};this.unbindEventWithSelectorOrCallback=function(a){var c=d(this);b.removeEvent("function"===typeof a?function(b){for(var d=0;d<c.length;d++)if(b.target===c[d]&&b.callback===a)return!0;return!1}:function(b){for(var d=0;d<c.length;d++)if(b.target===c[d]&&b.selector===a)return!0;return!1})};
  579. this.unbindEventWithSelectorAndCallback=function(a,c){var e=d(this);b.removeEvent(function(b){for(var d=0;d<e.length;d++)if(b.target===e[d]&&b.selector===a&&b.callback===c)return!0;return!1})};return this},k=new w(z,{fireOnAttributesModification:!1},x),l=new w(A,{},y);u&&g(u.fn);g(HTMLElement.prototype);g(NodeList.prototype);g(HTMLCollection.prototype);g(HTMLDocument.prototype);g(Window.prototype)}})(this,"undefined"===typeof jQuery?null:jQuery);
  580.  
  581. }
  582. ////// End jQuery Addon
  583. jQuery(document).arrive("#txnEdit-category_input", add_dropdown_hook);
  584. jQuery(document).arrive("#txnEdit-toggle", google_search_fix);
  585.  
  586. }
  587.  
  588.  
  589. /**
  590. * Mint.com loads jquery after page is loaded, and it conflicts with other verions. We can't
  591. * use a sandbox for our script, either, since we must use their version of jquery in order
  592. * to hook into their ajax completion events.
  593. * Therefore, we must manually check for jquery every so often (50 ms) until it finally exists.
  594. * Then, we can call our jquery-requiring function and modify the page.
  595. * @param method
  596. */
  597. function defer(method) {
  598. if (window.jQuery) {
  599. method();
  600. } else {
  601. setTimeout(function () {
  602. defer(method)
  603. }, 50);
  604. }
  605. }
  606. window.addEventListener('load', function () {
  607. defer(after_jquery);
  608. });
  609. }());