Greasy Fork

Basic Math Solver

A simple calculator for addition, subtraction, and multiplication that floats on any webpage.

目前为 2024-11-23 提交的版本。查看 最新版本

// ==UserScript==
// @version 0.2
// @name         Basic Math Solver
// @namespace    https://example.com
// @description  A simple calculator for addition, subtraction, and multiplication that floats on any webpage.
// @author       nukerboss
// @match         http://live.mathletics.com/*
// @match        *://live.mathletics.com/*
// @grant        none
// ==/UserScript==
// @license MIT
(function() {
    'use strict';

    // Create a floating calculator div
    const calculator = document.createElement('div');
    calculator.style.position = 'fixed';
    calculator.style.bottom = '20px';
    calculator.style.right = '20px';
    calculator.style.width = '300px';
    calculator.style.padding = '10px';
    calculator.style.backgroundColor = '#f0f0f0';
    calculator.style.border = '1px solid #ccc';
    calculator.style.borderRadius = '10px';
    calculator.style.boxShadow = '0px 0px 10px rgba(0,0,0,0.1)';
    calculator.style.zIndex = '9999';
    calculator.innerHTML = `
        <h3>Math Solver</h3>
        <input type="text" id="mathInput" placeholder="Enter math problem (e.g., 5+3)" style="width: 90%; padding: 5px; margin-bottom: 10px;" />
        <button id="calculateBtn" style="padding: 5px 10px;">Calculate</button>
        <div id="result" style="margin-top: 10px; font-weight: bold;"></div>
    `;
    document.body.appendChild(calculator);

    // Add event listener to the calculate button
    document.getElementById('calculateBtn').addEventListener('click', function() {
        const mathInput = document.getElementById('mathInput').value;
        const resultDiv = document.getElementById('result');

        try {
            // Evaluate the math expression
            const result = eval(mathInput);
            resultDiv.textContent = `Result: ${result}`;
        } catch (error) {
            resultDiv.textContent = "Invalid input! Please enter a valid math problem.";
        }
    });
})();