Advertisement
Guest User

custom.js

a guest
Mar 13th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. // Custom JavaScript for CPU usage highlighting
  2. (function() {
  3. // Function to find and highlight CPU usage values
  4. function highlightCpuUsage() {
  5. console.log('Searching for CPU usage values...');
  6.  
  7. // Look for any text containing "CPU" and then find nearby percentage values
  8. const allElements = document.querySelectorAll('*');
  9.  
  10. allElements.forEach(element => {
  11. // Skip elements without text content
  12. if (!element.textContent) return;
  13.  
  14. // Check if this element contains "CPU" text
  15. if (element.textContent.includes('CPU')) {
  16. console.log('Found CPU element:', element);
  17.  
  18. // Look for percentage values in siblings or children
  19. const siblings = getSiblings(element);
  20. siblings.forEach(sibling => {
  21. checkForPercentage(sibling);
  22. });
  23.  
  24. // Also check children of parent
  25. if (element.parentNode) {
  26. Array.from(element.parentNode.children).forEach(child => {
  27. checkForPercentage(child);
  28. });
  29. }
  30. }
  31. });
  32.  
  33. // Function to get all siblings of an element
  34. function getSiblings(element) {
  35. if (!element.parentNode) return [];
  36. return Array.from(element.parentNode.children).filter(child => child !== element);
  37. }
  38.  
  39. // Function to check if an element contains a percentage and highlight it
  40. function checkForPercentage(element) {
  41. if (!element || !element.textContent) return;
  42.  
  43. // Check if this element or any of its children contain a percentage
  44. if (element.textContent.includes('%')) {
  45. console.log('Found potential percentage element:', element);
  46.  
  47. // Try to find the exact element with just the percentage
  48. const percentElements = findPercentageElements(element);
  49. percentElements.forEach(el => {
  50. applyHighlighting(el);
  51. });
  52. }
  53. }
  54.  
  55. // Function to find elements containing just percentage values
  56. function findPercentageElements(element) {
  57. const results = [];
  58.  
  59. // Check if this element itself contains just a percentage
  60. if (element.childNodes.length === 1 &&
  61. element.childNodes[0].nodeType === 3 &&
  62. /^\s*\d+%\s*$/.test(element.textContent)) {
  63. results.push(element);
  64. return results;
  65. }
  66.  
  67. // Check direct children
  68. Array.from(element.children).forEach(child => {
  69. if (child.childNodes.length === 1 &&
  70. child.childNodes[0].nodeType === 3 &&
  71. /^\s*\d+%\s*$/.test(child.textContent)) {
  72. results.push(child);
  73. }
  74. });
  75.  
  76. // If we found direct children with percentages, return them
  77. if (results.length > 0) return results;
  78.  
  79. // Otherwise, look for spans that might contain percentages
  80. const spans = element.querySelectorAll('span');
  81. spans.forEach(span => {
  82. if (/^\s*\d+%\s*$/.test(span.textContent)) {
  83. results.push(span);
  84. }
  85. });
  86.  
  87. return results;
  88. }
  89.  
  90. // Function to apply highlighting based on percentage value
  91. function applyHighlighting(element) {
  92. const percentText = element.textContent.trim();
  93. const percentValue = parseFloat(percentText);
  94.  
  95. if (isNaN(percentValue)) return;
  96.  
  97. console.log('Highlighting element with value:', percentValue, element);
  98.  
  99. // Remove any existing highlighting classes
  100. element.classList.remove('cpu-value-low', 'cpu-value-medium', 'cpu-value-high');
  101.  
  102. // Apply appropriate class based on value
  103. if (percentValue >= 50) {
  104. element.classList.add('cpu-value-high');
  105. } else if (percentValue >= 25) {
  106. element.classList.add('cpu-value-medium');
  107. } else {
  108. element.classList.add('cpu-value-low');
  109. }
  110. }
  111. }
  112.  
  113. // Run the function after a delay to ensure the DOM is loaded
  114. setTimeout(highlightCpuUsage, 2000);
  115.  
  116. // Run periodically to catch updates
  117. setInterval(highlightCpuUsage, 5000);
  118. })();
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement