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.
Prefills selected text and automatically adapts inside Bootstrap modals.
Real-time speech-to-text dictation mapped directly to active textareas.
Zero dynamic inline CSS, strict DOMPurify sanitization, and CSRF token binding.
Granular per-textarea destroy, MutationObserver sync, and no memory leaks.
Include the companion stylesheet before or after Bootstrap 5, and include the script after jQuery 3.x.
<!-- 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>
Simply add the data-ai-generate attribute to any HTML textarea. The built-in MutationObserver automatically binds to present and dynamically inserted textareas.
<!-- Automatic declarative attachment -->
<textarea data-ai-generate class="form-control" rows="4" placeholder="Type here..."></textarea>
Or programmatically initialize using the jQuery plugin syntax:
// Target a specific textarea by selector
$('#product-description').aiDescriptionGenerator();
Enable Web Speech API dictation directly beside the generation icon.
data-ai-voice="true"
Request styled HTML generation. Sanitized automatically via DOMPurify.
data-ai-response="html"
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...'
}
});
| 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. |
| 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. |
Listen for lifecycle hooks fired directly on your native <textarea> element:
// 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);
});
Your server (Laravel, Node, Python, etc.) must accept and respond with the following JSON schema:
{
"prompt": "Write a product description for eco shoes",
"maxLength": 200,
"responseFormat": "text", // or "html"
"temperature": 0.7
}
{
"status": "success",
"content": "Step lightly with our all-new eco sneakers...",
"tokens": 42
}
Test dynamic initialization and destruction in real-time. Notice how other textareas on the page remain completely untouched.
[role="dialog"] and automatically repositions the generator overlay and adjusts the stacking z-index (10600+) to ensure it appears on top.
<meta name="csrf-token" content="..."> automatically and includes the X-CSRF-TOKEN header along with same-origin credentials.