FV - Stud selector

Better dropdown and checkbox selection for Animal Husbandry

  1. // ==UserScript==
  2. // @name FV - Stud selector
  3. // @version 0.3.1
  4. // @description Better dropdown and checkbox selection for Animal Husbandry
  5. // @author msjanny (#7302)
  6. // @match https://www.furvilla.com/career/breeder/*
  7. // @match https://www.furvilla.com/career/stables/*
  8. // @match https://www.furvilla.com/career/forest/*
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @namespace https://greasyfork.org/users/319295
  12. // ==/UserScript==
  13.  
  14. var stud = GM_getValue('stud', '');
  15. var findfem = GM_getValue('findfem', false);
  16. var lastChecked = null;
  17.  
  18. (function () {
  19. 'use strict';
  20. /* globals $:false */
  21.  
  22. function loadScripts(func) {
  23. //check every 300ms if modal has loaded before continuing
  24. if ($('#modal').prop('style').display != 'block' || $('#modal').find('img[src="/img/loading.gif"]').length) {
  25. setTimeout(function () {
  26. loadScripts(func);
  27. }, 300);
  28. } else func();
  29. }
  30.  
  31. function selectStud() {
  32. var studbtn = $(
  33. '<a class="btn" style="margin-left: 20px">Remember Stud</a>'
  34. );
  35. studbtn.click(function () {
  36. stud = $('.stable-select select[name=male_id]').val();
  37. GM_setValue('stud', stud);
  38. });
  39. $("h2:contains('Male')").append(studbtn);
  40.  
  41. var findfem_cb = $(
  42. '<input type="checkbox" class="form-check-input" style="height: 24px; width: 24px; margin-left: 20px">'
  43. );
  44. if (findfem) {
  45. findfem_cb.prop('checked', true);
  46. }
  47. findfem_cb.change(function () {
  48. findfem = !findfem;
  49. GM_setValue('findfem', findfem);
  50. if (findfem) findFem();
  51. });
  52. $("h2:contains('Female')").append(findfem_cb);
  53.  
  54. if (stud) {
  55. $('.stable-select select[name=male_id]').val(stud);
  56. var option = $(`.stable-select select option[value=${stud}]`);
  57. if (option.length) {
  58. updateStablePreview(option);
  59. if (findfem) findFem();
  60. }
  61. }
  62. }
  63. function findFem() {
  64. var species = $('.stable-select select[name=male_id] option:selected')
  65. .text()
  66. .match(/\b[a-zA-Z\-]+$/g);
  67. var females = $(
  68. `.stable-select select[name=female_id] option:contains(${species})`
  69. );
  70. if (!females.length) {
  71. return;
  72. }
  73. var female = females.eq(0);
  74. updateStablePreview(female);
  75. $('.stable-select select[name=female_id]').val(female.attr('value'));
  76. }
  77.  
  78. function imAnimals() {
  79. $('.modal .pagination a').click(function () {
  80. loadScripts(imAnimals);
  81. });
  82. checkAll();
  83. checkRTB();
  84. }
  85. function imStables() {
  86. $('.modal .pagination a').click(function () {
  87. loadScripts(imStables);
  88. });
  89. checkAll();
  90. }
  91. function forest() {
  92. $('.modal .pagination a').click(function () {
  93. loadScripts(forest);
  94. });
  95.  
  96. // ctrl+select
  97. $('#modal .css-checkbox').click(function (e) {
  98. if (e.ctrlKey || e.shiftKey) {
  99. var from = $('#modal .css-checkbox').index(this);
  100. var to = $('#modal .css-checkbox').index(lastChecked);
  101. if (to < 0) to = from;
  102.  
  103. var start = Math.min(from, to) + 1;
  104. var end = Math.max(from, to) + 1;
  105. if (from < to) end--;
  106.  
  107. $('#modal .css-checkbox')
  108. .slice(start, end)
  109. .click();
  110. }
  111. lastChecked = $(this);
  112. });
  113.  
  114. // check common males
  115. var RTBbtn = $(
  116. '<a class="pull-right label label-primary" style="margin-right: 10px;"><i class="fas fa-mars"></i> Check Common Males</a>'
  117. );
  118. RTBbtn.click(function () {
  119. $('.modal .inventory-table tr').each(function () {
  120. var txt = $(this).find('td:nth-child(2)').text();
  121. if (
  122. txt.includes('(Male)') &&
  123. $(this).find('br + span > .label-default').length
  124. ) {
  125. $(this).find('.css-checkbox').click();
  126. }
  127. });
  128. });
  129. $('.modal-body .serpent-all').after(RTBbtn);
  130. $('.modal-body .serpent-all').after(document.createTextNode('Use ctrl + click to select range.'));
  131. }
  132.  
  133. function checkAll() {
  134. // create check all button
  135. var cbbtn = $(
  136. '<a class="pull-right label label-primary" style="margin-bottom: 15px;"><i class="fa fa-check-square-o"></i> Check All</a>'
  137. );
  138. cbbtn.click(function () {
  139. $('#modal .css-checkbox').click();
  140. });
  141. $('.modal-body > .clearfix').prepend(cbbtn);
  142. $('.modal-body > .clearfix').prepend(document.createTextNode('Use ctrl + click to select range.'));
  143.  
  144. // ctrl+select
  145. $('#modal .css-checkbox').click(function (e) {
  146. if (e.ctrlKey || e.shiftKey) {
  147. var from = $('#modal .css-checkbox').index(this);
  148. var to = $('#modal .css-checkbox').index(lastChecked);
  149. if (to < 0) to = from;
  150.  
  151. var start = Math.min(from, to) + 1;
  152. var end = Math.max(from, to) + 1;
  153. if (from < to) end--;
  154.  
  155. $('#modal .css-checkbox')
  156. .slice(start, end)
  157. .click();
  158. }
  159. lastChecked = $(this);
  160. });
  161. }
  162. function checkRTB() {
  163. // create check all button
  164. var RTBbtn = $(
  165. '<a class="pull-right label label-primary" style="margin-right: 10px; margin-bottom: 15px;"><i class="fas fa-venus"></i> Check RTB</a>'
  166. );
  167. RTBbtn.click(function () {
  168. $('.modal .inventory-table tr').each(function () {
  169. var txt = $(this).find('td:nth-child(2)').text();
  170. if (txt.includes('(Female)') && txt.includes('Breedable Now')) {
  171. $(this).find('.css-checkbox').click();
  172. }
  173. });
  174. });
  175. $('.modal-body .inventory-table').before(RTBbtn);
  176. }
  177.  
  178. function stables() {
  179. // ctrl+select
  180. $('.css-checkbox').click(function (e) {
  181. if (e.ctrlKey || e.shiftKey) {
  182. // get the column
  183. var cbclass = $(this).prop('class').includes("export-animal-input") ? ".export-animal-input" : ".export-stable-input";
  184.  
  185. var from = $(cbclass).index(this);
  186. var to = $(cbclass).index(lastChecked);
  187. if (to < 0) to = from;
  188.  
  189. var start = Math.min(from, to) + 1;
  190. var end = Math.max(from, to) + 1;
  191. if (from < to) end--;
  192.  
  193. $(cbclass)
  194. .slice(start, end)
  195. .click();
  196. }
  197. lastChecked = $(this);
  198. });
  199.  
  200. $('h1 + p').text($('h1 + p').text() + ' Use ctrl + click to select range.');
  201.  
  202. // select all buttons
  203. var tr = $('<tr> <td colspan="2" class="text-right"> Select All </td> <td align="center" class="tooltipster tooltipstered"> <a class="label label-primary all-stables" data-action=""><i class="fa fa-check-square-o"></i></a> </td> <td align="center" class="tooltipster tooltipstered"> <a class="label label-primary all-animals" data-action="storage"><i class="fa fa-check-square-o"></i></a> </td> </tr>');
  204. $('.stables-list tbody').append(tr);
  205. $('.all-animals').click(function () {
  206. $('.export-animal-input').prop('checked', true);
  207. });
  208. $('.all-stables').click(function () {
  209. $('.export-stable-input').prop('checked', true);
  210. });
  211. }
  212.  
  213. $(document).ready(function () {
  214. var url = window.location.href;
  215. if (url.includes('breeder')) {
  216. $('.breed-a').click(function () {
  217. loadScripts(selectStud);
  218. });
  219. $('.import-animals-btn').click(function () {
  220. loadScripts(imAnimals);
  221. });
  222. $('.import-stables-btn').click(function () {
  223. loadScripts(imStables);
  224. });
  225. } else if (url.includes('stables')) {
  226. stables();
  227. } else if (url.includes('forest')) {
  228. $('.import-animals-btn').click(function () {
  229. loadScripts(forest);
  230. });
  231. }
  232. });
  233. })();