Numeric Data Operator

Allows calculation of sum or product on numeric data pasted in a textbox, uses cmd [ to summon text boxes

  1. // ==UserScript==
  2. // @name Numeric Data Operator
  3. // @namespace http://your.namespace.com
  4. // @version 0.4
  5. // @description Allows calculation of sum or product on numeric data pasted in a textbox, uses cmd [ to summon text boxes
  6. // @author Andrew Lakkis
  7. // @match *://*/*
  8. // @license MIT
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to parse and process the numeric data
  16. function processData(data) {
  17. const rows = data.trim().split('\n');
  18. const columns = rows.map(row => row.split(/\s+/));
  19. const numericColumns = columns[0].map((_, i) => columns.map(row => parseFloat(row[i])).filter(num => !isNaN(num)));
  20.  
  21. return numericColumns;
  22. }
  23.  
  24. // Function to calculate the sum of a column
  25. function sum(column, useComma) {
  26. const sumValue = column.reduce((acc, curr) => acc + curr, 0);
  27. const expression = useComma ? column.join(', ') : column.join(' + ');
  28. return `${expression} = ${sumValue}`;
  29. }
  30.  
  31. // Function to calculate the product of a column
  32. function product(column, useComma) {
  33. const productValue = column.reduce((acc, curr) => acc * curr, 1);
  34. const expression = useComma ? column.join(', ') : column.join(' * ');
  35. return `${expression} = ${productValue}`;
  36. }
  37.  
  38. // Function to create and display the user interface
  39. function createUI(data) {
  40. if (!document.querySelector('[placeholder="Paste numeric data here..."]')) {
  41. const container = document.createElement('div');
  42. container.style.position = 'fixed';
  43. container.style.top = '50%';
  44. container.style.left = '50%';
  45. container.style.transform = 'translate(-50%, -50%)';
  46. container.style.backgroundColor = '#fff';
  47. container.style.padding = '20px';
  48. container.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.2)';
  49. container.style.zIndex = '9999';
  50.  
  51. const textArea = document.createElement('textarea');
  52. textArea.style.width = '300px';
  53. textArea.style.height = '200px';
  54. textArea.placeholder = 'Paste numeric data here...';
  55. container.appendChild(textArea);
  56.  
  57. const select = document.createElement('select');
  58. const sumOption = document.createElement('option');
  59. sumOption.text = 'Sum';
  60. select.add(sumOption);
  61. const productOption = document.createElement('option');
  62. productOption.text = 'Product';
  63. select.add(productOption);
  64. container.appendChild(select);
  65.  
  66. const commaCheckbox = document.createElement('input');
  67. commaCheckbox.type = 'checkbox';
  68. commaCheckbox.id = 'commaCheckbox';
  69. const commaLabel = document.createElement('label');
  70. commaLabel.htmlFor = 'commaCheckbox';
  71. commaLabel.textContent = 'Use comma separation';
  72. container.appendChild(commaCheckbox);
  73. container.appendChild(commaLabel);
  74.  
  75. const calculateButton = document.createElement('button');
  76. calculateButton.textContent = 'Calculate';
  77. calculateButton.onclick = function() {
  78. const selectedOption = select.options[select.selectedIndex].text;
  79. const useComma = commaCheckbox.checked;
  80. const numericData = processData(textArea.value);
  81. const result = selectedOption === 'Sum' ? numericData.map(column => sum(column, useComma)) : numericData.map(column => product(column, useComma));
  82. resultTextBox.value = result.join('\n\n');
  83. };
  84. container.appendChild(calculateButton);
  85.  
  86. const resultTextBox = document.createElement('textarea');
  87. resultTextBox.style.width = '300px';
  88. resultTextBox.style.height = '200px';
  89. resultTextBox.placeholder = 'Calculation results will appear here...';
  90. resultTextBox.disabled = true;
  91. container.appendChild(resultTextBox);
  92.  
  93. const closeButton = document.createElement('button');
  94. closeButton.textContent = 'Close';
  95. closeButton.onclick = function() {
  96. document.body.removeChild(container);
  97. };
  98. container.appendChild(closeButton);
  99.  
  100. document.body.appendChild(container);
  101. }
  102. }
  103.  
  104. // Check if the user presses Cmd + [
  105. document.addEventListener('keydown', function(event) {
  106. if (event.metaKey && event.key === '[') {
  107. event.preventDefault();
  108. createUI();
  109. }
  110. });
  111. })();