Enterprise Production Ready v2.0

Intelligent AI Content Generation For Any Textarea

Seamlessly embed multi-modal text generation, speech recognition, and contextual prompt prefilling into any form or modal. Zero framework locks, fully CSP-safe, and resilient.

AI Generator Icon

Core Production Features

Context Aware

Prefills selected text and automatically adapts inside Bootstrap modals.

Voice Recognition

Real-time speech-to-text dictation mapped directly to active textareas.

CSP & XSS Safe

Zero dynamic inline CSS, strict DOMPurify sanitization, and CSRF token binding.

Clean Lifecycle

Granular per-textarea destroy, MutationObserver sync, and no memory leaks.

Installation

CDN / Local

Include the companion stylesheet before or after Bootstrap 5, and include the script after jQuery 3.x.

HTML Header & Scripts
<!-- 1. Stylesheets -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/ai-description-generator.css">

<!-- 2. Dependencies (jQuery 3.x & DOMPurify for HTML mode) -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/3.0.6/purify.min.js"></script>

<!-- 3. AI Generator Plugin -->
<script src="js/ai-description-generator.js"></script>

Basic Implementation

Quick Start

Simply add the data-ai-generate attribute to any HTML textarea. The built-in MutationObserver automatically binds to present and dynamically inserted textareas.

Declarative HTML Setup
<!-- Automatic declarative attachment -->
<textarea data-ai-generate class="form-control" rows="4" placeholder="Type here..."></textarea>

Or programmatically initialize using the jQuery plugin syntax:

JavaScript Initialization
// Target a specific textarea by selector
$('#product-description').aiDescriptionGenerator();

Advanced Attributes & Configuration

Voice Recognition

Enable Web Speech API dictation directly beside the generation icon.

data-ai-voice="true"
Rich HTML Mode

Request styled HTML generation. Sanitized automatically via DOMPurify.

data-ai-response="html"
Global Customization (AIGenerator.init)
AIGenerator.init({
    apiEndpoint: '/api/v1/ai/generate',
    defaultMaxTokens: 300,
    defaultTemperature: 0.7,
    cooldownMs: 2500, // anti-spam delay
    i18n: {
        title: 'Smart Assistant',
        generate: 'Create Content',
        loadingText: 'Synthesizing with AI...'
    }
});

Live Interactive Examples

Ready to Test
1. Standard AI Textarea data-ai-generate
2. Voice Input + AI Generation data-ai-voice="true"
3. Custom Modal Instance Configuration Scoped Options

Configuration Options

Property Type Default Description
apiEndpoint String '/api/ai/generate' The backend REST route that processes the prompt.
apiMethod String 'POST' HTTP request method for the generation endpoint.
apiHeaders Object {} Optional headers (Authorization/Cookies stripped for safety).
defaultMaxTokens Number 200 Default maximum length slider value.
defaultTemperature Number 0.7 Creativity level preset: 0.3 (Low), 0.7 (Med), 1.0 (High).
cooldownMs Number 2000 Anti-spam rate limit preventing rapid duplicate requests.
timeout Number 60000 Max client wait time (ms) before triggering AbortController.
closeOnOutsideClick Boolean false Closes the generator modal when backdrop is clicked.
prefill.enabled Boolean true Automatically copy highlighted text into the prompt box.

Public JavaScript Methods

Method Scope Parameters Usage
.aiDescriptionGenerator() jQuery options? (Object) Attaches AI capability and buttons to the selected textarea.
.aiDescriptionGenerator('destroy') jQuery none Detaches UI, unbinds events, and removes instance modals.
AIGenerator.init() Global options (Object) Configures global defaults and binds document listeners.
AIGenerator.detach() Global target (Selector/jQuery) Detaches AI from a specific textarea element.
AIGenerator.destroy() Global none Kills all observers, active instances, and modals across page.

Lifecycle Events

Listen for lifecycle hooks fired directly on your native <textarea> element:

Event Listener Example
// Triggered as soon as API response finishes
$('textarea').on('aiContentGenerated', function(e, content) {
    console.log('AI generated response:', content);
});

// Triggered when user clicks 'Insert' into the textarea
$('textarea').on('aiContentInserted', function(e, text) {
    console.log('Content inserted into field:', text);
});

Backend API Contract

POST Endpoint

Your server (Laravel, Node, Python, etc.) must accept and respond with the following JSON schema:

Incoming Request Payload
{
  "prompt": "Write a product description for eco shoes",
  "maxLength": 200,
  "responseFormat": "text", // or "html"
  "temperature": 0.7
}
Standard Success Response
{
  "status": "success",
  "content": "Step lightly with our all-new eco sneakers...",
  "tokens": 42
}

Live Dynamic Sandbox

Full Lifecycle

Test dynamic initialization and destruction in real-time. Notice how other textareas on the page remain completely untouched.

Frequently Asked Questions

Yes! The plugin automatically detects when a textarea is hosted inside an active modal or [role="dialog"] and automatically repositions the generator overlay and adjusts the stacking z-index (10600+) to ensure it appears on top.

The module scans for <meta name="csrf-token" content="..."> automatically and includes the X-CSRF-TOKEN header along with same-origin credentials.

The SpeechRecognition API is supported in modern Chromium browsers (Chrome, Edge, Opera) and Safari. If a user is on an unsupported browser, an alert notification handles the fallback gracefully without throwing console errors.