hwm_art_durability.user.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // ==UserScript==
  2. // @name HWM - Artifact durability
  3. // @namespace figey_hwm_
  4. // @description Show artefact durability
  5. // @version 0.1.0
  6. // @run-at document-end
  7. // @noframe
  8. // @include /^https?:\/\/(www\.)?(heroeswm\.ru|lordswm\.com)\/home\.php.*/
  9. // @include /^https?:\/\/(www\.)?(heroeswm\.ru|lordswm\.com)\/pl_info\.php.*/
  10. // @include /^https?:\/\/(www\.)?(heroeswm\.ru|lordswm\.com)\/inventory\.php.*/
  11. // ==/UserScript==
  12. /**
  13. * Find parent element by tag name
  14. * @param node base element
  15. * @param tag parent element type
  16. * @param count how many parents we must skip
  17. * @return parent for custom node or null if no parent
  18. */
  19. function getParentByTagName(node, tag, count) {
  20. if( typeof node !== 'object' || node.tagName == 'HTML'
  21. || typeof tag !== 'string' || tag == ''
  22. || typeof count !== 'number' || count <= 0
  23. ){
  24. console.error('Invalid call function ' + this.name);
  25. return null;
  26. }
  27. let parent = node.parentNode;
  28. tag = tag.toUpperCase();
  29. while (parent.tagName !== "HTML") {
  30. if (parent.tagName === tag) {
  31. if( count == 1 ){
  32. return parent;
  33. } else {
  34. count--;
  35. }
  36. }
  37. parent = parent.parentNode;
  38. }
  39. if( parent.tagName !== tag ){
  40. console.error("Failed found parent for:");
  41. console.error(node);
  42. return null;
  43. }
  44. return parent;
  45. }
  46. getParentByTagName = getParentByTagName.bind(getParentByTagName);
  47. /**
  48. * Add element with durability status into artifact image
  49. * @param element artifact image
  50. * @param durability artefact durability
  51. */
  52. function addDurabilityInfo(element, durability){
  53. const span = document.createElement('div');
  54. span.style = 'position: absolute; z-index: 5; font-size: 14px; font-weight:bold; padding: 0px 2px; border-radius: 0% 0% 50% 0%;';
  55. span.textContent = durability.current;
  56. span.style.textShadow = "1px 0 1px #dabd88, 0 1px 1px #dabd88, -1px 0 1px #dabd88, 0 -1px 1px #dabd88, \
  57. 2px 0 2px #dabd88, 0 2px 2px #dabd88, -2px 0 2px #dabd88, 0 -2px 2px #dabd88";
  58. element.prepend(span);
  59. }
  60. /**
  61. * Add alert for low durability arts
  62. * @param element artifact image
  63. * @param durability artefact durability
  64. */
  65. function addDurabilityAlert(element, durability){
  66. if( durability.current == 0 ){
  67. element.querySelector('img[src*="artifact"]').style.backgroundColor = 'rgba(128,128,128,0.65)';
  68. } else if( durability.current < 7 && durability.current < durability.max / 2 ){
  69. element.querySelector('img[src*="artifact"]').style.backgroundColor = 'rgba(255,0,0,' + (0.58 - 0.08*durability.current) + ')';
  70. }
  71. }
  72. /**
  73. * Extract durability info for artifact
  74. * @param element articaft element
  75. * @return artefact durability, current and maximum
  76. */
  77. function getDurability(element){
  78. const dur = element.querySelector('img').title.match(/(\d+)\/(\d+)/);
  79. if( dur ){
  80. return { current: dur[1], max: dur[2] };
  81. } else {
  82. return null;
  83. }
  84. }
  85. /**
  86. * Get artefact list from page
  87. * @return array with artefact elements
  88. */
  89. function getArtifacts(){
  90. const result = Array();
  91. document.querySelectorAll('div img[title*="Прочность"]').forEach(function(img){
  92. const parent = getParentByTagName(img, 'div', 1);
  93. if( !result.includes(parent) ){
  94. result.push(parent);
  95. }
  96. });
  97. return result;
  98. }
  99. /**
  100. * Player info && main page
  101. * Inventory(first load)
  102. */
  103. getArtifacts().forEach(function(art){
  104. const durability = getDurability(art)
  105. addDurabilityInfo(art, durability);
  106. addDurabilityAlert(art, durability);
  107. });
  108. /**
  109. * Inventory(only after update)
  110. * NOTE
  111. * arts_fud take of
  112. * arts_fd_ok wear
  113. * arts_fd_none cant wear
  114. */
  115. if( location.pathname == '/inventory.php' ){
  116. const template = document.createElement('template');
  117. [arts_fud, arts_fd_ok, arts_fd_none].forEach(function(artList){
  118. for( let i = 0; i < artList.length; ++i){
  119. if( artList[i] ){
  120. template.innerHTML = artList[i];
  121. const artNode = template.content.firstChild;
  122. const art = artNode.querySelector('div.arts_info');
  123. const durability = getDurability(art);
  124. addDurabilityInfo(art, durability);
  125. addDurabilityAlert(art, durability);
  126. artList[i] = template.innerHTML;
  127. }
  128. }
  129. });
  130. }