Trophy Counter

Counts trophies and shows missing trophies in Kingdom of Loathing.

  1. // ==UserScript==
  2. // @name Trophy Counter
  3. // @namespace kol.interface.unfinished
  4. // @description Counts trophies and shows missing trophies in Kingdom of Loathing.
  5. // @include https://*kingdomofloathing.com/showplayer.php*
  6. // @include http://kol.coldfront.net/thekolwiki/index.php/Trophy*
  7. // @version 1.13
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // ==/UserScript==
  11.  
  12. //Version 1.13
  13. // - fix it so it works again given minor wiki changes to table sorting, and switch to https
  14. //Version 1.12
  15. // - add @grant
  16. //Version 1.1
  17. // - make impossible trophies dimmer
  18. //Version 1.0
  19.  
  20. function doPage() {
  21. // find table root of area interested in
  22. var yourTrophies = {};
  23. var count = 0;
  24. var c = document.evaluate('//center[text()="Trophies:"]',document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
  25. if (c.singleNodeValue) {
  26. var ts = document.evaluate('.//img',c.singleNodeValue.nextSibling,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
  27. for (var i=0;i<ts.snapshotLength;i++) {
  28. var t = ts.snapshotItem(i).getAttribute('title');
  29. yourTrophies[t] = true;
  30. count++;
  31. }
  32. GM_setValue('yourtrophies',JSON.stringify(yourTrophies));
  33. addButton(c.singleNodeValue,count);
  34. if (GM_getValue('showmissing',''))
  35. showMissing();
  36. }
  37. }
  38.  
  39. function addButton(c,count) {
  40. var total = GM_getValue('knowntrophycount','?');
  41. c.appendChild(document.createTextNode('\u00A0'+count+'/'+total));
  42. var v = document.createElement('a');
  43. v.setAttribute('href','#');
  44. v.setAttribute('id','showmissingtrophybutton');
  45. v.setAttribute('style','font-size:x-small;vertical-align:middle;');
  46. fixupButton(v);
  47. v.addEventListener('click',bHandler,false);
  48. c.appendChild(document.createTextNode('\u00a0\u00a0\u00a0'));
  49. c.appendChild(v);
  50. var goWiki = document.createElement('a');
  51. goWiki.setAttribute('title','Click to go to the full list of trophies on the KoL wiki.');
  52. goWiki.setAttribute('target','_blank');
  53. goWiki.setAttribute('id','trophywikilink');
  54. goWiki.setAttribute('style','font-size:x-small;vertical-align:middle;');
  55. goWiki.setAttribute('href','http://kol.coldfront.net/thekolwiki/index.php/Trophy');
  56. goWiki.appendChild(document.createTextNode('[Load trophy list]'));
  57. c.appendChild(document.createTextNode('\u00A0'));
  58. c.appendChild(goWiki);
  59. }
  60.  
  61. function fixupButton(v) {
  62. if (GM_getValue('showmissing','')) {
  63. v.setAttribute('title','Click to hide all missing trophies.');
  64. if (v.firstChild)
  65. v.replaceChild(document.createTextNode('[Hide Missing]'),v.firstChild);
  66. else
  67. v.appendChild(document.createTextNode('[Hide Missing]'));
  68. } else {
  69. v.setAttribute('title','Click to display all missing trophies.');
  70. if (v.firstChild)
  71. v.replaceChild(document.createTextNode('[Show Missing]'),v.firstChild);
  72. else
  73. v.appendChild(document.createTextNode('[Show Missing]'));
  74. }
  75. }
  76.  
  77. function showMissing() {
  78. var v = document.getElementById('showmissingtrophybutton');
  79. if (GM_getValue('showmissing','')) {
  80. missing();
  81. } else {
  82. var xx = document.getElementsByClassName('showmissingaddition');
  83. for (var i=xx.length-1;i>=0;i--) {
  84. var x = xx[i];
  85. x.parentNode.removeChild(x);
  86. }
  87. }
  88. fixupButton(v);
  89. }
  90.  
  91. function bHandler(e) {
  92. var b = GM_getValue('showmissing','');
  93. GM_setValue('showmissing', (b ? '' : 'true'));
  94. showMissing();
  95. }
  96.  
  97. // locate all missing recipes
  98. function missing() {
  99. if (!GM_getValue('knowntrophycount'))
  100. return;
  101. var c = document.evaluate('//center[text()="Trophies:"]',document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
  102. if (c.singleNodeValue && c.singleNodeValue.nextSibling) {
  103. var t = c.singleNodeValue.nextSibling;
  104. var r = t.insertRow(-1);
  105. r.setAttribute('class','showmissingaddition');
  106. var ctr = document.createElement('center');
  107. ctr.appendChild(document.createTextNode('Missing Trophies'));
  108. r.insertCell(-1).appendChild(ctr);
  109. r = t.insertRow(-1);
  110. r.setAttribute('class','showmissingaddition');
  111. td = r.insertCell(-1);
  112. td.setAttribute('align','center');
  113. var lastrc = 0;
  114. var alltrophies = JSON.parse(GM_getValue('knowntrophies','[]'));
  115. var yourtrophies = JSON.parse(GM_getValue('yourtrophies','{}'));
  116. var parity = 1;
  117. for (var i=0;i<alltrophies.length;i++) {
  118. if (!alltrophies[i])
  119. continue;
  120. if (!yourtrophies[alltrophies[i].name]) {
  121. if (lastrc==0) {
  122. td.appendChild(document.createElement('br'));
  123. parity = 1-parity;
  124. }
  125. lastrc = (lastrc+1) % (5+parity);
  126. var img = document.createElement('img');
  127. img.setAttribute('src','http://images.kingdomofloathing.com/otherimages/trophy/'+alltrophies[i].img);
  128. img.setAttribute('width','100');
  129. img.setAttribute('height','100');
  130. img.setAttribute('title',i+'. '+alltrophies[i].name+': '+alltrophies[i].cause);
  131. if (alltrophies[i].name.match(/^Silver Yeti$/) ||
  132. alltrophies[i].name.match(/^Golden Yeti$/) ||
  133. alltrophies[i].name.match(/^This Lousy Trophy$/) ||
  134. alltrophies[i].name.match(/^Friend of Elves$/) ||
  135. alltrophies[i].name.match(/^Reindeer Hunter$/) ||
  136. alltrophies[i].name.match(/^Look, Ma! No Pants!$/)) {
  137. img.setAttribute('style','opacity:0.4');
  138. img.setAttribute('title',i+'. '+alltrophies[i].name+': '+alltrophies[i].cause+' [note: no longer obtainable]');
  139. }
  140. td.appendChild(img);
  141. }
  142. }
  143. r = t.insertRow(-1);
  144. r.setAttribute('class','showmissingaddition');
  145. td = r.insertCell(-1);
  146. td.setAttribute('bgcolor','black');
  147. td.setAttribute('height','1');
  148. }
  149. return true;
  150. }
  151.  
  152. // return a new copy of the master list
  153. function retrieveCopy(page) {
  154. var r = JSON.parse(GM_getValue('knowntrophies','[]'));
  155. return r;
  156. }
  157.  
  158. function strip(s) {
  159. if (!s)
  160. return '';
  161. return s.replace(/^\s+/,'').replace(/\s+$/,'');
  162. }
  163.  
  164. function removeHTML(s) {
  165. if (!s)
  166. return '';
  167. return s.replace(/<[^>]*>/g,'');
  168. }
  169.  
  170. // gather data from a kol wiki page
  171. function gather() {
  172. var trophies = [];
  173. var count=0;
  174. var ts = document.evaluate('//table[@class="sortable"]',document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
  175. var t = ts.singleNodeValue;
  176. if (t) {
  177. for (var i=1;i<t.rows.length;i++) {
  178. var r = t.rows[i];
  179. var tnum = Number(r.cells[0].innerHTML);
  180. if (tnum) {
  181. if (r.cells[1] && r.cells[1].firstChild) {
  182. var tname = strip(r.cells[1].firstChild.innerHTML);
  183. var timg = strip(r.cells[3].innerHTML);
  184. var tcause = strip(removeHTML(r.cells[4].innerHTML));
  185. if (tname && timg) {
  186. timg = timg.replace(/ /g,'_')+'.gif';
  187. count++;
  188. trophies[tnum] = {'name':tname,'img':timg,'cause':tcause};
  189. }
  190. }
  191. }
  192. }
  193. }
  194. console.log('Loaded '+count+' trophies from the wiki');
  195. if (count>0) {
  196. GM_setValue('knowntrophycount',count);
  197. GM_setValue('knowntrophies',JSON.stringify(trophies));
  198. } else {
  199. console.log('t is '+((t==null) ? "null" : "ok"));
  200. }
  201. }
  202.  
  203.  
  204. if (window.location.href.indexOf('http://kol.coldfront.net/thekolwiki/index.php/')>=0) {
  205. gather();
  206. } else {
  207. doPage();
  208. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址