Skip to content

Commit eef3b09

Browse files
authored
Merge pull request #153 from qunash/dev
Dev
2 parents efc097e + ed12d17 commit eef3b09

File tree

7 files changed

+28
-40
lines changed

7 files changed

+28
-40
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "webchatgpt",
4-
"version": "3.2.6",
4+
"version": "3.2.7",
55
"license": "MIT",
66
"scripts": {
77
"build-dev": "node build.mjs",

src/components/slashCommandsMenu.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ const SlashCommandItem = (props: {
4343
}
4444

4545
// const renderSlashButton = (textarea: HTMLTextAreaElement, show: boolean, onClick: () => void) => {
46-
// let div = document.querySelector('wcg-slash-button-div')
46+
// let div = document.querySelector('div.wcg-slash-button-div')
4747
// if (div) div.remove()
4848

49-
// div = document.createElement('wcg-slash-button-div')
49+
// div = document.createElement('div.wcg-slash-button-div')
5050
// div.className = "self-center"
5151
// textarea.parentElement.insertBefore(div, textarea.parentElement.firstChild)
5252
// render(<SlashButton show={show} onclick={onClick} />, div)

src/content-scripts/mainUI.tsx

+23-35
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ import SlashCommandsMenu, { slashCommands } from 'src/components/slashCommandsMe
1212
import { apiExtractText } from './api'
1313

1414
let isProcessing = false
15+
let updatingUI = false
1516

17+
const rootEl = getRootElement()
1618
let btnSubmit: HTMLButtonElement | null | undefined
1719
let textarea: HTMLTextAreaElement | null
1820
let chatGptFooter: HTMLDivElement | null
21+
let toolbar: HTMLElement | null
1922

2023

2124
function 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

133134
async 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

167170
async 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(() => {
206204
window.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

222210
window.onunload = function () {

src/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "__MSG_appName__",
44
"description": "__MSG_appDesc__",
55
"default_locale": "en",
6-
"version": "3.2.6",
6+
"version": "3.2.7",
77
"icons": {
88
"16": "icons/icon16.png",
99
"48": "icons/icon48.png",

src/manifest.v2.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "__MSG_appName__",
44
"description": "__MSG_appDesc__",
55
"default_locale": "en",
6-
"version": "3.2.6",
6+
"version": "3.2.7",
77
"icons": {
88
"16": "icons/icon16.png",
99
"48": "icons/icon48.png",

0 commit comments

Comments
 (0)