Macro Combat Compacter

Compacts the divisions shown when using fight macros in KoL, displaying only the last.

当前为 2014-08-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Macro Combat Compacter
  3. // @namespace kol.interface.unfinished
  4. // @description Compacts the divisions shown when using fight macros in KoL, displaying only the last.
  5. // @include http://*kingdomofloathing.com/fight.php*
  6. // @include http://127.0.0.1:*/fight.php*
  7. // @version 1.21
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // ==/UserScript==
  11.  
  12. //Version 1.21
  13. // - add @grant
  14. //Version 1.2
  15. // - added round counter and attack/defense notice compaction
  16. //Version 1.1
  17. // - also now shows effect gains/losses
  18. //Version 1.0
  19.  
  20. function compact() {
  21. var hrs = document.evaluate( '//hr', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  22. var stuff = [];
  23. var ids = 1;
  24. extractAdjustments(hrs.snapshotLength);
  25. // gather contents
  26. for (var i=hrs.snapshotLength-1;i>=0;i--) {
  27. var hr2 = hrs.snapshotItem(i);
  28. var itemids = "";
  29. stuff[i] = document.createElement('div');
  30. stuff[i].setAttribute('class','compactDiv');
  31. while (hr2.previousSibling && hr2.previousSibling.tagName!='HR' && hr2.previousSibling.tagName!='BR') {
  32. var n = hr2.previousSibling;
  33. n.parentNode.removeChild(n);
  34. if (stuff[i].firstChild)
  35. stuff[i].insertBefore(n,stuff[i].firstChild);
  36. else
  37. stuff[i].appendChild(n);
  38. }
  39. stuff[i].setAttribute('style','display:none');
  40. hr2.parentNode.insertBefore(stuff[i],hr2);
  41. hr2.addEventListener('click',expandDivH,true);
  42. hr2.setAttribute('title','click to display round '+(i+1));
  43. var r = findAcquires(stuff[i]);
  44. if (r.length>0) {
  45. for (var j=0;j<r.length;j++) {
  46. itemids = itemids + " " + ids;
  47. r[j].setAttribute('id','compactAcquire_'+ids);
  48. ids++;
  49. stuff[i].parentNode.insertBefore(r[j],stuff[i]);
  50. }
  51. stuff[i].setAttribute('compactAcquire',itemids);
  52. }
  53. }
  54. }
  55.  
  56. // delete any duplicated item/meat acquisitions
  57. function removeAcquires(alist) {
  58. var aa = alist.split(' ');
  59. for (var i=0;i<aa.length;i++) {
  60. if (aa[i]) {
  61. var r = document.getElementById('compactAcquire_'+aa[i]);
  62. if (r) {
  63. r.parentNode.removeChild(r);
  64. }
  65. }
  66. }
  67. }
  68.  
  69. // expand a single div and delete any item/meat acquisitions
  70. function expandDiv(d) {
  71. var s = d.getAttribute("style");
  72. if (s.match(/display\s*:\s*none\s*;?/i)) {
  73. d.setAttribute("style",s.replace(/display\s*:\s*none\s*;?/i,''));
  74. var itemlist = d.getAttribute('compactAcquire');
  75. if (itemlist) {
  76. removeAcquires(itemlist);
  77. }
  78. }
  79. }
  80.  
  81. // handler for introduced divs
  82. function expandDivH() {
  83. var d = this.previousSibling;
  84. if (d && d.tagName=='DIV') {
  85. expandDiv(d);
  86. }
  87. this.removeEventListener('click',expandDivH,true);
  88. }
  89.  
  90. // expand all divs handler
  91. function expandAllDivs(e) {
  92. var ds = document.getElementsByClassName('compactDiv');
  93. if (ds) {
  94. for (var i=0;i<ds.length;i++) {
  95. expandDiv(ds[i]);
  96. }
  97. }
  98. }
  99.  
  100. // change the jump to bottom button to an expand all rounds button
  101. function addExpandAll() {
  102. var b = document.getElementById('jumptobot');
  103. b.addEventListener('click',expandAllDivs,false);
  104. b.setAttribute('title','click to expand all rounds');
  105. b.innerHTML = '(expand rounds)';
  106. }
  107.  
  108. // return clones of all item/meat acquisitions from the supplied root element
  109. function findAcquires(doc) {
  110. var r = [];
  111. var msg = ['.//td[text()="You acquire an item: "]',
  112. './/td[text()="You acquire an effect: "]',
  113. './/td[text()="You lose an effect: "]'];
  114.  
  115. for (var m=0;m<msg.length;m++) {
  116. var ps = document.evaluate(msg[m],doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  117. if (ps.snapshotLength>0) {
  118. for (var i=0;i<ps.snapshotLength;i++) {
  119. var p = ps.snapshotItem(i).parentNode;
  120. while (p && p.tagName!='CENTER')
  121. p = p.parentNode;
  122. if (p) {
  123. r[r.length] = p.cloneNode(true);
  124. }
  125. }
  126. }
  127. }
  128. ps = document.evaluate('.//td[contains(text(),"Meat.")]',doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  129. if (ps.snapshotLength>0) {
  130. for (var i=0;i<ps.snapshotLength;i++) {
  131. var p = ps.snapshotItem(i);
  132. if (p.innerHTML && p.innerHTML.match(/You gain [0-9]+ Meat./)) {
  133. p = p.parentNode;
  134. while (p && p.tagName!='CENTER')
  135. p = p.parentNode;
  136. if (p) {
  137. r[r.length] = p.cloneNode(true);
  138. }
  139. }
  140. }
  141. }
  142. return r;
  143. }
  144.  
  145. function accumulateTitle(n,text) {
  146. if (n.nodeType==3) n = n.parentNode;
  147. var t = n.getAttribute('title');
  148. if (t) {
  149. n.setAttribute('title',t+' '+text);
  150. } else
  151. n.setAttribute('title',text);
  152. }
  153.  
  154. function hideAtkDef(p) {
  155. p.parentNode.removeChild(p);
  156. }
  157.  
  158. function extractAdjustments(extrar) {
  159. var atk=Number(GM_getValue('atk',0)),def=Number(GM_getValue('def',0));
  160. var delta=0,deltd=0;
  161. var r=Number(GM_getValue('round',0))+extrar;
  162. var psa = document.evaluate('.//td[contains(text(),"Monster attack power reduced by ")]',document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  163. for (var ia=0;ia<psa.snapshotLength;ia++) {
  164. var p = psa.snapshotItem(ia);
  165. var delt = Number(p.innerHTML.replace(/[^0-9]+/g,''));
  166. delta += delt;
  167. p = p.parentNode;
  168. while (p && p.tagName!='CENTER')
  169. p = p.parentNode;
  170. if (p) {
  171. if (p.previousSibling)
  172. accumulateTitle(p.previousSibling,'Monster attack power reduced by '+delt+'.');
  173. hideAtkDef(p);
  174. }
  175. }
  176. psa = document.evaluate('.//td[contains(text(),"Monster defense reduced by ")]',document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  177. for (var ia=0;ia<psa.snapshotLength;ia++) {
  178. var p = psa.snapshotItem(ia);
  179. var delt = Number(p.innerHTML.replace(/[^0-9]+/g,''));
  180. deltd += delt;
  181. p = p.parentNode;
  182. while (p && p.tagName!='CENTER')
  183. p = p.parentNode;
  184. if (p) {
  185. if (p.previousSibling) {
  186. accumulateTitle(p.previousSibling,'Monster defense reduced by '+delt+'.');
  187. }
  188. hideAtkDef(p);
  189. }
  190. }
  191. r++;
  192. GM_setValue('round',r);
  193. atk += delta;
  194. def += deltd;
  195. if (atk!=0 || def!=0 || r>1) {
  196. var d = document.getElementById('compactatkdef');
  197. var ps;
  198. if (!d) {
  199. ps = document.evaluate('.//td/b[text()="Combat!"]',document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
  200. if (ps.singleNodeValue) {
  201. d = document.createElement('div');
  202. d.setAttribute('style','display:inline;float:left;font-size:small;');
  203. d.setAttribute('id','compactatkdef');
  204. ps.singleNodeValue.parentNode.insertBefore(d,ps.singleNodeValue);
  205. } else
  206. return;
  207. }
  208. GM_setValue('atk',atk);
  209. GM_setValue('def',def);
  210. if (delta!=0 || deltd!=0) {
  211. d.setAttribute('title','Round '+r+': monster attack reduced by '+delta+' and defense by '+deltd+'.');
  212. } else {
  213. d.setAttribute('title','Round '+r+'.');
  214. }
  215. var c = (r>1) ? '\u00A0('+r+') ' : '\u00A0';
  216. if (atk>0) c = c + 'A: -'+atk+' ';
  217. if (def>0) c = c + 'D: -'+def;
  218. if (d.firstChild)
  219. d.replaceChild(document.createTextNode(c),d.firstChild);
  220. else
  221. d.appendChild(document.createTextNode(c));
  222. }
  223. }
  224.  
  225. if (window.location.search && window.location.search.indexOf('ireallymeanit')>=0) {
  226. GM_setValue('atk',0);
  227. GM_setValue('def',0);
  228. GM_setValue('round',0);
  229. } else if (document.evaluate('.//p[text()="It gets the jump on you."]',document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue) {
  230. GM_setValue('atk',0);
  231. GM_setValue('def',0);
  232. GM_setValue('round',0);
  233. }
  234.  
  235. if (document.getElementById('jumptobot')) {
  236. compact();
  237. addExpandAll();
  238. } else {
  239. extractAdjustments(0);
  240. }