wod item sorter

sort items in inventory

当前为 2015-03-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name wod item sorter
  3. // @namespace org.holer.webgame.util.wod
  4. // @version 0.1.22
  5. // @description sort items in inventory
  6. // @include http://*.world-of-dungeons.org/wod/spiel/hero/items.php*
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
  8. // @grant none
  9. // @copyright 2012+, Russell
  10. // ==/UserScript==
  11.  
  12.  
  13. function main() {
  14. window.wisMsgg = {
  15. applySortRule: "apply sort rules",
  16. autoSort: "auto sort",
  17. append: "append",
  18. exception: "exception",
  19. deleteStr: "delete",
  20. generateRule: "generate rule",
  21. saveRule: "save rule",
  22. loadRule: "load rule",
  23. noLocalStorageSupport: "browser do not support localStorage, can't save settings"
  24. };
  25.  
  26. window.wisMsg = {
  27. applySortRule: "整理",
  28. autoSort: "自动整理",
  29. append: "增加规则",
  30. exception: "增加子规则",
  31. deleteStr: "删除",
  32. generateRule: "生成规则",
  33. saveRule: "保存规则",
  34. loadRule: "加载已保存的规则",
  35. noLocalStorageSupport: "浏览器不支持localStorage,无法保存设置。"
  36. };
  37.  
  38.  
  39. window.statsHtml = '<div class=item_sort_stats></div>';
  40. window.taHtml = '<textarea id="wiscj" style="width:100%;height:5em;"></textarea>';
  41. window.uiHtml = '<hr><div id="wisc" class="gadget_body">'+taHtml+'</div>';
  42. window.btnsHtml = '<div><button id="wisawrb" onclick="applyWisRule()" class="button">'+wisMsg.applySortRule+'</button></div>';
  43. window.eolHtml = '<ol></ol>';
  44.  
  45.  
  46.  
  47. window.counts = {
  48. go_lager : 0,
  49. go_group_2 : 0,
  50. go_group : 0,
  51. go_keller : 0,
  52. npc : 0
  53. };
  54.  
  55.  
  56. window.out_alias = {
  57. "仓库":"go_lager",
  58. "go_lager":"go_lager",
  59. "贮藏室":"go_keller",
  60. "go_keller":"go_keller",
  61. "团队仓库":"go_group_2",
  62. "go_group_2":"go_group_2",
  63. "宝库":"go_group",
  64. "go_group":"go_group",
  65. "npc":"npc",
  66. };
  67.  
  68.  
  69. window.ruleObj = null;
  70.  
  71. window.strToRegexI = function () {
  72. ruleObj = jQuery.parseJSON(ruleObj);
  73. if (ruleObj.regex) return;
  74. strToRegex(ruleObj.rules);
  75. ruleObj.regex = true;
  76. };
  77.  
  78.  
  79. RegExp.escape = function(text) {
  80. return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  81. };
  82.  
  83. // add r:regular exp pair to the dictionary l
  84. window.strToRegex = function (l){
  85. for (var i =0; i<l.length;i++){
  86. //var c;
  87. //c = l[i];
  88. // 1 :
  89. // 2 : uses left
  90. // 3 :
  91. l[i].r = new RegExp("^\n*?"+RegExp.escape(l[i].n) +"!?\n*? *?\n*? *?(\\(([\\d]+)\\/([\\d]+)\\))?\n*?$");
  92. }
  93. };
  94.  
  95. // convert textarea (in JSON) into dictionary
  96. // and add regular expression obj
  97. window.wisLoadRule = function () {
  98. var rule = $("#wiscj").val();
  99.  
  100. ruleObj = rule;
  101. strToRegexI();
  102. };
  103.  
  104. window.getOperation = function (name, list){
  105. var c;
  106. for(var i=0;i<list.length;i++){
  107. c = list[i];
  108. var c_out = out_alias[c.o];
  109. //if (c.r.test(name)) {
  110. //return c.o;
  111. //}
  112. var result = c.r.exec(name);
  113. if (result !== null){
  114. if (c.geq && result[2] && Number(result[2])>=Number(c.geq)){
  115. return c_out;
  116. } else if (c.leq && result[2] && Number(result[2])<=Number(c.leq)){
  117. return c_out;
  118. } else if (!c.geq && !c.leq){
  119. return c_out;
  120. };
  121. }
  122. }
  123. }
  124.  
  125. window.applyWisRule = function () {
  126. wisLoadRule();
  127. resetCount();
  128. $("div.layout_clear > table.content_table > tbody > tr").each(function () {
  129. var t = $(this);
  130. // o = where the item should go if rule matches
  131. //console.log('wod sort item: '+t.children("td").eq(1).children("a").text());
  132. //var o = getOperation(t.children("td").eq(1).children("a").text(), ruleObj.rules);
  133. //console.log('465667 '+t.children("td").eq(1).text());
  134. var o = getOperation(t.children("td").eq(1).text(), ruleObj.rules);
  135. if (o) {
  136. applyOperation(t,o);
  137. }
  138. });
  139. $("div.item_sort_stats").focus().text(" 仓库" + counts.go_lager + " 贮藏室" +counts.go_keller+ " 宝库 "+counts.go_group+" 团队仓库"+counts.go_group_2+" NPC "+counts.npc);
  140. }
  141.  
  142. window.resetCount = function () {
  143. counts.go_lager = 0;
  144. counts.go_group_2 = 0;
  145. counts.go_group = 0;
  146. counts.go_keller = 0;
  147. counts.npc = 0;
  148. };
  149.  
  150. window.applyOperation = function(t,o){
  151. var s;
  152. if ("NUL" == o) {
  153. return;
  154. }
  155. var c;
  156. if ("npc"==o) {
  157. //s = t.children().eq(3).children("input:checkbox");
  158. s = t.children("td").has("img[title='金币']").eq(0).children("input:checkbox");
  159. s.attr('checked', true);
  160. c = "rgba(255,34,34,0.9)";
  161. s.parent().css("color",c);
  162. t.children().eq(1).children("a").css("background-color",c);
  163. counts.npc += 1;
  164. } else {
  165. s = t.children().eq(2).children("select");
  166. if ("-"+o != s.val()) {
  167. s.val(o);
  168. c = "rgba(127,127,127,0.5)";
  169. s.css("border-color",c);
  170. t.children().eq(1).children("a").css("background-color",c);
  171. counts[o] += 1;
  172. }
  173. }
  174. };
  175.  
  176.  
  177. window.injectUi = function (){
  178. $("div#main_content").after(uiHtml);
  179. $("div#main_content").after(btnsHtml);
  180. //$("#main_content form input[type='submit']:eq(0)").after(statsHtml);
  181. $("#wisawrb:eq(0)").after(statsHtml);
  182. };
  183.  
  184.  
  185.  
  186. window.rowOnMouseColor = function () {
  187. $("div.layout_clear table.content_table tbody tr").each(function () {
  188. $(this).addClass("tr_mouse");
  189. });
  190. $("div.gadget_body form table.content_table tbody tr").each(function () {
  191. $(this).addClass("tr_mouse");
  192. });
  193. };
  194.  
  195. function addGlobalStyle(css) {
  196. var head, style;
  197. head = document.getElementsByTagName('head')[0];
  198. if (!head) { return; }
  199. style = document.createElement('style');
  200. style.type = 'text/css';
  201. style.innerHTML = css;
  202. head.appendChild(style);
  203. }
  204.  
  205. function hoverToggleSelect () {
  206. $("table.content_table tbody tr td input[value=do]").mouseover(function () {
  207. $(this).prop("checked",!$(this).is(":checked"));
  208. });
  209. }
  210.  
  211. addGlobalStyle('.tr_mouse:hover { background-color:rgba(248,248,23,0.5); }');
  212.  
  213. window.addEventListener("load",injectUi,false);
  214.  
  215. //window.addEventListener("load",autoSort,false);
  216.  
  217. window.addEventListener("load", rowOnMouseColor,false);
  218.  
  219. //window.addEventListener("load", hoverToggleSelect,false);
  220. };
  221.  
  222.  
  223. $(document).ready(main);