@@ -12,18 +12,22 @@ import SlashCommandsMenu, { slashCommands } from 'src/components/slashCommandsMe
12
12
import { apiExtractText } from './api'
13
13
14
14
let isProcessing = false
15
+ let updatingUI = false
15
16
17
+ const rootEl = getRootElement ( )
16
18
let btnSubmit : HTMLButtonElement | null | undefined
17
19
let textarea : HTMLTextAreaElement | null
18
20
let chatGptFooter : HTMLDivElement | null
21
+ let toolbar : HTMLElement | null
19
22
20
23
21
24
function renderSlashCommandsMenu ( ) {
22
25
23
- let div = document . querySelector ( 'wcg-slash-commands-menu' )
26
+ let div = document . querySelector ( 'div. wcg-slash-commands-menu' )
24
27
if ( div ) div . remove ( )
25
28
26
- div = document . createElement ( 'wcg-slash-commands-menu' )
29
+ div = document . createElement ( 'div' )
30
+ div . className = "wcg-slash-commands-menu"
27
31
const textareaParentParent = textarea ?. parentElement ?. parentElement
28
32
29
33
textareaParentParent ?. insertBefore ( div , textareaParentParent . firstChild )
@@ -70,9 +74,7 @@ async function handleSubmit(query: string) {
70
74
71
75
try {
72
76
const results = await processQuery ( query , userConfig )
73
- // console.info("WebChatGPT results --> ", results)
74
77
const compiledPrompt = await compilePrompt ( results , query )
75
- // console.info("WebChatGPT compiledPrompt --> ", compiledPrompt)
76
78
textarea . value = compiledPrompt
77
79
pressEnter ( )
78
80
} catch ( error ) {
@@ -95,7 +97,6 @@ async function onSubmit(event: MouseEvent | KeyboardEvent) {
95
97
if ( ! isProcessing && ( event . type === "click" || ( isKeyEvent && event . key === 'Enter' ) ) ) {
96
98
const query = textarea ?. value . trim ( )
97
99
98
- // if query is empty or undefined, return
99
100
if ( ! query ) return
100
101
101
102
textarea . value = ""
@@ -132,11 +133,13 @@ function showErrorMessage(error: Error) {
132
133
133
134
async function updateUI ( ) {
134
135
135
- formChild = document . querySelector ( 'form' ) ?. children [ 0 ] as HTMLElement
136
- textarea = getTextArea ( )
137
- // console.info("UpdateUI textarea: ", textarea)
136
+ if ( updatingUI ) return
138
137
139
- const toolbar = getWebChatGPTToolbar ( )
138
+ updatingUI = true
139
+
140
+ textarea = getTextArea ( )
141
+ toolbar = getWebChatGPTToolbar ( )
142
+ console . info ( "toolbar --> " , toolbar )
140
143
if ( ! textarea ) {
141
144
toolbar ?. remove ( )
142
145
return
@@ -155,27 +158,19 @@ async function updateUI() {
155
158
156
159
renderSlashCommandsMenu ( )
157
160
158
- // textarea.parentElement.style.flexDirection = 'row'
159
-
160
161
chatGptFooter = getFooter ( )
161
162
if ( chatGptFooter ) {
162
163
const lastChild = chatGptFooter . lastElementChild as HTMLElement
163
164
if ( lastChild ) lastChild . style . padding = '0 0 0.5em 0'
164
165
}
166
+
167
+ updatingUI = false
165
168
}
166
169
167
170
async function renderToolbar ( ) {
168
171
169
172
try {
170
173
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
-
179
174
const { shadowRootDiv, shadowRoot } = await createShadowRoot ( 'content-scripts/mainUI.css' )
180
175
shadowRootDiv . classList . add ( 'wcg-toolbar' )
181
176
textareaParentParent ?. appendChild ( shadowRootDiv )
@@ -188,12 +183,15 @@ async function renderToolbar() {
188
183
}
189
184
}
190
185
191
- const form = document . querySelector ( 'form' )
192
- const formParent = form ?. parentElement
193
186
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
+
197
195
try {
198
196
updateUI ( )
199
197
} catch ( e ) {
@@ -206,17 +204,7 @@ const mutationObserver = new MutationObserver(() => {
206
204
window . onload = function ( ) {
207
205
updateUI ( )
208
206
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 } )
220
208
}
221
209
222
210
window . onunload = function ( ) {
0 commit comments