AI Grammar Checker (Grammarly Alternative) for Violentmonkey

Checks grammar and spelling in text inputs (Like Grammarly Premium!)

目前為 2025-02-13 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         AI Grammar Checker (Grammarly Alternative) for Violentmonkey
// @namespace    http://violentmonkey.net/
// @version      1.2
// @description  Checks grammar and spelling in text inputs (Like Grammarly Premium!)
// @author       You
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @connect      api.languagetool.org
// ==/UserScript==

var GrammarChecker = {};

GrammarChecker.API_URL = "https://api.languagetool.org/v2/check";

GrammarChecker.init = function() {
    console.log("🚀 Grammar Checker Loaded for Violentmonkey!");

    // Observe text inputs and editable divs
    let observer = new MutationObserver(() => {
        document.querySelectorAll("textarea, input[type='text'], [contenteditable='true']").forEach(element => {
            if (!element.dataset.grammarChecked) {
                element.dataset.grammarChecked = "true";
                element.addEventListener("blur", () => GrammarChecker.checkGrammar(element));
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
};

GrammarChecker.checkGrammar = function(element) {
    let text = element.value || element.innerText;
    if (!text.trim()) return;

    console.log("📝 Checking grammar for:", text);

    GM_xmlhttpRequest({
        method: "POST",
        url: GrammarChecker.API_URL,
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        data: `language=en-US&text=${encodeURIComponent(text)}`,
        onload: function(response) {
            if (response.status === 200) {
                let data = JSON.parse(response.responseText);
                GrammarChecker.showCorrections(element, data);
            } else {
                console.error("❌ API Error:", response.statusText);
            }
        },
        onerror: function(error) {
            console.error("❌ Network Error:", error);
        }
    });
};

GrammarChecker.showCorrections = function(element, data) {
    let errors = data.matches;
    if (errors.length === 0) {
        console.log("✅ No errors found.");
        return;
    }

    // Remove old tooltips
    document.querySelectorAll(".grammar-tooltip").forEach(e => e.remove());

    let suggestions = errors.map(e => `${e.message} ➝ ${e.replacements.map(r => r.value).join(", ")}`).join("\n");

    let tooltip = document.createElement("div");
    tooltip.className = "grammar-tooltip";
    tooltip.innerText = suggestions;

    tooltip.style.position = "absolute";
    tooltip.style.background = "#fffa65";
    tooltip.style.color = "#333";
    tooltip.style.border = "1px solid #f39c12";
    tooltip.style.padding = "8px";
    tooltip.style.zIndex = "9999";
    tooltip.style.maxWidth = "300px";
    tooltip.style.fontSize = "14px";
    tooltip.style.top = `${element.getBoundingClientRect().bottom + window.scrollY}px`;
    tooltip.style.left = `${element.getBoundingClientRect().left}px`;

    document.body.appendChild(tooltip);

    setTimeout(() => tooltip.remove(), 5000);
};

// Start the script
GrammarChecker.init();