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.3
  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. if (percentageDisplay) {
  49. percentageDisplay.innerText = ` (${healthPercentage.toFixed(3)}%)`;
  50. }
  51. }
  52. }, 500);
  53.  
  54. console.log(`[${moduleName}] Battle Percentage initialized.`);
  55. }
  56.  
  57. function initializeToastKiller() {
  58. document.addEventListener('click', function(e) {
  59. if (e.target && e.target.className && e.target.className.includes('toasted')) {
  60. e.target.remove();
  61. }
  62. });
  63. console.log(`[${moduleName}] Toast Killer initialized.`);
  64. }
  65.  
  66. function initializeXHRHook() {
  67. let rawOpen = XMLHttpRequest.prototype.open;
  68.  
  69. XMLHttpRequest.prototype.open = function() {
  70. if (!this._hooked) {
  71. this._hooked = true;
  72. setupHook(this);
  73. }
  74. rawOpen.apply(this, arguments);
  75. }
  76.  
  77. function setupHook(xhr) {
  78. if (window.elethorGeneralPurposeOnUserLoad) {
  79. const e = new Event('EGPXHR');
  80. e.xhr = xhr;
  81.  
  82. window.elethorGeneralPurposeOnUserLoad.dispatchEvent(e);
  83. }
  84. }
  85. window.elethorGeneralPurposeOnUserLoad = new EventTarget();
  86.  
  87. elethorGeneralPurposeOnUserLoad.addEventListener('EGPXHR', function (e) {
  88. console.log('xhr event:', e);
  89. });
  90. console.log(`[${moduleName}] XHR Hook initialized.`);
  91. }
  92.  
  93. (function run() {
  94. initializeUpdateBattleHealthPercentage();
  95. initializeToastKiller();
  96. initializeXHRHook();
  97. console.log(`[${moduleName}] Loaded.`);
  98. })();
  99. })();
  100.