Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- const targetSelector = 'textarea.textarea';
- const debounceDelay = 500; // Adjust as needed
- let debounceTimer;
- const SYNTHETIC_EVENT_FLAG = 'isAISyntheticFixEvent'; // Custom property name
- const textarea = document.querySelector(targetSelector);
- if (!textarea) {
- console.error("AI Studio Fix: Could not find the target textarea with selector:", targetSelector);
- alert("AI Studio Fix: Textarea not found. The script couldn't be applied.");
- return;
- }
- console.log("AI Studio Fix: Attempting to apply input debouncing to:", textarea);
- let lastKnownValue = textarea.value;
- textarea.addEventListener('input', function(event) {
- // If this event was dispatched by our script, let it pass through normally
- // and don't start another debounce cycle.
- if (event[SYNTHETIC_EVENT_FLAG]) {
- console.log("AI Studio Fix: Allowing synthetic event to pass through.", event);
- return; // Do nothing more for our own synthetic events
- }
- // This is a user-generated event, so stop it and debounce
- event.stopImmediatePropagation();
- console.log("AI Studio Fix: User input captured and stopped. Current value:", event.target.value);
- clearTimeout(debounceTimer);
- const currentTarget = event.target;
- debounceTimer = setTimeout(() => {
- console.log("AI Studio Fix: Debounce timeout. Current textarea value:", currentTarget.value);
- // Create synthetic 'input' event and mark it
- const inputEvent = new Event('input', {
- bubbles: true,
- cancelable: true
- });
- inputEvent[SYNTHETIC_EVENT_FLAG] = true; // Mark as our synthetic event
- currentTarget.dispatchEvent(inputEvent);
- console.log("AI Studio Fix: Synthetic 'input' event dispatched.");
- // Dispatch synthetic 'change' event if value actually changed
- if (currentTarget.value !== lastKnownValue) {
- const changeEvent = new Event('change', {
- bubbles: true,
- cancelable: true
- });
- // No need to mark 'change' event unless it could also trigger an 'input' listener
- // For safety, we could, but usually 'input' listeners don't listen for 'change' specifically.
- // changeEvent[SYNTHETIC_EVENT_FLAG] = true; // Optional, probably not needed
- currentTarget.dispatchEvent(changeEvent);
- console.log("AI Studio Fix: Synthetic 'change' event dispatched because value changed.");
- lastKnownValue = currentTarget.value;
- } else {
- console.log("AI Studio Fix: Skipped synthetic 'change' event as value hasn't changed since last dispatch.");
- }
- }, debounceDelay);
- }, true); // true for 'useCapture'
- console.log(`AI Studio Fix: Input debouncing (delay: ${debounceDelay}ms) with loop prevention is now active.`);
- alert(`AI Studio Fix: Input debouncing with loop prevention is active. Try typing.`);
- lastKnownValue = textarea.value;
- })();
Advertisement
Add Comment
Please, Sign In to add comment