Numeric Data Operator

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

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Numeric Data Operator
// @namespace    http://your.namespace.com
// @version      0.4
// @description  Allows calculation of sum or product on numeric data pasted in a textbox, uses cmd [ to summon text boxes
// @author       Andrew Lakkis
// @match        *://*/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to parse and process the numeric data
    function processData(data) {
        const rows = data.trim().split('\n');
        const columns = rows.map(row => row.split(/\s+/));
        const numericColumns = columns[0].map((_, i) => columns.map(row => parseFloat(row[i])).filter(num => !isNaN(num)));

        return numericColumns;
    }

    // Function to calculate the sum of a column
    function sum(column, useComma) {
        const sumValue = column.reduce((acc, curr) => acc + curr, 0);
        const expression = useComma ? column.join(', ') : column.join(' + ');
        return `${expression} = ${sumValue}`;
    }

    // Function to calculate the product of a column
    function product(column, useComma) {
        const productValue = column.reduce((acc, curr) => acc * curr, 1);
        const expression = useComma ? column.join(', ') : column.join(' * ');
        return `${expression} = ${productValue}`;
    }

    // Function to create and display the user interface
    function createUI(data) {
        if (!document.querySelector('[placeholder="Paste numeric data here..."]')) {
            const container = document.createElement('div');
            container.style.position = 'fixed';
            container.style.top = '50%';
            container.style.left = '50%';
            container.style.transform = 'translate(-50%, -50%)';
            container.style.backgroundColor = '#fff';
            container.style.padding = '20px';
            container.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.2)';
            container.style.zIndex = '9999';

            const textArea = document.createElement('textarea');
            textArea.style.width = '300px';
            textArea.style.height = '200px';
            textArea.placeholder = 'Paste numeric data here...';
            container.appendChild(textArea);

            const select = document.createElement('select');
            const sumOption = document.createElement('option');
            sumOption.text = 'Sum';
            select.add(sumOption);
            const productOption = document.createElement('option');
            productOption.text = 'Product';
            select.add(productOption);
            container.appendChild(select);

            const commaCheckbox = document.createElement('input');
            commaCheckbox.type = 'checkbox';
            commaCheckbox.id = 'commaCheckbox';
            const commaLabel = document.createElement('label');
            commaLabel.htmlFor = 'commaCheckbox';
            commaLabel.textContent = 'Use comma separation';
            container.appendChild(commaCheckbox);
            container.appendChild(commaLabel);

            const calculateButton = document.createElement('button');
            calculateButton.textContent = 'Calculate';
            calculateButton.onclick = function() {
                const selectedOption = select.options[select.selectedIndex].text;
                const useComma = commaCheckbox.checked;
                const numericData = processData(textArea.value);
                const result = selectedOption === 'Sum' ? numericData.map(column => sum(column, useComma)) : numericData.map(column => product(column, useComma));
                resultTextBox.value = result.join('\n\n');
            };
            container.appendChild(calculateButton);

            const resultTextBox = document.createElement('textarea');
            resultTextBox.style.width = '300px';
            resultTextBox.style.height = '200px';
            resultTextBox.placeholder = 'Calculation results will appear here...';
            resultTextBox.disabled = true;
            container.appendChild(resultTextBox);

            const closeButton = document.createElement('button');
            closeButton.textContent = 'Close';
            closeButton.onclick = function() {
                document.body.removeChild(container);
            };
            container.appendChild(closeButton);

            document.body.appendChild(container);
        }
    }

    // Check if the user presses Cmd + [
    document.addEventListener('keydown', function(event) {
        if (event.metaKey && event.key === '[') {
            event.preventDefault();
            createUI();
        }
    });
})();