WAX CPU Usage Indicator

Show WAX CPU percentage used in top left corner

  1. // ==UserScript==
  2. // @name WAX CPU Usage Indicator
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.5
  5. // @description Show WAX CPU percentage used in top left corner
  6. // @author Xortrox
  7. // @esversion: 6
  8. // @match https://play.arenaofglory.io/*
  9. // @icon https://play.arenaofglory.io/favicon.ico
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (async function() {
  14. 'use strict';
  15.  
  16. const API_URL = 'https://nftgaming.global.binfra.one/v1/chain';
  17.  
  18. const walletAddressKey = 'user-script-wallet-address';
  19. let walletAddress = localStorage.getItem(walletAddressKey) || undefined;
  20.  
  21. const cpuDivId = 'user-script-cpu';
  22. const walletInputId = 'user-script-wallet-address';
  23. const walletSaveButtonId = 'user-script-wallet-save';
  24.  
  25. const cpuUpdateIntervalMS = 60000 * 5;
  26.  
  27. async function getWaxAccount(accountName) {
  28. return (await fetch(`${API_URL}/get_account`, { method: 'POST', body: JSON.stringify({account_name: accountName}) })).json();
  29. }
  30.  
  31. async function getWaxCPUPercentage(accountName) {
  32. const account = await getWaxAccount(accountName);
  33.  
  34. return (account.cpu_limit.used / account.cpu_limit.max * 100).toFixed(2);
  35. }
  36.  
  37. function touchCPUDiv() {
  38. const exists = document.getElementById(cpuDivId);
  39.  
  40. if (!exists) {
  41. const div = document.createElement('div');
  42. div.id = cpuDivId;
  43. div.setAttribute('style', 'position: fixed; left: 5px; top: 5px; height: 25px; background: rgba(1,1,1,0.7); border: 1px solid limegreen; color: limegreen; padding: 3px; cursor: pointer;');
  44.  
  45. const walletSaveButton = document.createElement('button');
  46. walletSaveButton.id = walletSaveButtonId;
  47. walletSaveButton.setAttribute('style', 'background: transparent; color: limegreen; height: 25px; cursor: pointer;');
  48. walletSaveButton.innerText = 'Save';
  49. walletSaveButton.addEventListener('click', () => {
  50. const walletInput = document.getElementById(walletInputId);
  51. localStorage.setItem(walletAddressKey, walletInput.value);
  52. walletAddress = localStorage.getItem(walletAddressKey) || undefined;
  53. updateCPUPercentage();
  54. });
  55.  
  56. const walletInput = document.createElement('input');
  57. walletInput.id = walletInputId;
  58. walletInput.setAttribute('style', 'width: 100px; background: transparent; color: limegreen; height: 25px;');
  59. walletInput.setAttribute('data-lpignore', 'true');
  60. walletInput.setAttribute('type', 'text');
  61. walletInput.setAttribute('name', 'user-script-wallet');
  62. walletInput.setAttribute('placeholder', 'Wallet Address');
  63. walletInput.setAttribute('value', walletAddress);
  64. walletInput.addEventListener('input', () => {
  65. console.log('input', walletInput.value);
  66. });
  67. walletInput.addEventListener('blur', () => {
  68. console.log('blur', walletInput.value);
  69. });
  70.  
  71. div.appendChild(walletSaveButton);
  72.  
  73. div.appendChild(walletInput);
  74.  
  75. const cpuSpan = document.createElement('span');
  76. cpuSpan.id = cpuDivId + '-span';
  77. cpuSpan.setAttribute('style', 'padding: 3px;');
  78.  
  79. div.appendChild(cpuSpan);
  80.  
  81. div.addEventListener('click', () => {
  82. updateCPUPercentage();
  83. });
  84.  
  85. document.body.appendChild(div);
  86. }
  87.  
  88. return document.getElementById(cpuDivId + '-span');
  89. }
  90.  
  91. async function updateCPUPercentage() {
  92. if (!getUserScriptGlobals().gameVisible) {
  93. console.log('[AoG User Script]: Skipping CPU update as game was hidden.');
  94. return;
  95. }
  96.  
  97. const div = touchCPUDiv();
  98.  
  99. if (walletAddress) {
  100. div.innerText = `CPU usage: ${await getWaxCPUPercentage(walletAddress)}%`;
  101. }
  102. }
  103.  
  104. function getUserScriptGlobals() {
  105. if (!window.UserScript) {
  106. window.UserScript = { gameVisible: true };
  107. }
  108.  
  109. return window.UserScript;
  110. }
  111.  
  112. function registerVisibilityChange() {
  113. document.addEventListener("visibilitychange", (event) => {
  114. const hidden = event?.target?.hidden
  115. getUserScriptGlobals().gameVisible = hidden !== undefined && hidden !== true;
  116. });
  117. }
  118.  
  119. registerVisibilityChange();
  120.  
  121. updateCPUPercentage();
  122.  
  123. setInterval(updateCPUPercentage, cpuUpdateIntervalMS);
  124. })();