Bang Identifier

identifieds DoD potions. KOL

  1. // ==UserScript==
  2. // @name Bang Identifier
  3. // @namespace KoLCtH
  4. // @include *kingdomofloathing.com/game.php*
  5. // @include *kingdomofloathing.com/charsheet.php*
  6. // @include *kingdomofloathing.com/fight.php*
  7. // @include *kingdomofloathing.com/inventory.php*
  8. // @include *kingdomofloathing.com/afterlife.php*
  9. // @include *127.0.0.1:*/game.php*
  10. // @include *127.0.0.1:*/charsheet.php*
  11. // @include *127.0.0.1:*/fight.php*
  12. // @include *127.0.0.1:*/inventory.php*
  13. // @include *127.0.0.1:*/afterlife.php*
  14. // @version 0.0.1.20140812160538
  15. // @description identifieds DoD potions. KOL
  16. // ==/UserScript==
  17.  
  18. switch(window.location.pathname)
  19. {
  20. case '/game.php':
  21. GM_deleteValue("CurrentCharName");
  22. break
  23. case '/inventory.php':
  24. if (!$('table#potions'))
  25. return
  26. document.addEventListener('click', function (e)
  27. {
  28. //place event listener to break the link and get the results page directly so we can parse it
  29. if (e.target.tagName.toLowerCase() != 'a')
  30. return
  31. var itemNum = e.target.href.match(/\d+$/)
  32. if (!itemNum || itemNum < 818 || itemNum > 828)
  33. return
  34. e.preventDefault();
  35. e.stopPropagation();
  36. unsafeWindow.loadingDiv();
  37. var URL = e.target.href.replace('which=3', 'ajax=1')
  38. GM_xmlhttpRequest({
  39. method: 'GET',
  40. url: URL,
  41. onload: function (res)
  42. {
  43. stealThisBang(res.responseText, e.target, true)
  44. }
  45. });
  46. }, true);
  47. $('td[id]', true).forEach(function (td)
  48. {
  49. var itemNum = parseInt(td.id.substr(1))
  50. if (itemNum > 818 && itemNum < 828)
  51. {
  52. td.appendChild(CE("br"))
  53. //get bang effects from scriptval or place listener to catch them
  54. var effect = identifyBang(itemNum)
  55. var newNode = td.appendChild(CE("strong", 'style|color:blue', 'text|' + effect))
  56. }
  57. })
  58. break
  59. case '/fight.php':
  60. var bangUsed = $('img[src *= "exclam.gif"]')
  61. if (bangUsed)
  62. stealThisBang(null, bangUsed)
  63. break
  64. case "/charsheet.php":
  65. var charName = $('center>b>a').textContent
  66. GM_setValue('CurrentCharName', charName)
  67. var bangNames = ["milky", "swirly", "bubbly", "smoky", "cloudy", "effervescent", "fizzy", "dark", "murky"];
  68. var knownBangs = GM_getValue(charName + '.bangs', '0|0|0|0|0|0|0|0|0').split('|')
  69. var insertPoint = $('p:contains("Current Familiar:")').parentNode
  70. var p = insertPoint.appendChild(CE('p'))
  71. p.appendChild(CE('b', 'text|Bang Potions:'))
  72. p.appendChild(CE('br'))
  73. var table = p.appendChild(CE('table', 'width|60%'))
  74. table.appendChild(CE('td', 'height|1', 'colspan|2', 'bgcolor|black'))
  75. for (var n=0;n<9;n++)
  76. {
  77. var tr = table.appendChild(CE('tr'))
  78. if (n & 1)
  79. tr.style.backgroundColor = '#ddd'
  80. var td1 = tr.appendChild(CE('td', 'align|left', 'text|' + bangNames[n] + ' potion:'))
  81. var bangEff = (knownBangs[n] == '0') ? 'unknown' : knownBangs[n]
  82. var td2 = tr.appendChild(CE('td', 'align|right', 'text|' + bangEff))
  83. }
  84. table.appendChild(CE('td', 'height|1', 'colspan|2', 'bgcolor|black'))
  85. break
  86. case '/afterlife.php':
  87. GM_deleteValue(getCharName() + '.bangs')
  88. break
  89. }
  90.  
  91. function identifyBang(itemNum)
  92. {
  93. var charName = getCharName()
  94. var whichBang = itemNum - 819;
  95. var knownBangs = GM_getValue(charName + '.bangs', "0|0|0|0|0|0|0|0|0").split("|");
  96. if (knownBangs[whichBang] != 0)
  97. return knownBangs[whichBang];
  98. else
  99. return 'unknown';
  100. }
  101.  
  102. function stealThisBang(txt, tgt, fromInv)
  103. {
  104. var charName = getCharName()
  105. txt = txt || tgt.parentNode.nextSibling.textContent
  106. var bangEffects = {
  107. 0:['Confused', 'looks confused', '-30% Mys (20 Adv)'],
  108. 1:['liquid fire', 'like a wino', 'Gain 1-3 Drunk'],
  109. 2:['hit points', 'feels better', 'Gain 14-16 HP/MP'],
  110. 3:["Izchak's Blessing", 'more stylish', '+25% Mox (10 Adv)'],
  111. 4:['Strength of Ten Ettins', 'much stronger', '+25% Mus (10 Adv)'],
  112. 5:['Strange Mental Acuity', 'much smarter', '+25% Mys (10 Adv)'],
  113. 6:['Object Detection', 'blink', '+12.5% Item Drops (10 Adv)'],
  114. 7:['Sleepy', 'yawns', '-30% Mus (20 Adv)'],
  115. 8:['Teleportitis', 'starts disappearing', 'Teleportitis (10 Adv)']
  116. }
  117. var knownBangs = GM_getValue(charName + '.bangs', '0|0|0|0|0|0|0|0|0').split('|');
  118. if (fromInv)
  119. //intercepts bang use from inventory
  120. {
  121. for (var n=0;n<9;n++)
  122. {
  123. if (txt.indexOf(bangEffects[n][0]) != -1)
  124. {
  125. var thisBangEffect = bangEffects[n][2]
  126. break
  127. }
  128. }
  129. var thisBangNum = tgt.href.match(/(\d+)$/)[1] - 819
  130. //replace 'unknown' with correct effect
  131. tgt.parentNode.nextSibling.nextSibling.textContent = knownBangs[thisBangNum] = thisBangEffect;
  132. var frag = CE('div')
  133. frag.innerHTML = txt
  134. $('#effdiv').replaceChild($('center', false, frag), $('#effdiv').firstChild)
  135. unsafeWindow.top.charpane.location.href = "charpane.php"
  136. unsafeWindow.updateInv({itemNum:-1})
  137. }
  138. else
  139. //detects bang use in combat
  140. {
  141. var bangNames = ["milky", "swirly", "bubbly", "smoky", "cloudy", "effervescent", "fizzy", "dark", "murky"];
  142. for (var n=0;n<9;n++)
  143. {
  144. if (!thisBangNum)
  145. {
  146. if (txt.indexOf(bangNames[n]+' potion') != -1)
  147. var thisBangNum = n
  148. }
  149. if (!thisBangEffect)
  150. {
  151. if (txt.indexOf(bangEffects[n][1]) != -1)
  152. var thisBangEffect = bangEffects[n][2]
  153. }
  154. }
  155. knownBangs[thisBangNum] = thisBangEffect
  156. }
  157. GM_setValue(charName + '.bangs', knownBangs.join('|'));
  158. }
  159.  
  160. function getCharName()
  161. {
  162. //can get charName from any other frame and save it to GM val for next time
  163. if (GM_getValue('CurrentCharName', null))
  164. return GM_getValue('CurrentCharName')
  165. if (!window.top || !window.top.frames[1])
  166. return false
  167. var charName = window.top.frames[1].document.querySelector('b')
  168. if (charName)
  169. charName = charName.textContent
  170. else
  171. return false
  172. GM_setValue('CurrentCharName', charName)
  173. return charName
  174. }
  175.  
  176. function CE(tag/*,attributes*/)
  177. {
  178. var node = document.createElement(tag)
  179. for (i=1,len=arguments.length;i<len;i++)
  180. {
  181. var attr = arguments[i].split('|')
  182. if (attr[0] == 'text')
  183. node.textContent = attr[1]
  184. else
  185. node.setAttribute(attr[0], attr[1])
  186. }
  187. return node
  188. }
  189.  
  190. function $(selector, all, scope)
  191. {
  192. scope = scope || document
  193. if (selector.indexOf(':contains') != -1)
  194. {
  195. var test = selector.match(/([^:]+):contains\(["'](.+)["']\)/)
  196. var res = scope.querySelectorAll(test[1])
  197. for (a in res) {
  198. if (res[a].textContent && res[a].textContent.indexOf(test[2]) != -1)
  199. return res[a]
  200. }
  201. }
  202. else if (all)
  203. return Array.slice.call(scope.querySelectorAll(selector))
  204. else
  205. return scope.querySelector(selector)
  206. }