Advertisement
Josiahiscool73

Performance and Keystrokes/Mouse Display in JS

Apr 16th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.41 KB | None | 0 0
  1. // Advanced Performance Monitor with Movable UI
  2. // Usage: Copy this entire script into your browser's console and press Enter
  3.  
  4. (function() {
  5. // Configuration
  6. const config = {
  7. keysToShow: ['KeyW', 'KeyA', 'KeyS', 'KeyD', 'ShiftLeft', 'ControlLeft', 'KeyQ', 'KeyZ', 'KeyX', 'KeyC', 'KeyV', 'KeyF', 'Digit1', 'Digit2', 'Digit3', 'Digit4', 'Digit5', 'Digit6'],
  8. keyboardLayout: {
  9. row1: ['Digit1', 'Digit2', 'Digit3', 'Digit4', 'Digit5', 'Digit6'],
  10. row2: ['KeyQ', 'KeyW', 'KeyE', 'KeyF'],
  11. row3: ['KeyA', 'KeyS', 'KeyD'],
  12. row4: ['KeyZ', 'KeyX', 'KeyC', 'KeyV'],
  13. modifiers: ['ShiftLeft', 'ControlLeft']
  14. },
  15. keyLabels: {
  16. 'KeyW': 'W', 'KeyA': 'A', 'KeyS': 'S', 'KeyD': 'D',
  17. 'ShiftLeft': 'SHIFT', 'ControlLeft': 'CTRL',
  18. 'KeyQ': 'Q', 'KeyZ': 'Z', 'KeyX': 'X', 'KeyC': 'C', 'KeyV': 'V', 'KeyF': 'F',
  19. 'Digit1': '1', 'Digit2': '2', 'Digit3': '3', 'Digit4': '4', 'Digit5': '5', 'Digit6': '6'
  20. },
  21. toggleUIKey: 'Home',
  22. lockUnlockKey: 'End'
  23. };
  24.  
  25. // State
  26. let locked = true;
  27. let uiVisible = true;
  28. let activeModules = {
  29. keyboard: true,
  30. mouse: true,
  31. mouseCoords: true,
  32. fps: true,
  33. ping: true
  34. };
  35.  
  36. // Create styles
  37. const styleEl = document.createElement('style');
  38. styleEl.textContent = `
  39. .perf-monitor-module {
  40. position: fixed;
  41. background-color: rgba(0, 0, 0, 0.7);
  42. color: #fff;
  43. padding: 10px;
  44. border-radius: 5px;
  45. font-family: monospace;
  46. z-index: 9999;
  47. min-width: 120px;
  48. user-select: none;
  49. transition: border 0.3s;
  50. }
  51. .perf-monitor-module.unlocked {
  52. border: 1px solid #666;
  53. cursor: move;
  54. }
  55. .perf-monitor-module.locked {
  56. border: 1px solid transparent;
  57. }
  58. .perf-monitor-title {
  59. font-weight: bold;
  60. border-bottom: 1px solid #666;
  61. margin-bottom: 5px;
  62. padding-bottom: 2px;
  63. display: flex;
  64. justify-content: space-between;
  65. align-items: center;
  66. }
  67. .perf-monitor-close {
  68. cursor: pointer;
  69. color: #ff6666;
  70. font-size: 16px;
  71. line-height: 14px;
  72. }
  73. .perf-keyboard {
  74. display: flex;
  75. flex-direction: column;
  76. gap: 5px;
  77. }
  78. .perf-keyboard-row {
  79. display: flex;
  80. gap: 5px;
  81. justify-content: center;
  82. }
  83. .perf-key {
  84. border: 1px solid #666;
  85. border-radius: 3px;
  86. padding: 5px;
  87. min-width: 20px;
  88. text-align: center;
  89. background-color: #333;
  90. box-shadow: 0 2px 0 #222;
  91. transition: all 0.1s;
  92. }
  93. .perf-key.active {
  94. background-color: #80ff80;
  95. color: #000;
  96. transform: translateY(2px);
  97. box-shadow: 0 0 0 #222;
  98. }
  99. .perf-modifiers {
  100. display: flex;
  101. justify-content: space-between;
  102. margin-top: 5px;
  103. }
  104. .perf-mouse-display {
  105. width: 80px;
  106. height: 120px;
  107. background-color: #333;
  108. border-radius: 40px;
  109. position: relative;
  110. margin: 10px auto;
  111. overflow: hidden;
  112. }
  113. .perf-mouse-button {
  114. position: absolute;
  115. width: 40px;
  116. height: 30px;
  117. background-color: #444;
  118. border-radius: 5px 5px 0 0;
  119. transition: all 0.1s;
  120. }
  121. .perf-mouse-button.left {
  122. left: 0;
  123. top: 0;
  124. }
  125. .perf-mouse-button.right {
  126. right: 0;
  127. top: 0;
  128. }
  129. .perf-mouse-button.active {
  130. background-color: #80ff80;
  131. }
  132. .perf-mouse-wheel {
  133. position: absolute;
  134. width: 10px;
  135. height: 15px;
  136. background-color: #666;
  137. left: 50%;
  138. top: 15px;
  139. transform: translateX(-50%);
  140. border-radius: 5px;
  141. }
  142. .perf-config-panel {
  143. position: fixed;
  144. left: 50%;
  145. top: 50%;
  146. transform: translate(-50%, -50%);
  147. background-color: rgba(0, 0, 0, 0.9);
  148. padding: 20px;
  149. border-radius: 5px;
  150. z-index: 10000;
  151. min-width: 300px;
  152. }
  153. .perf-config-title {
  154. font-size: 16px;
  155. margin-bottom: 15px;
  156. text-align: center;
  157. }
  158. .perf-config-option {
  159. margin-bottom: 10px;
  160. }
  161. .perf-config-buttons {
  162. display: flex;
  163. justify-content: space-between;
  164. margin-top: 20px;
  165. }
  166. .perf-config-button {
  167. background: #333;
  168. border: 1px solid #666;
  169. color: white;
  170. padding: 5px 10px;
  171. cursor: pointer;
  172. border-radius: 3px;
  173. }
  174. `;
  175. document.head.appendChild(styleEl);
  176.  
  177. // Create modules
  178. function createModule(id, title, initialX, initialY) {
  179. const module = document.createElement('div');
  180. module.className = `perf-monitor-module ${locked ? 'locked' : 'unlocked'}`;
  181. module.id = id;
  182. module.style.left = `${initialX}px`;
  183. module.style.top = `${initialY}px`;
  184.  
  185. const titleBar = document.createElement('div');
  186. titleBar.className = 'perf-monitor-title';
  187. titleBar.innerHTML = `${title} <span class="perf-monitor-close">×</span>`;
  188.  
  189. const content = document.createElement('div');
  190. content.className = 'perf-monitor-content';
  191.  
  192. module.appendChild(titleBar);
  193. module.appendChild(content);
  194. document.body.appendChild(module);
  195.  
  196. // Make module draggable
  197. let isDragging = false;
  198. let offsetX, offsetY;
  199.  
  200. titleBar.addEventListener('mousedown', (e) => {
  201. if (locked) return;
  202. isDragging = true;
  203. offsetX = e.clientX - module.getBoundingClientRect().left;
  204. offsetY = e.clientY - module.getBoundingClientRect().top;
  205. });
  206.  
  207. document.addEventListener('mousemove', (e) => {
  208. if (!isDragging) return;
  209. module.style.left = `${e.clientX - offsetX}px`;
  210. module.style.top = `${e.clientY - offsetY}px`;
  211. });
  212.  
  213. document.addEventListener('mouseup', () => {
  214. isDragging = false;
  215. });
  216.  
  217. // Close button
  218. const closeBtn = titleBar.querySelector('.perf-monitor-close');
  219. closeBtn.addEventListener('click', () => {
  220. const moduleId = module.id.replace('perf-monitor-', '');
  221. activeModules[moduleId] = false;
  222. module.style.display = 'none';
  223. });
  224.  
  225. return content;
  226. }
  227.  
  228. // Create modules
  229. let keyboardModule, mouseModule, mouseCoordsModule, fpsModule, pingModule;
  230.  
  231. function createModules() {
  232. // Create keyboard module (top right)
  233. if (activeModules.keyboard) {
  234. keyboardModule = createModule('perf-monitor-keyboard', 'Keyboard', window.innerWidth - 240, 20);
  235. keyboardModule.innerHTML = `<div class="perf-keyboard"></div>`;
  236. renderKeyboard();
  237. }
  238.  
  239. // Create mouse visual module (below keyboard)
  240. if (activeModules.mouse) {
  241. mouseModule = createModule('perf-monitor-mouse', 'Mouse', window.innerWidth - 180, 220);
  242. mouseModule.innerHTML = `
  243. <div class="perf-mouse-display">
  244. <div class="perf-mouse-button left"></div>
  245. <div class="perf-mouse-button right"></div>
  246. <div class="perf-mouse-wheel"></div>
  247. </div>
  248. <div class="perf-mouse-clicks">
  249. Clicks: L: 0 | R: 0
  250. </div>
  251. `;
  252. }
  253.  
  254. // Create mouse coordinates module (bottom left)
  255. if (activeModules.mouseCoords) {
  256. mouseCoordsModule = createModule('perf-monitor-mouseCoords', 'Mouse Position', 20, window.innerHeight - 100);
  257. mouseCoordsModule.innerHTML = 'X: 0, Y: 0';
  258. }
  259.  
  260. // Create FPS module (top left)
  261. if (activeModules.fps) {
  262. fpsModule = createModule('perf-monitor-fps', 'FPS', 20, 20);
  263. fpsModule.innerHTML = '0 FPS';
  264. }
  265.  
  266. // Create ping module (top left, below FPS)
  267. if (activeModules.ping) {
  268. pingModule = createModule('perf-monitor-ping', 'Ping', 20, 100);
  269. pingModule.innerHTML = 'Ping: ---ms';
  270. }
  271. }
  272.  
  273. // Render keyboard
  274. function renderKeyboard() {
  275. if (!activeModules.keyboard) return;
  276.  
  277. const keyboardEl = keyboardModule.querySelector('.perf-keyboard');
  278. keyboardEl.innerHTML = '';
  279.  
  280. // Create rows
  281. Object.keys(config.keyboardLayout).forEach(rowKey => {
  282. if (rowKey === 'modifiers') return;
  283.  
  284. const rowEl = document.createElement('div');
  285. rowEl.className = 'perf-keyboard-row';
  286.  
  287. config.keyboardLayout[rowKey].forEach(keyCode => {
  288. if (!config.keysToShow.includes(keyCode)) return;
  289.  
  290. const keyEl = document.createElement('div');
  291. keyEl.className = 'perf-key';
  292. keyEl.dataset.keyCode = keyCode;
  293. keyEl.textContent = config.keyLabels[keyCode] || keyCode;
  294. rowEl.appendChild(keyEl);
  295. });
  296.  
  297. keyboardEl.appendChild(rowEl);
  298. });
  299.  
  300. // Create modifiers
  301. const modifiersEl = document.createElement('div');
  302. modifiersEl.className = 'perf-modifiers';
  303.  
  304. config.keyboardLayout.modifiers.forEach(keyCode => {
  305. if (!config.keysToShow.includes(keyCode)) return;
  306.  
  307. const keyEl = document.createElement('div');
  308. keyEl.className = 'perf-key';
  309. keyEl.dataset.keyCode = keyCode;
  310. keyEl.textContent = config.keyLabels[keyCode] || keyCode;
  311. keyEl.style.width = '60px';
  312. modifiersEl.appendChild(keyEl);
  313. });
  314.  
  315. keyboardEl.appendChild(modifiersEl);
  316. }
  317.  
  318. // Keyboard tracking
  319. const keyState = {};
  320.  
  321. document.addEventListener('keydown', (e) => {
  322. if (e.code === config.toggleUIKey) {
  323. toggleUI();
  324. return;
  325. }
  326.  
  327. if (e.code === config.lockUnlockKey) {
  328. toggleLock();
  329. return;
  330. }
  331.  
  332. keyState[e.code] = true;
  333. updateKeyboard();
  334. });
  335.  
  336. document.addEventListener('keyup', (e) => {
  337. keyState[e.code] = false;
  338. updateKeyboard();
  339. });
  340.  
  341. function updateKeyboard() {
  342. if (!activeModules.keyboard) return;
  343.  
  344. document.querySelectorAll('.perf-key').forEach(keyEl => {
  345. const keyCode = keyEl.dataset.keyCode;
  346. if (keyState[keyCode]) {
  347. keyEl.classList.add('active');
  348. } else {
  349. keyEl.classList.remove('active');
  350. }
  351. });
  352. }
  353.  
  354. // Mouse tracking
  355. let mouseX = 0;
  356. let mouseY = 0;
  357. let mouseClicks = { left: 0, right: 0 };
  358. let mouseButtonsState = { left: false, right: false };
  359.  
  360. document.addEventListener('mousemove', (e) => {
  361. mouseX = e.clientX;
  362. mouseY = e.clientY;
  363. updateMouseCoords();
  364. });
  365.  
  366. document.addEventListener('mousedown', (e) => {
  367. if (e.button === 0) {
  368. mouseClicks.left++;
  369. mouseButtonsState.left = true;
  370. } else if (e.button === 2) {
  371. mouseClicks.right++;
  372. mouseButtonsState.right = true;
  373. }
  374. updateMouse();
  375. });
  376.  
  377. document.addEventListener('mouseup', (e) => {
  378. if (e.button === 0) {
  379. mouseButtonsState.left = false;
  380. } else if (e.button === 2) {
  381. mouseButtonsState.right = false;
  382. }
  383. updateMouse();
  384. });
  385.  
  386. function updateMouseCoords() {
  387. if (!activeModules.mouseCoords) return;
  388. mouseCoordsModule.innerHTML = `X: ${mouseX}, Y: ${mouseY}`;
  389. }
  390.  
  391. function updateMouse() {
  392. if (!activeModules.mouse) return;
  393.  
  394. const leftBtn = mouseModule.querySelector('.perf-mouse-button.left');
  395. const rightBtn = mouseModule.querySelector('.perf-mouse-button.right');
  396. const clicksEl = mouseModule.querySelector('.perf-mouse-clicks');
  397.  
  398. if (mouseButtonsState.left) {
  399. leftBtn.classList.add('active');
  400. } else {
  401. leftBtn.classList.remove('active');
  402. }
  403.  
  404. if (mouseButtonsState.right) {
  405. rightBtn.classList.add('active');
  406. } else {
  407. rightBtn.classList.remove('active');
  408. }
  409.  
  410. clicksEl.textContent = `Clicks: L: ${mouseClicks.left} | R: ${mouseClicks.right}`;
  411. }
  412.  
  413. // FPS tracking
  414. let frameCount = 0;
  415. let lastFrameTime = performance.now();
  416. let fps = 0;
  417.  
  418. function updateFPS() {
  419. frameCount++;
  420. const now = performance.now();
  421. const delta = now - lastFrameTime;
  422.  
  423. if (delta >= 1000) {
  424. fps = Math.round((frameCount * 1000) / delta);
  425. frameCount = 0;
  426. lastFrameTime = now;
  427. displayFPS();
  428. }
  429.  
  430. requestAnimationFrame(updateFPS);
  431. }
  432.  
  433. function displayFPS() {
  434. if (!activeModules.fps) return;
  435.  
  436. const color = fps > 50 ? '#80ff80' : fps > 30 ? '#ffff80' : '#ff8080';
  437. fpsModule.innerHTML = `<span style="color: ${color}">${fps} FPS</span>`;
  438. }
  439.  
  440. // Ping tracking
  441. let pingHistory = [];
  442. const MAX_PING_HISTORY = 5;
  443. let pingInterval;
  444.  
  445. function measurePing() {
  446. if (!activeModules.ping) return;
  447.  
  448. const start = performance.now();
  449.  
  450. fetch('/favicon.ico', {
  451. method: 'HEAD',
  452. cache: 'no-store'
  453. })
  454. .then(() => {
  455. const pingTime = Math.round(performance.now() - start);
  456. pingHistory.unshift(pingTime);
  457. if (pingHistory.length > MAX_PING_HISTORY) {
  458. pingHistory.pop();
  459. }
  460. displayPing();
  461. })
  462. .catch(() => {
  463. pingHistory.unshift(-1); // Error
  464. if (pingHistory.length > MAX_PING_HISTORY) {
  465. pingHistory.pop();
  466. }
  467. displayPing();
  468. });
  469. }
  470.  
  471. function displayPing() {
  472. if (!activeModules.ping) return;
  473.  
  474. const currentPing = pingHistory[0];
  475. const avgPing = pingHistory.filter(p => p >= 0).reduce((sum, p) => sum + p, 0) /
  476. pingHistory.filter(p => p >= 0).length || 0;
  477.  
  478. let color = '#80ff80';
  479. if (currentPing > 100) color = '#ffff80';
  480. if (currentPing > 200) color = '#ff8080';
  481. if (currentPing < 0) color = '#ff0000';
  482.  
  483. pingModule.innerHTML = `
  484. <div>Current: <span style="color: ${color}">${currentPing < 0 ? 'ERR' : currentPing + 'ms'}</span></div>
  485. <div>Avg: ${Math.round(avgPing)}ms</div>
  486. `;
  487. }
  488.  
  489. // Toggle UI
  490. function toggleUI() {
  491. if (uiVisible) {
  492. showConfigPanel();
  493. } else {
  494. document.querySelectorAll('.perf-monitor-module').forEach(module => {
  495. const moduleId = module.id.replace('perf-monitor-', '');
  496. if (activeModules[moduleId]) {
  497. module.style.display = 'block';
  498. }
  499. });
  500. uiVisible = true;
  501. }
  502. }
  503.  
  504. // Toggle lock/unlock
  505. function toggleLock() {
  506. locked = !locked;
  507. document.querySelectorAll('.perf-monitor-module').forEach(module => {
  508. if (locked) {
  509. module.classList.remove('unlocked');
  510. module.classList.add('locked');
  511. } else {
  512. module.classList.remove('locked');
  513. module.classList.add('unlocked');
  514. }
  515. });
  516.  
  517. // Notification
  518. showNotification(locked ? 'UI Locked' : 'UI Unlocked');
  519. }
  520.  
  521. // Show notification
  522. function showNotification(message) {
  523. const notification = document.createElement('div');
  524. notification.style.cssText = `
  525. position: fixed;
  526. left: 50%;
  527. bottom: 30px;
  528. transform: translateX(-50%);
  529. background-color: rgba(0, 0, 0, 0.8);
  530. color: white;
  531. padding: 10px 20px;
  532. border-radius: 5px;
  533. font-family: monospace;
  534. z-index: 10001;
  535. `;
  536. notification.textContent = message;
  537. document.body.appendChild(notification);
  538.  
  539. setTimeout(() => {
  540. notification.style.opacity = '0';
  541. notification.style.transition = 'opacity 0.5s';
  542. setTimeout(() => document.body.removeChild(notification), 500);
  543. }, 1500);
  544. }
  545.  
  546. // Configuration panel
  547. function showConfigPanel() {
  548. uiVisible = false;
  549.  
  550. // Hide all modules
  551. document.querySelectorAll('.perf-monitor-module').forEach(module => {
  552. module.style.display = 'none';
  553. });
  554.  
  555. // Create config panel
  556. const configPanel = document.createElement('div');
  557. configPanel.className = 'perf-config-panel';
  558. configPanel.innerHTML = `
  559. <div class="perf-config-title">Performance Monitor Settings</div>
  560.  
  561. <div class="perf-config-option">
  562. <label>
  563. <input type="checkbox" id="perf-config-keyboard" ${activeModules.keyboard ? 'checked' : ''}>
  564. Keyboard Display
  565. </label>
  566. </div>
  567.  
  568. <div class="perf-config-option">
  569. <label>
  570. <input type="checkbox" id="perf-config-mouse" ${activeModules.mouse ? 'checked' : ''}>
  571. Mouse Display
  572. </label>
  573. </div>
  574.  
  575. <div class="perf-config-option">
  576. <label>
  577. <input type="checkbox" id="perf-config-mouseCoords" ${activeModules.mouseCoords ? 'checked' : ''}>
  578. Mouse Coordinates
  579. </label>
  580. </div>
  581.  
  582. <div class="perf-config-option">
  583. <label>
  584. <input type="checkbox" id="perf-config-fps" ${activeModules.fps ? 'checked' : ''}>
  585. FPS Counter
  586. </label>
  587. </div>
  588.  
  589. <div class="perf-config-option">
  590. <label>
  591. <input type="checkbox" id="perf-config-ping" ${activeModules.ping ? 'checked' : ''}>
  592. Ping Monitor
  593. </label>
  594. </div>
  595.  
  596. <div class="perf-config-buttons">
  597. <button class="perf-config-button" id="perf-config-save">Save</button>
  598. <button class="perf-config-button" id="perf-config-cancel">Cancel</button>
  599. <button class="perf-config-button" id="perf-config-reset">Reset Positions</button>
  600. </div>
  601. `;
  602. document.body.appendChild(configPanel);
  603.  
  604. // Event listeners
  605. configPanel.querySelector('#perf-config-save').addEventListener('click', () => {
  606. activeModules.keyboard = configPanel.querySelector('#perf-config-keyboard').checked;
  607. activeModules.mouse = configPanel.querySelector('#perf-config-mouse').checked;
  608. activeModules.mouseCoords = configPanel.querySelector('#perf-config-mouseCoords').checked;
  609. activeModules.fps = configPanel.querySelector('#perf-config-fps').checked;
  610. activeModules.ping = configPanel.querySelector('#perf-config-ping').checked;
  611.  
  612. // Remove all existing modules
  613. document.querySelectorAll('.perf-monitor-module').forEach(module => {
  614. document.body.removeChild(module);
  615. });
  616.  
  617. // Create new modules
  618. createModules();
  619.  
  620. document.body.removeChild(configPanel);
  621. uiVisible = true;
  622. });
  623.  
  624. configPanel.querySelector('#perf-config-cancel').addEventListener('click', () => {
  625. document.body.removeChild(configPanel);
  626.  
  627. // Show modules again
  628. document.querySelectorAll('.perf-monitor-module').forEach(module => {
  629. const moduleId = module.id.replace('perf-monitor-', '');
  630. if (activeModules[moduleId]) {
  631. module.style.display = 'block';
  632. }
  633. });
  634.  
  635. uiVisible = true;
  636. });
  637.  
  638. configPanel.querySelector('#perf-config-reset').addEventListener('click', () => {
  639. // Remove all existing modules
  640. document.querySelectorAll('.perf-monitor-module').forEach(module => {
  641. document.body.removeChild(module);
  642. });
  643.  
  644. // Create new modules with default positions
  645. createModules();
  646.  
  647. document.body.removeChild(configPanel);
  648. uiVisible = true;
  649. });
  650. }
  651.  
  652. // Initialize
  653. createModules();
  654. updateKeyboard();
  655. updateMouse();
  656. updateMouseCoords();
  657. updateFPS();
  658.  
  659. // Ping every 2 seconds
  660. measurePing();
  661. pingInterval = setInterval(measurePing, 2000);
  662.  
  663. // Add context menu prevention
  664. document.addEventListener('contextmenu', (e) => {
  665. const isMonitorElement = e.target.closest('.perf-monitor-module, .perf-config-panel');
  666. if (isMonitorElement) {
  667. e.preventDefault();
  668. }
  669. });
  670.  
  671. console.log('Advanced Performance Monitor started!');
  672. console.log('Press Home to toggle UI visibility/settings');
  673. console.log('Press End to lock/unlock UI elements');
  674. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement