@@ -12,18 +12,22 @@ import SlashCommandsMenu, { slashCommands } from 'src/components/slashCommandsMe
1212import { apiExtractText } from './api'
1313
1414let isProcessing = false
15+ let updatingUI = false
1516
17+ const rootEl = getRootElement ( )
1618let btnSubmit : HTMLButtonElement | null | undefined
1719let textarea : HTMLTextAreaElement | null
1820let chatGptFooter : HTMLDivElement | null
21+ let toolbar : HTMLElement | null
1922
2023
2124function renderSlashCommandsMenu ( ) {
2225
23- let div = document . querySelector ( 'wcg-slash-commands-menu' )
26+ let div = document . querySelector ( 'div. wcg-slash-commands-menu' )
2427 if ( div ) div . remove ( )
2528
26- div = document . createElement ( 'wcg-slash-commands-menu' )
29+ div = document . createElement ( 'div' )
30+ div . className = "wcg-slash-commands-menu"
2731 const textareaParentParent = textarea ?. parentElement ?. parentElement
2832
2933 textareaParentParent ?. insertBefore ( div , textareaParentParent . firstChild )
@@ -70,9 +74,7 @@ async function handleSubmit(query: string) {
7074
7175 try {
7276 const results = await processQuery ( query , userConfig )
73- // console.info("WebChatGPT results --> ", results)
7477 const compiledPrompt = await compilePrompt ( results , query )
75- // console.info("WebChatGPT compiledPrompt --> ", compiledPrompt)
7678 textarea . value = compiledPrompt
7779 pressEnter ( )
7880 } catch ( error ) {
@@ -95,7 +97,6 @@ async function onSubmit(event: MouseEvent | KeyboardEvent) {
9597 if ( ! isProcessing && ( event . type === "click" || ( isKeyEvent && event . key === 'Enter' ) ) ) {
9698 const query = textarea ?. value . trim ( )
9799
98- // if query is empty or undefined, return
99100 if ( ! query ) return
100101
101102 textarea . value = ""
@@ -132,11 +133,13 @@ function showErrorMessage(error: Error) {
132133
133134async function updateUI ( ) {
134135
135- formChild = document . querySelector ( 'form' ) ?. children [ 0 ] as HTMLElement
136- textarea = getTextArea ( )
137- // console.info("UpdateUI textarea: ", textarea)
136+ if ( updatingUI ) return
138137
139- const toolbar = getWebChatGPTToolbar ( )
138+ updatingUI = true
139+
140+ textarea = getTextArea ( )
141+ toolbar = getWebChatGPTToolbar ( )
142+ console . info ( "toolbar --> " , toolbar )
140143 if ( ! textarea ) {
141144 toolbar ?. remove ( )
142145 return
@@ -155,27 +158,19 @@ async function updateUI() {
155158
156159 renderSlashCommandsMenu ( )
157160
158- // textarea.parentElement.style.flexDirection = 'row'
159-
160161 chatGptFooter = getFooter ( )
161162 if ( chatGptFooter ) {
162163 const lastChild = chatGptFooter . lastElementChild as HTMLElement
163164 if ( lastChild ) lastChild . style . padding = '0 0 0.5em 0'
164165 }
166+
167+ updatingUI = false
165168}
166169
167170async function renderToolbar ( ) {
168171
169172 try {
170173 const textareaParentParent = textarea ?. parentElement ?. parentElement
171- // const textareaParentParent = formChild
172- // if (textareaParentParent && textareaParentParent.parentElement) {
173- // textareaParentParent.style.flexDirection = 'column'
174- // textareaParentParent.parentElement.style.flexDirection = 'column'
175- // textareaParentParent.parentElement.style.gap = '0px'
176- // textareaParentParent.parentElement.style.marginBottom = '0.5em'
177- // }
178-
179174 const { shadowRootDiv, shadowRoot } = await createShadowRoot ( 'content-scripts/mainUI.css' )
180175 shadowRootDiv . classList . add ( 'wcg-toolbar' )
181176 textareaParentParent ?. appendChild ( shadowRootDiv )
@@ -188,12 +183,15 @@ async function renderToolbar() {
188183 }
189184}
190185
191- const form = document . querySelector ( 'form' )
192- const formParent = form ?. parentElement
193186
194- const rootEl = getRootElement ( )
195- let formChild = document . querySelector ( 'form' ) ?. children [ 0 ] as HTMLElement
196- const mutationObserver = new MutationObserver ( ( ) => {
187+ const mutationObserver = new MutationObserver ( ( mutations ) => {
188+
189+ if ( ! mutations . some ( mutation => mutation . removedNodes . length > 0 ) ) return
190+
191+ console . info ( "WebChatGPT: Mutation observer triggered" )
192+
193+ if ( getWebChatGPTToolbar ( ) ) return
194+
197195 try {
198196 updateUI ( )
199197 } catch ( e ) {
@@ -206,17 +204,7 @@ const mutationObserver = new MutationObserver(() => {
206204window . onload = function ( ) {
207205 updateUI ( )
208206
209- if ( formChild ) {
210- mutationObserver . observe ( formChild , { childList : true } )
211- }
212-
213- if ( rootEl ) {
214- mutationObserver . observe ( rootEl , { childList : true } )
215- }
216-
217- if ( formParent ) {
218- mutationObserver . observe ( formParent , { childList : true } )
219- }
207+ mutationObserver . observe ( rootEl , { childList : true , subtree : true } )
220208}
221209
222210window . onunload = function ( ) {
0 commit comments