1- import { CONFIG } from './content/config'
21import { logger } from './content/logger'
32import { injectStyles } from './content/styles'
43import { HandlerRegistry } from '../datamodel/handler-registry'
@@ -11,7 +10,7 @@ export default defineContentScript({
1110 main ( ) {
1211 const textAreasOnPageLoad = document . querySelectorAll < HTMLTextAreaElement > ( `textarea` )
1312 for ( const textarea of textAreasOnPageLoad ) {
14- initializeMaybe ( textarea )
13+ initializeMaybeIsPageload ( textarea , true )
1514 }
1615 const observer = new MutationObserver ( handleMutations )
1716 observer . observe ( document . body , {
@@ -30,35 +29,36 @@ function handleMutations(mutations: MutationRecord[]): void {
3029 if ( node . nodeType === Node . ELEMENT_NODE ) {
3130 const element = node as Element
3231 if ( element . tagName === 'TEXTAREA' ) {
33- initializeMaybe ( element as HTMLTextAreaElement )
32+ initializeMaybeIsPageload ( element as HTMLTextAreaElement , false )
3433 }
3534 // Also check for textareas within added subtrees
3635 const textareas = element . querySelectorAll ?.( 'textarea' )
3736 if ( textareas ) {
3837 for ( const textarea of textareas ) {
39- initializeMaybe ( textarea )
38+ initializeMaybeIsPageload ( textarea , false )
4039 }
4140 }
4241 }
4342 }
4443 }
4544}
4645
47- function initializeMaybe ( textarea : HTMLTextAreaElement ) {
48- if ( ! textarea . classList . contains ( CONFIG . ADDED_OVERTYPE_CLASS ) ) {
49- logger . debug ( 'activating textarea {}' , textarea )
50- injectStyles ( )
51- textarea . classList . add ( CONFIG . ADDED_OVERTYPE_CLASS )
52-
53- // Use registry to identify and handle this specific textarea
54- const textareaInfo = handlerRegistry . identifyTextarea ( textarea )
55- if ( textareaInfo ) {
56- logger . debug ( 'Identified textarea:' , textareaInfo . context . type , textareaInfo . context . unique_key )
57- textareaRegistry . register ( textareaInfo )
58- } else {
59- logger . debug ( 'No handler found for textarea' )
60- }
46+ function initializeMaybeIsPageload ( textarea : HTMLTextAreaElement , isPageload : boolean ) {
47+ // Check if this textarea is already registered
48+ if ( textareaRegistry . get ( textarea ) ) {
49+ logger . debug ( 'textarea already registered {}' , textarea )
50+ return
51+ }
52+
53+ logger . debug ( 'activating textarea {}' , textarea )
54+ injectStyles ( )
55+
56+ // Use registry to identify and handle this specific textarea
57+ const textareaInfo = handlerRegistry . identifyTextarea ( textarea )
58+ if ( textareaInfo ) {
59+ logger . debug ( 'Identified textarea:' , textareaInfo . context . type , textareaInfo . context . unique_key )
60+ textareaRegistry . register ( textareaInfo )
6161 } else {
62- logger . debug ( 'already activated textarea {}' , textarea )
62+ logger . debug ( 'No handler found for textarea' )
6363 }
6464}
0 commit comments