LWM_ProgressBars

Insert level & skill progress bars to home page & player info page.

目前为 2014-08-21 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name LWM_ProgressBars
  3. // @description Insert level & skill progress bars to home page & player info page.
  4. // @namespace saturn_hwm
  5. // @author saturn573
  6. // @homepage http://greasyfork.org/scripts/2892-lwm-progressbars
  7. // @include http://178.248.235.15/home.php
  8. // @include http://*.lordswm.com/home.php
  9. // @include http://*.heroeswm.ru/home.php
  10. // @include http://178.248.235.15/pl_info.php?id=*
  11. // @include http://*.lordswm.com/pl_info.php?id=*
  12. // @include http://*.heroeswm.ru/pl_info.php?id=*
  13. // @version 0.15
  14. // @grant none
  15.  
  16. var Scales = [
  17. [0, 1500, 4500, 15000, 32000, 90000, 190000, 400000, 860000, 1650000, 3000000, 5000000, 8500000, 14500000, 25000000, 43000000, 70000000, 108000000, 160000000, 230000000, 325000000],
  18. [20, 50, 90, 160, 280, 500, 900, 1600, 2900, 5300, 9600, 17300],
  19. [16, 60, 180, 400, 700, 1200, 2000, 3000, 4300, 6000, 8000, 10500],
  20. [90, 180, 360, 720, 1500, 3000, 5000, 8000, 12000, 17000, 23000, 30000, 38000, 47000, 57000],
  21. [10, 30, 60, 100, 150, 210, 280, 360, 450, 550, 660, 800, 1000, 1300, 2000],
  22. [50, 120, 240, 400, 600, 840, 1200, 2000, 3000, 4300, 6000, 8000, 10800, 14000, 17600],
  23. [100, 240, 480, 800, 1200, 1680, 2400, 4000, 6000, 8600],
  24. [50, 120, 300, 600, 1000, 1500, 2200, 3000, 4000, 5500, 7800, 11000, 14500, 18200, 22200],
  25. [150, 350, 750, 1400, 2200, 4000, 6000],
  26. [30, 80, 165, 310, 555, 970, 1680, 2885, 5770],
  27. [104, 588, 2200, 7000, 10000],
  28. [8, 29, 71, 155, 295, 505, 799, 1191, 1695, 6000, 12000]
  29. ];
  30.  
  31. var factionCount = 9;
  32.  
  33. var Styles = '.pb{ display:inline-block; width:135px; height:16px; background:white; border:2px solid; border-radius: 7px/3px; cursor: pointer; }\
  34. .pb-scale { height: 100%; border-radius: 2px/1px; background:#af9f39; background: linear-gradient(to top, #af9f39, #fffbca); }\
  35. .pb-scale:hover .pb-side-text { opacity: 1; }\
  36. .pb-scale:hover .pb-front-text { opacity: 0; }\
  37. .pb-text { padding-top: 1px; background: initial; overflow: none; position: relative; width: 135px; text-align: center; color: darkgreen; font-size: 12px; font-weight: bold; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }\
  38. .pb-side-text { opacity:0; }\
  39. .pb-front-text { opacity:1; top: -15px; }\
  40. .levelpb { display: inline-flex; margin-left: 10px; width: 180px; } \
  41. .levelpb .pb-side-text { width: 180px; }\
  42. .levelpb .pb-front-text { width: 180px; }\
  43. .skill-table { width: 100%; margin-bottom: 10px; }\
  44. .skill-table caption { text-align: left; }\
  45. .skill-table caption span { float: right; font-size: small; margin-right: 10; }\
  46. .key-column { text-align: left; width: 100%; font-variant: small-caps; }\
  47. .skill-value { font-size: 15px; }\
  48. .skill-value-complete { color: #af9f39; font-size: 20px; font-weight: bold; font-family: "Comic Sans MS", cursive, sans-serif; }\
  49. .skill-value-none { font-weight: normal; }\
  50. .skill-value-low { color: blue; }\
  51. .progress-column { text-align: center; width: 135px; }\
  52. .expander { margin-left: 5px; border-radius: 8px; height: 16px; width: 16px; font-size: 8px; font-weight:bold; background: gold; background: linear-gradient(to top, #af9f39, #fffbca); border: #af9f39; border-style: outset; border-width: 1px; position: relative; top: -3px; }';
  53.  
  54. var expandCaption = '>';
  55. var collapseCaption = '<';
  56. var levelStringRU = '\u0411\u043E\u0435\u0432\u043E\u0439 \u0443\u0440\u043E\u0432\u0435\u043D\u044C';
  57. var factionsGroupTitleRU = '\u0424\u0440\u0430\u043A\u0446\u0438\u044F';
  58. var guildsGroupTitleRU = '\u0413\u0438\u043B\u044C\u0434\u0438\u044F';
  59.  
  60. function round(value, precision)
  61. {
  62. if (precision > 0)
  63. {
  64. var b = precision * 10;
  65. return Math.round(value * b) / b;
  66. }
  67. else
  68. {
  69. return Math.floor(value);
  70. }
  71. }
  72.  
  73. function parseSourceCode(source)
  74. {
  75. var captions = [];
  76. var match;
  77. var cr = /((?:[a-z'\u0430-\u044F\u0451]+\s)?[a-z\u0430-\u044F\u0451]+):\s/gi;
  78. while ((match = cr.exec(source)) != null)
  79. {
  80. captions.push({ Index: cr.lastIndex, Value: match[0].toString() });
  81. }
  82. var getCaption = function(index)
  83. {
  84. for (var ii = captions.length - 1; ii>= 0; ii--)
  85. {
  86. if (captions[ii].Index < index)
  87. {
  88. return captions[ii].Value;
  89. }
  90. }
  91. return null;
  92. }
  93. var result = [];
  94. var sr = /(?:\s|>)(\d+)(<\/b>|<\/a>)?\s\((\d+(?:\.\d+)?)\)/g;
  95. while((match = sr.exec(source)) != null)
  96. {
  97. result.push({
  98. Caption: getCaption(sr.lastIndex),
  99. Level: +match[1].toString(),
  100. Score: +match[3].toString(),
  101. Sign: match[2]
  102. });
  103. }
  104. return result;
  105. }
  106. function addStyles()
  107. {
  108. var style = document.createElement('style');
  109. style.type = 'text/css';
  110. style.appendChild(document.createTextNode(Styles));
  111. document.head.appendChild(style);
  112. }
  113.  
  114. function checkScale(scale)
  115. {
  116. if (scale && scale.length > 0)
  117. {
  118. if (scale.length > 1)
  119. {
  120. for (var ii = 1; ii < scale.length; ii++)
  121. {
  122. if (scale[ii] <= scale[ii - 1])
  123. {
  124. return false;
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. }
  131.  
  132. function createProgressBar(points, left, percentage)
  133. {
  134. var border = document.createElement('div');
  135. border.className = 'pb';
  136. var scale = document.createElement('div');
  137. scale.className = 'pb-scale';
  138. scale.style.width = percentage.toFixed(0) + '%';
  139. var valueDiv = document.createElement('div');
  140. valueDiv.appendChild(document.createTextNode(points));
  141. valueDiv.className = 'pb-text pb-side-text';
  142. var l = document.createElement('span');
  143. l.style.fontSize = 'smaller';
  144. l.appendChild(document.createTextNode('+' + left));
  145. valueDiv.appendChild(l);
  146. scale.appendChild(valueDiv);
  147. var percentageDiv = document.createElement('div');
  148. percentageDiv.className = 'pb-text pb-front-text';
  149. percentageDiv.appendChild(document.createTextNode(scale.style.width));
  150. scale.appendChild(percentageDiv);
  151. border.appendChild(scale);
  152. return border;
  153. }
  154.  
  155. function getScoreRange(score, scale)
  156. {
  157. if (!checkScale(scale))
  158. {
  159. return;
  160. }
  161. var initialValue = 0;
  162. var finalValue = score;
  163. var level = 0;
  164. for (var ii = 0; ii < scale.length; ii++)
  165. {
  166. if (score >= scale[ii])
  167. {
  168. initialValue = scale[ii];
  169. }
  170. else
  171. {
  172. finalValue = scale[ii];
  173. level = ii;
  174. break;
  175. }
  176. }
  177. return { Initial: initialValue, Final: finalValue, Level: level };
  178. }
  179.  
  180. function getProgressPercentage(range, score)
  181. {
  182. if (range)
  183. {
  184. return (score - range.Initial) * 100 / (range.Final - range.Initial);
  185. }
  186. return 0;
  187. }
  188.  
  189. function getScale(index)
  190. {
  191. return Scales[index];
  192. }
  193.  
  194. function getCaption(value, exclude)
  195. {
  196. if (exclude)
  197. {
  198. var r = new RegExp(exclude, 'i');
  199. return value.Caption.replace(r, '');
  200. }
  201. return value.Caption;
  202. }
  203.  
  204. function createRow(value, scaleIndex, excludeCaption)
  205. {
  206. var scale = getScale(scaleIndex);
  207. var range = getScoreRange(value.Score, scale);
  208. var row = document.createElement('tr');
  209. var c1 = document.createElement('td');
  210. c1.className = 'key-column';
  211. if (scaleIndex == 1 && value.Sign)
  212. {
  213. c1.style = 'font-weight: bold; text-decoration:underline; ';
  214. }
  215. c1.appendChild(document.createTextNode(getCaption(value, excludeCaption)));
  216. var lb = document.createElement('b');
  217. lb.className = 'skill-value';
  218. lb.appendChild(document.createTextNode(value.Level));
  219. if (value.Score >= range.Final)
  220. {
  221. lb.className = 'skill-value-complete';
  222. }
  223. else if (!value.Score)
  224. {
  225. lb.className = 'skill-value-none';
  226. }
  227. c1.appendChild(lb);
  228. row.appendChild(c1);
  229. var c2 = document.createElement('td');
  230. c2.className = 'progress-column';
  231. if (value.Score == 0)
  232. {
  233. c2.appendChild(document.createTextNode('-'));
  234. }
  235. else if (value.Score >= range.Final)
  236. {
  237. c2.appendChild(document.createTextNode(value.Score));
  238. }
  239. else
  240. {
  241. var percentage = getProgressPercentage(range, value.Score);
  242. var points = value.Score;
  243. var left = round(range.Final - value.Score, 1);
  244. if (range.Level > value.Level)
  245. {
  246. percentage = 100;
  247. left = '0.1';
  248. var rl = document.createElement('small');
  249. rl.innerHTML = - (range.Level - value.Level);
  250. lb.className = 'skill-value-low';
  251. c1.appendChild(rl);
  252. }
  253. var pb = createProgressBar(points, left, percentage);
  254. c2.appendChild(pb);
  255. }
  256. row.appendChild(c2);
  257. return row;
  258. }
  259.  
  260. function createExpanderButton()
  261. {
  262. var cb = document.createElement('input');
  263. cb.type = 'button';
  264. cb.value = expandCaption;
  265. cb.className = 'expander';
  266. cb.collapsed = true;
  267. cb.onclick = function(event)
  268. {
  269. var r = this.parentNode.parentNode;
  270. var display = this.collapsed ? '' : 'none';
  271. r.nextSibling.style.display = display;
  272. r.nextSibling.nextSibling.style.display = display;
  273. r.nextSibling.nextSibling.nextSibling.style.display = display;
  274. this.collapsed = !this.collapsed;
  275. this.value = this.collapsed ? expandCaption : collapseCaption;
  276. };
  277. return cb;
  278. }
  279.  
  280. function insertExpander(table)
  281. {
  282. var row = table.lastChild;
  283. for (var ii = 0; ii < 3; ii++)
  284. {
  285. row.firstChild.style = 'padding-left: 15px;';
  286. row.style.display = 'none';
  287. row = row.previousSibling;
  288. }
  289. row.firstChild.appendChild(createExpanderButton());
  290. }
  291.  
  292. function getScaleIndex(index)
  293. {
  294. if (index < factionCount)
  295. {
  296. return 1;
  297. }
  298. var result = index - factionCount + 2;
  299. if (result >= Scales.length)
  300. {
  301. result = Scales.length - 1;
  302. }
  303. return result;
  304. }
  305.  
  306. function isEn()
  307. {
  308. return /^www\.lordswm\.com/.test(location.host);
  309. }
  310.  
  311. function createSkillTable(caption)
  312. {
  313. var result = document.createElement('table');
  314. result.className = 'skill-table';
  315. var cpt = document.createElement('caption');
  316. cpt.appendChild(document.createTextNode(caption));
  317. result.appendChild(cpt);
  318. return result;
  319. }
  320.  
  321. function replaceSkills()
  322. {
  323. var home = document.getElementById('home_2');
  324. if (home)
  325. {
  326. var mainNode = home.parentNode;
  327. var items = parseSourceCode(mainNode.innerHTML.toString());
  328. var range = document.createRange();
  329. range.selectNodeContents(mainNode);
  330. range.deleteContents();
  331. range.detach();
  332. var t = createSkillTable(isEn() ? 'Factions' : factionsGroupTitleRU);
  333. var excludeCaption;
  334. var ii = 0;
  335. var factionPoints = 0;
  336. do
  337. {
  338. if (ii < factionCount)
  339. {
  340. factionPoints += items[ii].Score
  341. }
  342. t.appendChild(createRow(items[ii], getScaleIndex(ii), excludeCaption));
  343. ii++;
  344. if (ii == factionCount)
  345. {
  346. var sum = document.createElement('span');
  347. sum.appendChild(document.createTextNode(' \u03A3'));
  348. var sub = document.createElement('sub');
  349. sub.appendChild(document.createTextNode(isEn() ? 'all' : 'общ.'));
  350. sum.appendChild(sub);
  351. sum.appendChild(document.createTextNode('= ' + round(factionPoints, 2)));
  352. t.firstChild.appendChild(sum);
  353. mainNode.appendChild(t);
  354. t = createSkillTable(isEn() ? 'Guilds' : guildsGroupTitleRU);
  355. excludeCaption = "('\\sguild)|(" + guildsGroupTitleRU + ')';
  356. }
  357. }
  358. while (ii < items.length - 2);
  359. insertExpander(t);
  360. mainNode.appendChild(t);
  361. }
  362. }
  363.  
  364. function getLevelPoints(points)
  365. {
  366. var r = /\(([\d\.]+)\)(?:\s\+(-?[\d\.]+))?/;
  367. var m = r.exec(points)
  368. if (m)
  369. {
  370. return { Points: m[1], Left: m[2] }
  371. }
  372. }
  373.  
  374. function insertLevelUpProgressBar()
  375. {
  376. var bs= document.getElementsByTagName('b');
  377. var lbReg = new RegExp('(?:Combat\\slevel|' + levelStringRU + '):\\s(\\d+)');
  378. for (var ii = 0; ii < bs.length; ii++)
  379. {
  380. var b = bs[ii];
  381. var m = lbReg.exec(b.innerHTML);
  382. if (m)
  383. {
  384. var start = b.nextSibling;
  385. var end = start;
  386. do
  387. {
  388. end = end.nextSibling;
  389. }
  390. while (end && (!end.tagName || end.tagName.toLowerCase() != 'br'));
  391. var next = end.nextSibling;
  392. if (end)
  393. {
  394. var pointsText;
  395. var range = document.createRange();
  396. range.setStart(start, 0);
  397. range.setEnd(end, 0);
  398. pointsText = range.toString();
  399. var lvl = getLevelPoints(range.toString());
  400. if (lvl)
  401. {
  402. var r = getScoreRange(lvl.Points, getScale(0));
  403. if (r && r.Final > lvl.Points)
  404. {
  405. var percentage = getProgressPercentage(r, lvl.Points);
  406. if (r.Level > m[1])
  407. {
  408. b.appendChild(document.createTextNode('+'));
  409. percentage = 100;
  410. }
  411. var pb = createProgressBar(lvl.Points, lvl.Left, percentage);
  412. pb.className += ' levelpb';
  413. b.parentNode.insertBefore(pb, b.nextSibling);
  414. range.deleteContents();
  415. }
  416. }
  417. range.detach();
  418. break;
  419. }
  420. }
  421. }
  422. }
  423.  
  424. function main()
  425. {
  426. addStyles();
  427. insertLevelUpProgressBar();
  428. replaceSkills();
  429. }
  430.  
  431. main();
  432.  
  433. // ==/UserScript==