Macro Combat Compacter

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

  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 0.0.1.20140812160455
  8. // ==/UserScript==
  9.  
  10. //Version 1.1
  11. // - also now shows effect gains/losses
  12. //Version 1.0
  13.  
  14. function compact() {
  15. var hrs = document.evaluate( '//hr', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  16. var stuff = [];
  17. var ids = 1;
  18. // gather contents
  19. for (var i=hrs.snapshotLength-1;i>=0;i--) {
  20. var hr2 = hrs.snapshotItem(i);
  21. var itemids = "";
  22. stuff[i] = document.createElement('div');
  23. stuff[i].setAttribute('class','compactDiv');
  24. while (hr2.previousSibling && hr2.previousSibling.tagName!='HR' && hr2.previousSibling.tagName!='BR') {
  25. var n = hr2.previousSibling;
  26. n.parentNode.removeChild(n);
  27. if (stuff[i].firstChild)
  28. stuff[i].insertBefore(n,stuff[i].firstChild);
  29. else
  30. stuff[i].appendChild(n);
  31. }
  32. stuff[i].setAttribute('style','display:none');
  33. hr2.parentNode.insertBefore(stuff[i],hr2);
  34. hr2.addEventListener('click',expandDivH,true);
  35. hr2.setAttribute('title','click to display round '+(i+1));
  36. var r = findAcquires(stuff[i]);
  37. if (r.length>0) {
  38. for (var j=0;j<r.length;j++) {
  39. itemids = itemids + " " + ids;
  40. r[j].setAttribute('id','compactAcquire_'+ids);
  41. ids++;
  42. stuff[i].parentNode.insertBefore(r[j],stuff[i]);
  43. }
  44. stuff[i].setAttribute('compactAcquire',itemids);
  45. }
  46. }
  47. }
  48.  
  49. // delete any duplicated item/meat acquisitions
  50. function removeAcquires(alist) {
  51. var aa = alist.split(' ');
  52. for (var i=0;i<aa.length;i++) {
  53. if (aa[i]) {
  54. var r = document.getElementById('compactAcquire_'+aa[i]);
  55. if (r) {
  56. r.parentNode.removeChild(r);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. // expand a single div and delete any item/meat acquisitions
  63. function expandDiv(d) {
  64. var s = d.getAttribute("style");
  65. if (s.match(/display\s*:\s*none\s*;?/i)) {
  66. d.setAttribute("style",s.replace(/display\s*:\s*none\s*;?/i,''));
  67. var itemlist = d.getAttribute('compactAcquire');
  68. if (itemlist) {
  69. removeAcquires(itemlist);
  70. }
  71. }
  72. }
  73.  
  74. // handler for introduced divs
  75. function expandDivH() {
  76. var d = this.previousSibling;
  77. if (d && d.tagName=='DIV') {
  78. expandDiv(d);
  79. }
  80. this.removeEventListener('click',expandDivH,true);
  81. }
  82.  
  83. // expand all divs handler
  84. function expandAllDivs(e) {
  85. var ds = document.getElementsByClassName('compactDiv');
  86. if (ds) {
  87. for (var i=0;i<ds.length;i++) {
  88. expandDiv(ds[i]);
  89. }
  90. }
  91. }
  92.  
  93. // change the jump to bottom button to an expand all rounds button
  94. function addExpandAll() {
  95. var b = document.getElementById('jumptobot');
  96. b.addEventListener('click',expandAllDivs,false);
  97. b.setAttribute('title','click to expand all rounds');
  98. b.innerHTML = '(expand rounds)';
  99. }
  100.  
  101. // return clones of all item/meat acquisitions from the supplied root element
  102. function findAcquires(doc) {
  103. var r = [];
  104. var msg = ['.//td[text()="You acquire an item: "]',
  105. './/td[text()="You acquire an effect: "]',
  106. './/td[text()="You lose an effect: "]'];
  107.  
  108. for (var m=0;m<msg.length;m++) {
  109. var ps = document.evaluate(msg[m],doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  110. if (ps.snapshotLength>0) {
  111. for (var i=0;i<ps.snapshotLength;i++) {
  112. var p = ps.snapshotItem(i).parentNode;
  113. while (p && p.tagName!='CENTER')
  114. p = p.parentNode;
  115. if (p) {
  116. r[r.length] = p.cloneNode(true);
  117. }
  118. }
  119. }
  120. }
  121. ps = document.evaluate('.//td[contains(text(),"Meat.")]',doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  122. if (ps.snapshotLength>0) {
  123. for (var i=0;i<ps.snapshotLength;i++) {
  124. var p = ps.snapshotItem(i);
  125. if (p.innerHTML && p.innerHTML.match(/You gain [0-9]+ Meat./)) {
  126. p = p.parentNode;
  127. while (p && p.tagName!='CENTER')
  128. p = p.parentNode;
  129. if (p) {
  130. r[r.length] = p.cloneNode(true);
  131. }
  132. }
  133. }
  134. }
  135. return r;
  136. }
  137.  
  138. if (document.getElementById('jumptobot')) {
  139. compact();
  140. addExpandAll();
  141. }