TM save COs

TM save COs (The author does not guarantee the correctness of the script)

  1. // ==UserScript==
  2. // @name TM save COs
  3. // @namespace ByMLFC
  4. // @version 1.0.1
  5. // @description TM save COs (The author does not guarantee the correctness of the script)
  6. // @include http://trophymanager.com/tactics
  7. // @include http://*.trophymanager.com/tactics
  8. // @include http://trophymanager.com/tactics/*
  9. // @include http://*.trophymanager.com/tactics/*
  10. // ==/UserScript==
  11.  
  12. // page scope scripts
  13. var pageHead = document.getElementsByTagName("head")[0];
  14. var script = document.createElement('script');
  15. script.type = "text/javascript";
  16.  
  17. function embed() {
  18. // get CO from the page
  19. function getCO() {
  20. document.getElementById("CO_JSON").value = JSON.stringify(cond_orders);
  21. }
  22.  
  23. // if two orders are the same, return true, exclude id
  24. function compare_cond_order(cond_order1, cond_order2) {
  25. for (var j in cond_order1) {
  26. if (j != "ID") {
  27. if (cond_order1[j] != cond_order2[j]) {
  28. return false;
  29. }
  30. }
  31. }
  32. return true;
  33. }
  34.  
  35. function setCO() {
  36. var CO_name = document.getElementById("CO_list");
  37. if (CO_name.selectedIndex != 0) {
  38. var temp_cond_orders = JSON.parse(document.getElementById("CO_JSON").value);
  39.  
  40. $("#cond_orders_list").html("");
  41. for(var i in temp_cond_orders) {
  42. // skip save if COs are the same
  43. if (compare_cond_order(cond_orders[i], temp_cond_orders[i])) {
  44. var $co = co_create_cond_order(temp_cond_orders[i], true);
  45. } else {
  46. cond_orders[i] = temp_cond_orders[i];
  47. var $co = co_create_cond_order(temp_cond_orders[i], false);
  48. }
  49. $("#cond_orders_list").append($co);
  50. }
  51. } else {
  52. alert("Please select a CO-set");
  53. }
  54. }
  55.  
  56. // receive COs from the page or write COs from Monkey
  57. var input = document.createElement("input");
  58. input.id = "CO_JSON";
  59. input.type = "hidden";
  60. document.getElementsByTagName("body")[0].appendChild(input);
  61.  
  62. // prepare forms to call page scope scripts
  63. var form = document.createElement("form");
  64. form.id = "call_setCO";
  65. form.setAttribute("onreset", "setCO()");
  66. document.getElementsByTagName("body")[0].appendChild(form);
  67.  
  68. var form = document.createElement("form");
  69. form.id = "call_getCO";
  70. form.setAttribute("onreset", "getCO()");
  71. document.getElementsByTagName("body")[0].appendChild(form);
  72. }
  73.  
  74. var txtScr = embed.toString();
  75. txtScr = txtScr.substring(txtScr.indexOf("{")+1, txtScr.lastIndexOf("}"));
  76. script.appendChild(document.createTextNode(txtScr));
  77.  
  78. pageHead.appendChild(script);
  79.  
  80. // run in Monkey
  81. // check availability of the name and assign one if the name has been in use
  82. function name_check(original, temp_name, count) {
  83. if (temp_name.length == 0) {
  84. temp_name = name_check("Set", "Set_1", 2);
  85. } else {
  86. if (GM_getValue("co"+temp_name, false)) {
  87. temp_name = name_check(original, original+"_"+count, count+1);
  88. }
  89. }
  90. return temp_name;
  91. }
  92.  
  93. // save CO to Monkey
  94. function save_COs() {
  95. var CO_JSON = document.getElementById("CO_JSON").value;
  96. var CO_name = document.getElementById("CO_list");
  97. if (CO_name.selectedIndex != 0) {
  98. CO_name = CO_name.options[CO_name.selectedIndex].value;
  99. // confirm the user is going to overwrite the save
  100. if ((document.getElementById("CO_name").value == CO_name) || (document.getElementById("CO_name").value.length == 0)) {
  101. GM_setValue("co" + CO_name, CO_JSON);
  102. } else {
  103. alert("Not sure about your intention:\n1) To rename, press the rename button;\n2) To overwrite, leave the name box blank or type the name as your chosen save;\n3) To add a new set, choose from the dropdown list");
  104. }
  105. } else {
  106. CO_name = name_check(document.getElementById("CO_name").value, document.getElementById("CO_name").value, 1);
  107.  
  108. // add to dropdown list
  109. var option = document.createElement("option");
  110. option.value = CO_name;
  111. option.innerHTML = CO_name;
  112. document.getElementById("CO_list").appendChild(option);
  113.  
  114. document.getElementById("CO_list").selectedIndex = document.getElementById("CO_list").length - 1;
  115. GM_setValue("co" + CO_name, CO_JSON);
  116. };
  117. }
  118.  
  119. // load CO from Monkey
  120. function load_COs() {
  121. var CO_name = document.getElementById("CO_list");
  122. if (CO_name.selectedIndex != 0) {
  123. CO_name = CO_name.options[CO_name.selectedIndex].value;
  124. document.getElementById("CO_JSON").value = GM_getValue("co" + CO_name);
  125. }
  126. }
  127.  
  128. // delete CO from Monkey
  129. function delete_COs() {
  130. var CO_list = document.getElementById("CO_list");
  131. if (CO_list.selectedIndex != 0) {
  132. GM_deleteValue("co" + CO_list.options[CO_list.selectedIndex].value);
  133. CO_list.remove(CO_list.options[CO_list.selectedIndex]);
  134. }
  135. }
  136.  
  137. // rename CO from Monkey
  138. function rename_COs() {
  139. if (document.getElementById("CO_list").selectedIndex != 0) {
  140. load_COs();
  141. delete_COs();
  142. document.getElementById("CO_list").selectedIndex = 0;
  143. save_COs();
  144. }
  145. }
  146.  
  147. // call page scope scripts
  148. function call_setCO() {
  149. document.getElementById("call_setCO").reset();
  150. }
  151.  
  152. function call_getCO() {
  153. document.getElementById("call_getCO").reset();
  154. }
  155.  
  156. function save_onclick() {
  157. call_getCO();
  158. save_COs();
  159. }
  160.  
  161. function load_onclick() {
  162. load_COs();
  163. call_setCO();
  164. }
  165.  
  166. // create CO-set list
  167. document.getElementById("tactics").style.height = "510px";
  168.  
  169. var div = document.createElement("div");
  170. div.setAttribute("style", "position: absolute; top: 472px; left: 10px;");
  171.  
  172. // CO list
  173. var span = document.createElement("span");
  174. span.setAttribute("style", "padding-right: 10px;");
  175. var select = document.createElement("select");
  176. select.setAttribute("style", "width: 250px;");
  177. select.id = "CO_list";
  178. select.className = "ui-selectmenu ui-state-default ui-selectmenu-popup";
  179. var option = document.createElement("option");
  180. option.innerHTML = "Add a New CO-set";
  181. select.appendChild(option);
  182. span.appendChild(select);
  183.  
  184. div.appendChild(span);
  185. document.getElementById("tactics").appendChild(div);
  186.  
  187. // name
  188. var span = document.createElement("span");
  189. span.setAttribute("style", "padding-right: 10px;");
  190. var input = document.createElement("input");
  191. input.setAttribute("style", "width: 200px;");
  192. input.id = "CO_name";
  193. input.className = "embossed";
  194. input.type = "text";
  195. input.setAttribute("placeholder", "Name or Rename");
  196. span.appendChild(input);
  197. div.appendChild(span);
  198.  
  199. var GM_value_list = GM_listValues();
  200. for (var i in GM_value_list) {
  201. var key = GM_value_list[i];
  202. if (key.indexOf("co") == 0) {
  203. key = key.substring(2);
  204. var option = document.createElement("option");
  205. option.value = key;
  206. option.innerHTML = key;
  207. document.getElementById("CO_list").appendChild(option);
  208. }
  209. }
  210.  
  211. // save button
  212. var span3 = document.createElement("span");
  213. span3.setAttribute("style", "padding-right: 10px;");
  214. var span = document.createElement("span");
  215. span.className = "button";
  216. span.addEventListener("click", save_onclick, false);
  217.  
  218. var span2 = document.createElement("span");
  219. span2.className = "button_border";
  220. span2.innerHTML = "Save COs";
  221. span.appendChild(span2);
  222.  
  223. span3.appendChild(span);
  224. div.appendChild(span3);
  225.  
  226. // rename button
  227. var span3 = document.createElement("span");
  228. span3.setAttribute("style", "padding-right: 10px;");
  229. var span = document.createElement("span");
  230. span.className = "button";
  231. span.addEventListener("click", rename_COs, false);
  232.  
  233. var span2 = document.createElement("span");
  234. span2.className = "button_border";
  235. span2.innerHTML = "Rename CO-set";
  236. span.appendChild(span2);
  237.  
  238. span3.appendChild(span);
  239. div.appendChild(span3);
  240.  
  241. // load button
  242. var span3 = document.createElement("span");
  243. span3.setAttribute("style", "padding-right: 10px;");
  244. var span = document.createElement("span");
  245. span.className = "button";
  246. span.addEventListener("click", load_onclick, false);
  247.  
  248. var span2 = document.createElement("span");
  249. span2.className = "button_border";
  250. span2.innerHTML = "Load COs";
  251. span.appendChild(span2);
  252.  
  253. span3.appendChild(span);
  254. div.appendChild(span3);
  255.  
  256. // delete button
  257. var span3 = document.createElement("span");
  258. span3.setAttribute("style", "padding-right: 10px;");
  259. var span = document.createElement("span");
  260. span.className = "button";
  261. span.addEventListener("click", delete_COs, false);
  262.  
  263. var span2 = document.createElement("span");
  264. span2.className = "button_border";
  265. span2.innerHTML = "Delete CO-set";
  266. span.appendChild(span2);
  267.  
  268. span3.appendChild(span);
  269. div.appendChild(span3);