WAX CPU Usage Indicator

Show WAX CPU percentage used in top left corner

当前为 2022-06-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WAX CPU Usage Indicator
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  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 walletAddressKey = 'user-script-wallet-address';
  17. let walletAddress = localStorage.getItem(walletAddressKey) || undefined;
  18.  
  19. const cpuDivId = 'user-script-cpu';
  20. const walletInputId = 'user-script-wallet-address';
  21. const walletSaveButtonId = 'user-script-wallet-save';
  22.  
  23. const cpuUpdateIntervalMS = 60000;
  24.  
  25. async function getWaxAccount(accountName) {
  26. return (await fetch('https://chain.wax.io/v1/chain/get_account', { method: 'POST', body: JSON.stringify({account_name: accountName}) })).json();
  27. }
  28.  
  29. async function getWaxCPUPercentage(accountName) {
  30. const account = await getWaxAccount(accountName);
  31.  
  32. return (account.cpu_limit.used / account.cpu_limit.max * 100).toFixed(2);
  33. }
  34.  
  35. function touchCPUDiv() {
  36. const exists = document.getElementById(cpuDivId);
  37.  
  38. if (!exists) {
  39. const div = document.createElement('div');
  40. div.id = cpuDivId;
  41. 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;');
  42.  
  43. const walletSaveButton = document.createElement('button');
  44. walletSaveButton.id = walletSaveButtonId;
  45. walletSaveButton.setAttribute('style', 'background: transparent; color: limegreen; height: 25px; cursor: pointer;');
  46. walletSaveButton.innerText = 'Save';
  47. walletSaveButton.addEventListener('click', () => {
  48. const walletInput = document.getElementById(walletInputId);
  49. localStorage.setItem(walletAddressKey, walletInput.value);
  50. walletAddress = localStorage.getItem(walletAddressKey) || undefined;
  51. updateCPUPercentage();
  52. });
  53.  
  54. const walletInput = document.createElement('input');
  55. walletInput.id = walletInputId;
  56. walletInput.setAttribute('style', 'width: 100px; background: transparent; color: limegreen; height: 25px;');
  57. walletInput.setAttribute('data-lpignore', 'true');
  58. walletInput.setAttribute('type', 'text');
  59. walletInput.setAttribute('name', 'user-script-wallet');
  60. walletInput.setAttribute('placeholder', 'Wallet Address');
  61. walletInput.setAttribute('value', walletAddress);
  62. walletInput.addEventListener('input', () => {
  63. console.log('input', walletInput.value);
  64. });
  65. walletInput.addEventListener('blur', () => {
  66. console.log('blur', walletInput.value);
  67. });
  68.  
  69. div.appendChild(walletSaveButton);
  70.  
  71. div.appendChild(walletInput);
  72.  
  73. const cpuSpan = document.createElement('span');
  74. cpuSpan.id = cpuDivId + '-span';
  75. cpuSpan.setAttribute('style', 'padding: 3px;');
  76.  
  77. div.appendChild(cpuSpan);
  78.  
  79. div.addEventListener('click', () => {
  80. updateCPUPercentage();
  81. });
  82.  
  83. document.body.appendChild(div);
  84. }
  85.  
  86. return document.getElementById(cpuDivId + '-span');
  87. }
  88.  
  89. async function updateCPUPercentage() {
  90. const div = touchCPUDiv();
  91.  
  92. if (walletAddress) {
  93. div.innerText = `CPU usage: ${await getWaxCPUPercentage(walletAddress)}%`;
  94. }
  95. }
  96.  
  97. updateCPUPercentage();
  98.  
  99. setInterval(updateCPUPercentage, cpuUpdateIntervalMS);
  100. })();