Elethor General Purpose

Provides some general additions to Elethor

目前为 2021-01-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Elethor General Purpose
  3. // @description Provides some general additions to Elethor
  4. // @namespace https://www.elethor.com/
  5. // @version 1.0.6
  6. // @author Anders Morgan Larsen (Xortrox)
  7. // @match https://elethor.com/*
  8. // @match https://www.elethor.com/*
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. const moduleName = 'Elethor General Purpose';
  15.  
  16.  
  17. const playerHealthBarElementDefinition = '.progress.is-medium.is-danger';
  18. const playerHealthBarTextElementDefinition = '.is-fight-health';
  19.  
  20. function getBattleHealthPercentage() {
  21. const playerHealth = document.querySelector(playerHealthBarElementDefinition);
  22.  
  23. if (!playerHealth) {
  24. return 0;
  25. }
  26.  
  27. return playerHealth.value / playerHealth.max * 100;
  28. }
  29.  
  30. function initializeUpdateBattleHealthPercentage() {
  31. if (window.elethorExtrasInterval) {
  32. clearInterval(window.elethorExtrasInterval);
  33. }
  34.  
  35. window.elethorExtrasInterval = setInterval(() => {
  36. const playerHealthText = document.querySelector(playerHealthBarTextElementDefinition);
  37. const healthPercentage = getBattleHealthPercentage();
  38.  
  39. if (playerHealthText && healthPercentage && !isNaN(healthPercentage)) {
  40. let percentageDisplay;
  41. if (playerHealthText.children.length === 0) {
  42. percentageDisplay = document.createElement('span');
  43.  
  44. playerHealthText.appendChild(percentageDisplay);
  45. } else {
  46. percentageDisplay = playerHealthText.children[0];
  47.  
  48. }
  49.  
  50. if (percentageDisplay) {
  51. percentageDisplay.innerText = ` (${healthPercentage.toFixed(3)}%)`;
  52. }
  53. }
  54. }, 500);
  55.  
  56.  
  57. console.log(`[${moduleName}] Battle Percentage initialized.`);
  58. }
  59.  
  60. function initializeToastKiller() {
  61. document.addEventListener('click', function(e) {
  62. if (e.target
  63. && e.target.className
  64. && e.target.className.includes
  65. && e.target.className.includes('toasted')
  66. ) {
  67. e.target.remove();
  68. }
  69. });
  70.  
  71. console.log(`[${moduleName}] Toast Killer initialized.`);
  72. }
  73.  
  74. function initializeXHRHook() {
  75. let rawOpen = XMLHttpRequest.prototype.open;
  76.  
  77. XMLHttpRequest.prototype.open = function() {
  78. if (!this._hooked) {
  79. this._hooked = true;
  80. setupHook(this);
  81. }
  82. rawOpen.apply(this, arguments);
  83. }
  84.  
  85. function setupHook(xhr) {
  86. if (window.elethorGeneralPurposeOnUserLoad) {
  87. const e = new Event('EGPXHR');
  88. e.xhr = xhr;
  89.  
  90. window.elethorGeneralPurposeOnUserLoad.dispatchEvent(e);
  91. }
  92. }
  93. window.elethorGeneralPurposeOnUserLoad = new EventTarget();
  94.  
  95. console.log(`[${moduleName}] XHR Hook initialized.`);
  96. }
  97.  
  98. function initializeUserLoadListener() {
  99. elethorGeneralPurposeOnUserLoad.addEventListener('EGPXHR', function (e) {
  100. if (e && e.xhr && e.xhr.responseURL.endsWith('/game/user')) {
  101. console.log('User load request:', e);
  102. }
  103. });
  104.  
  105. console.log(`[${moduleName}] User Load Listener initialized.`);
  106. }
  107.  
  108. (function run() {
  109. initializeUpdateBattleHealthPercentage();
  110. initializeToastKiller();
  111. initializeXHRHook();
  112. initializeUserLoadListener();
  113.  
  114. console.log(`[${moduleName}] Loaded.`);
  115. })();
  116. })();
  117.