LWM_ProgressBars

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

目前为 2014-09-10 提交的版本。查看 最新版本

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