Lute: Current Page Statuses Alert Button

Adds a button to alert the current page term statuses

  1. // ==UserScript==
  2. // @name Lute: Current Page Statuses Alert Button
  3. // @version 20240509.2
  4. // @description Adds a button to alert the current page term statuses
  5. // @author jamesdeluk
  6. // @match http://localhost:500*/read/*
  7. // @grant none
  8. // @homepageURL https://greasyfork.org/en/scripts/493338-lute-current-page-statuses-alert-button
  9. // @supportURL https://greasyfork.org/en/scripts/493338-lute-current-page-statuses-alert-button/feedback
  10. // @namespace https://greasyfork.org/users/242246
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. window.addEventListener('load', function() {
  17. const btn_statuses = document.createElement('button');
  18. btn_statuses.textContent = "%";
  19. btn_statuses.style.position = "fixed";
  20. btn_statuses.style.left = "0";
  21. btn_statuses.style.top = "50%";
  22. btn_statuses.style.transform = "translateY(-50%)";
  23. btn_statuses.style.margin = "0.1em";
  24. btn_statuses.style.padding = "0.1em 0.1em";
  25. btn_statuses.style.zIndex = "1000";
  26. btn_statuses.onclick = function() {
  27. const statusNames = {
  28. 'status0': '?',
  29. 'status1': '1',
  30. 'status2': '2',
  31. 'status3': '3',
  32. 'status4': '4',
  33. 'status5': '5',
  34. 'status99': '✓',
  35. 'status98': 'X'
  36. };
  37. let totalStatusCount = 0;
  38. let statusCounts = Object.keys(statusNames).map(status => {
  39. let count = document.getElementsByClassName(status).length;
  40. totalStatusCount += count;
  41. return { status: statusNames[status], count };
  42. });
  43. let combinedStatusRange = ['status1', 'status2', 'status3', 'status4', 'status5'];
  44. let combinedStatusCount = combinedStatusRange.reduce((sum, status) => {
  45. return sum + document.getElementsByClassName(status).length;
  46. }, 0);
  47. let indexStatus5 = statusCounts.findIndex(item => item.status === '5');
  48. statusCounts.splice(indexStatus5 + 1, 0, { status: 'L', count: combinedStatusCount });
  49. let orderedAggregatedStatuses = ['?', 'L', '✓', 'X'];
  50. let detailedStatuses = ['1', '2', '3', '4', '5'];
  51. let total = `Total terms: ${totalStatusCount}\n`;
  52. let results = '';
  53. // let bars = '';
  54. orderedAggregatedStatuses.forEach(status => {
  55. let item = statusCounts.find(item => item.status === status);
  56. if (item) {
  57. let percentage = (item.count / totalStatusCount) * 100;
  58. // let barLength = Math.round(percentage / 5);
  59. // let bar = new Array(barLength + 1).join('+');
  60. results += `${status.padEnd(2)} : ${String(item.count).padEnd(3)} (${percentage.toFixed(0)}%)\n`;
  61. // bars += `${status.padEnd(2)} : ${bar}\n`;
  62. }
  63. });
  64. results += '\n';
  65. detailedStatuses.forEach(status => {
  66. let item = statusCounts.find(item => item.status === status);
  67. if (item) {
  68. let percentageOfCombined = (item.count / combinedStatusCount) * 100;
  69. let barLength = Math.round(percentageOfCombined / 5);
  70. let bar = new Array(barLength + 1).join('+');
  71. results += `${status.padEnd(2)} : ${String(item.count).padEnd(3)} (${percentageOfCombined.toFixed(0)}%)\n`;
  72. // bars += `${status.padEnd(2)} : ${bar}\n`;
  73. }
  74. });
  75. // alert(total + '\n' + results + '\n' + bars);
  76. alert(total + '\n' + results);
  77. };
  78. document.body.appendChild(btn_statuses);
  79. });
  80.  
  81. })();