Skip to content

Commit 226fb10

Browse files
committed
improve b96ba7c
1 parent 817385f commit 226fb10

File tree

21 files changed

+115
-124
lines changed

21 files changed

+115
-124
lines changed

src/_locales/de/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Vorlagen-Template",
144144
"Explain this: {{selection}}": "Erkläre das: {{selection}}",
145145
"New": "Neu",
146-
"DisplayMode": "Anzeigemodus",
147-
"Display in sidebar": "In der Seitenleiste anzeigen",
148-
"Display in floating toolbar": "In der schwebenden Symbolleiste anzeigen"
146+
"Always display floating window, disable sidebar for all site adapters": "Immer das schwebende Fenster anzeigen, die Seitenleiste für alle Website-Adapter deaktivieren"
149147
}

src/_locales/en/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Prompt Template",
144144
"Explain this: {{selection}}": "Explain this: {{selection}}",
145145
"New": "New",
146-
"DisplayMode": "DisplayMode",
147-
"Display in sidebar": "Display in sidebar",
148-
"Display in floating toolbar": "Display in floating toolbar"
146+
"Always display floating window, disable sidebar for all site adapters": "Always display floating window, disable sidebar for all site adapters"
149147
}

src/_locales/es/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Plantilla de sugerencias",
144144
"Explain this: {{selection}}": "Explicar esto: {{selection}}",
145145
"New": "Nuevo",
146-
"DisplayMode": "Modo de visualización",
147-
"Display in sidebar": "Mostrar en la barra lateral",
148-
"Display in floating toolbar": "Mostrar en la barra de herramientas flotante"
146+
"Always display floating window, disable sidebar for all site adapters": "Mostrar siempre la ventana flotante, desactivar la barra lateral para todos los adaptadores de sitios"
149147
}

src/_locales/fr/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Modèle de suggestion",
144144
"Explain this: {{selection}}": "Expliquer ceci : {{selection}}",
145145
"New": "Nouveau",
146-
"DisplayMode": "Mode d'affichage",
147-
"Display in sidebar": "Afficher dans la barre latérale",
148-
"Display in floating toolbar": "Afficher dans la barre d'outils flottante"
146+
"Always display floating window, disable sidebar for all site adapters": "Toujours afficher la fenêtre flottante, désactiver la barre latérale pour tous les adaptateurs de site"
149147
}

src/_locales/in/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Template Prompt",
144144
"Explain this: {{selection}}": "Jelaskan ini: {{selection}}",
145145
"New": "Baru",
146-
"DisplayMode": "Mode Tampilan",
147-
"Display in sidebar": "Tampilkan di bilah sisi",
148-
"Display in floating toolbar": "Tampilkan di bilah alat mengambang"
146+
"Always display floating window, disable sidebar for all site adapters": "Selalu tampilkan jendela mengambang, nonaktifkan sidebar untuk semua adapter situs"
149147
}

src/_locales/it/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Modello di prompt",
144144
"Explain this: {{selection}}": "Spiega questo: {{selection}}",
145145
"New": "Nuovo",
146-
"DisplayMode": "Modalità di visualizzazione",
147-
"Display in sidebar": "Visualizza nella barra laterale",
148-
"Display in floating toolbar": "Visualizza nella barra degli strumenti flottante"
146+
"Always display floating window, disable sidebar for all site adapters": "Mostra sempre la finestra flottante, disabilita la barra laterale per tutti gli adattatori del sito"
149147
}

src/_locales/ja/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "プロンプトテンプレート",
144144
"Explain this: {{selection}}": "これを説明する: {{selection}}",
145145
"New": "新規",
146-
"DisplayMode": "表示モード",
147-
"Display in sidebar": "サイドバーに表示",
148-
"Display in floating toolbar": "フローティングツールバーに表示"
146+
"Always display floating window, disable sidebar for all site adapters": "常にフローティングウィンドウを表示し、すべてのサイトアダプターでサイドバーを無効にします"
149147
}

src/_locales/ko/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "프롬프트 템플릿",
144144
"Explain this: {{selection}}": "이것을 설명하세요: {{selection}}",
145145
"New": "새로 만들기",
146-
"DisplayMode": "디스플레이 모드",
147-
"Display in sidebar": "사이드바에 표시",
148-
"Display in floating toolbar": "떠다니는 도구 모음에 표시"
146+
"Always display floating window, disable sidebar for all site adapters": "항상 떠다니는 창을 표시하고 모든 사이트 어댑터의 사이드바를 비활성화합니다"
149147
}

src/_locales/pt/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Modelo de Prompt",
144144
"Explain this: {{selection}}": "Explique isso: {{selection}}",
145145
"New": "Novo",
146-
"DisplayMode": "Modo de Exibição",
147-
"Display in sidebar": "Exibir na barra lateral",
148-
"Display in floating toolbar": "Exibir na barra de ferramentas flutuante"
146+
"Always display floating window, disable sidebar for all site adapters": "Sempre exibir janela flutuante, desativar barra lateral para todos os adaptadores de site"
149147
}

src/_locales/ru/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Шаблон запроса",
144144
"Explain this: {{selection}}": "Объяснить это: {{selection}}",
145145
"New": "Новый",
146-
"DisplayMode": "Режим отображения",
147-
"Display in sidebar": "Отображать в боковой панели",
148-
"Display in floating toolbar": "Отображать в плавающей панели инструментов"
146+
"Always display floating window, disable sidebar for all site adapters": "Всегда отображать плавающее окно, отключить боковую панель для всех адаптеров сайтов"
149147
}

src/_locales/tr/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "Prompt Şablonu",
144144
"Explain this: {{selection}}": "Bunu açıkla: {{selection}}",
145145
"New": "Yeni",
146-
"DisplayMode": "Görüntüleme Modu",
147-
"Display in sidebar": "Kenar çubuğunda görüntüle",
148-
"Display in floating toolbar": "Kayan araç çubuğunda görüntüle"
146+
"Always display floating window, disable sidebar for all site adapters": "Her zaman kayan pencereyi görüntüle, tüm site adaptörleri için kenar çubuğunu devre dışı bırak"
149147
}

src/_locales/zh-hans/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@
144144
"Prompt Template": "提示模板",
145145
"Explain this: {{selection}}": "解释这个: {{selection}}",
146146
"New": "新建",
147-
"DisplayMode": "显示方式",
148-
"Display in sidebar": "在侧边栏显示",
149-
"Display in floating toolbar": "在浮动工具栏显示",
147+
"Always display floating window, disable sidebar for all site adapters": "总是显示浮动窗口, 禁用所有站点适配器的侧边栏",
150148
"Temperature": "温度",
151149
"keep-alive Time": "保活时间",
152150
"5m": "5分钟",

src/_locales/zh-hant/main.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,5 @@
143143
"Prompt Template": "提示範本",
144144
"Explain this: {{selection}}": "解釋這個: {{selection}}",
145145
"New": "新增",
146-
"DisplayMode": "顯示模式",
147-
"Display in sidebar": "在側邊欄顯示",
148-
"Display in floating toolbar": "在浮動工具列顯示"
146+
"Always display floating window, disable sidebar for all site adapters": "總是顯示浮動視窗,停用所有網站適配器的側邊欄"
149147
}

src/components/ConversationCard/index.jsx

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DesktopDownloadIcon,
1010
LinkExternalIcon,
1111
MoveToBottomIcon,
12+
SearchIcon,
1213
} from '@primer/octicons-react'
1314
import { Pin, WindowDesktop, XLg } from 'react-bootstrap-icons'
1415
import FileSaver from 'file-saver'
@@ -46,6 +47,7 @@ function ConversationCard(props) {
4647
const { t } = useTranslation()
4748
const [isReady, setIsReady] = useState(!props.question)
4849
const [port, setPort] = useState(() => Browser.runtime.connect())
50+
const [triggered, setTriggered] = useState(!props.waitForTrigger)
4951
const [session, setSession] = useState(props.session)
5052
const windowSize = useClampWindowSize([750, 1500], [250, 1100])
5153
const bodyRef = useRef(null)
@@ -59,7 +61,7 @@ function ConversationCard(props) {
5961
const [conversationItemData, setConversationItemData] = useState(
6062
(() => {
6163
if (session.conversationRecords.length === 0)
62-
if (props.question)
64+
if (props.question && triggered)
6365
return [
6466
new ConversationItemData(
6567
'answer',
@@ -102,12 +104,12 @@ function ConversationCard(props) {
102104

103105
useEffect(async () => {
104106
// when the page is responsive, session may accumulate redundant data and needs to be cleared after remounting and before making a new request
105-
if (props.question) {
107+
if (props.question && triggered) {
106108
const newSession = initSession({ ...session, question: props.question })
107109
setSession(newSession)
108110
await postMessage({ session: newSession })
109111
}
110-
}, [props.question]) // usually only triggered once
112+
}, [props.question, triggered]) // usually only triggered once
111113

112114
/**
113115
* @param {string} value
@@ -516,32 +518,53 @@ function ConversationCard(props) {
516518
/>
517519
))}
518520
</div>
519-
<InputBox
520-
enabled={isReady}
521-
postMessage={postMessage}
522-
reverseResizeDir={props.pageMode}
523-
onSubmit={async (question) => {
524-
const newQuestion = new ConversationItemData('question', question)
525-
const newAnswer = new ConversationItemData(
526-
'answer',
527-
`<p class="gpt-loading">${t('Waiting for response...')}</p>`,
528-
)
529-
setConversationItemData([...conversationItemData, newQuestion, newAnswer])
530-
setIsReady(false)
521+
{props.waitForTrigger && !triggered ? (
522+
<p
523+
className="manual-btn"
524+
style={{ display: 'flex', justifyContent: 'center' }}
525+
onClick={() => {
526+
setConversationItemData([
527+
new ConversationItemData(
528+
'answer',
529+
`<p class="gpt-loading">${t(`Waiting for response...`)}</p>`,
530+
),
531+
])
532+
setTriggered(true)
533+
setIsReady(false)
534+
}}
535+
>
536+
<span className="icon-and-text">
537+
<SearchIcon size="small" /> {t('Ask ChatGPT')}
538+
</span>
539+
</p>
540+
) : (
541+
<InputBox
542+
enabled={isReady}
543+
postMessage={postMessage}
544+
reverseResizeDir={props.pageMode}
545+
onSubmit={async (question) => {
546+
const newQuestion = new ConversationItemData('question', question)
547+
const newAnswer = new ConversationItemData(
548+
'answer',
549+
`<p class="gpt-loading">${t('Waiting for response...')}</p>`,
550+
)
551+
setConversationItemData([...conversationItemData, newQuestion, newAnswer])
552+
setIsReady(false)
531553

532-
const newSession = { ...session, question, isRetry: false }
533-
setSession(newSession)
534-
try {
535-
await postMessage({ session: newSession })
536-
} catch (e) {
537-
updateAnswer(e, false, 'error')
538-
}
539-
bodyRef.current.scrollTo({
540-
top: bodyRef.current.scrollHeight,
541-
behavior: 'instant',
542-
})
543-
}}
544-
/>
554+
const newSession = { ...session, question, isRetry: false }
555+
setSession(newSession)
556+
try {
557+
await postMessage({ session: newSession })
558+
} catch (e) {
559+
updateAnswer(e, false, 'error')
560+
}
561+
bodyRef.current.scrollTo({
562+
top: bodyRef.current.scrollHeight,
563+
behavior: 'instant',
564+
})
565+
}}
566+
/>
567+
)}
545568
</div>
546569
)
547570
}
@@ -557,6 +580,7 @@ ConversationCard.propTypes = {
557580
onDock: PropTypes.func,
558581
notClampSize: PropTypes.bool,
559582
pageMode: PropTypes.bool,
583+
waitForTrigger: PropTypes.bool,
560584
}
561585

562586
export default memo(ConversationCard)

src/components/ConversationItem/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ReadButton from '../ReadButton'
55
import PropTypes from 'prop-types'
66
import MarkdownRender from '../MarkdownRender/markdown.jsx'
77
import { useTranslation } from 'react-i18next'
8-
import { isUsingCustomModel, isUsingOllamaModel } from '../../config/index.mjs'
8+
import { isUsingCustomModel } from '../../config/index.mjs'
99
import { useConfig } from '../../hooks/use-config.mjs'
1010

1111
function AnswerTitle({ descName, modelName }) {

src/components/FloatingToolbar/index.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function FloatingToolbar(props) {
2222
const windowSize = useClampWindowSize([750, 1500], [0, Infinity])
2323
const config = useConfig(() => {
2424
setRender(true)
25-
if (!triggered) {
25+
if (!triggered && selection) {
2626
props.container.style.position = 'absolute'
2727
setTimeout(() => {
2828
const left = Math.min(
@@ -49,7 +49,7 @@ function FloatingToolbar(props) {
4949

5050
if (!render) return <div />
5151

52-
if (triggered) {
52+
if (triggered || (prompt && !selection)) {
5353
const updatePosition = () => {
5454
const newPosition = setElementPositionInViewport(props.container, position.x, position.y)
5555
if (position.x !== newPosition.x || position.y !== newPosition.y) setPosition(newPosition) // clear extra virtual position offset
@@ -106,6 +106,7 @@ function FloatingToolbar(props) {
106106
dockable={props.dockable}
107107
onDock={onDock}
108108
onUpdate={onUpdate}
109+
waitForTrigger={prompt && !triggered && !selection}
109110
/>
110111
</div>
111112
</div>

src/config/index.mjs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ export const TriggerMode = {
88
manually: 'Manually',
99
}
1010

11-
export const DisplayMode = {
12-
sidebar: 'Display in sidebar',
13-
floatingToolbar: 'Display in floating toolbar',
14-
}
15-
1611
export const ThemeMode = {
1712
light: 'Light',
1813
dark: 'Dark',
@@ -213,8 +208,6 @@ export const defaultConfig = {
213208

214209
/** @type {keyof TriggerMode}*/
215210
triggerMode: 'manually',
216-
/** @type {keyof DisplayMode}*/
217-
displayMode: 'sidebar',
218211
/** @type {keyof ThemeMode}*/
219212
themeMode: 'auto',
220213
/** @type {keyof Models}*/
@@ -223,6 +216,7 @@ export const defaultConfig = {
223216
preferredLanguage: getNavigatorLanguage(),
224217
clickIconAction: 'popup',
225218
insertAtTop: isMobile(),
219+
alwaysFloatingSidebar: false,
226220
lockWhenAnswer: true,
227221
answerScrollMargin: 200,
228222
autoRegenAfterSwitchModel: false,

src/content-script/index.jsx

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import {
1616
createElementAtPosition,
1717
cropText,
18+
endsWithQuestionMark,
1819
getClientPosition,
1920
getPossibleElementByQuerySelector,
2021
} from '../utils'
@@ -33,28 +34,30 @@ import NotificationForChatGPTWeb from '../components/NotificationForChatGPTWeb'
3334
* @param {UserConfig} userConfig
3435
*/
3536
async function mountComponent(siteConfig, userConfig) {
36-
const retry = 10
37-
let oldUrl = location.href
38-
for (let i = 1; i <= retry; i++) {
39-
if (location.href !== oldUrl) {
40-
console.log(`SiteAdapters Retry ${i}/${retry}: stop`)
41-
return
42-
}
43-
const e =
44-
(siteConfig &&
45-
(getPossibleElementByQuerySelector(siteConfig.sidebarContainerQuery) ||
46-
getPossibleElementByQuerySelector(siteConfig.appendContainerQuery) ||
47-
getPossibleElementByQuerySelector(siteConfig.resultsContainerQuery))) ||
48-
getPossibleElementByQuerySelector([userConfig.prependQuery]) ||
49-
getPossibleElementByQuerySelector([userConfig.appendQuery])
50-
if (e) {
51-
console.log(`SiteAdapters Retry ${i}/${retry}: found`)
52-
console.log(e)
53-
break
54-
} else {
55-
console.log(`SiteAdapters Retry ${i}/${retry}: not found`)
56-
if (i === retry) return
57-
else await new Promise((r) => setTimeout(r, 500))
37+
if (!userConfig.alwaysFloatingSidebar) {
38+
const retry = 10
39+
let oldUrl = location.href
40+
for (let i = 1; i <= retry; i++) {
41+
if (location.href !== oldUrl) {
42+
console.log(`SiteAdapters Retry ${i}/${retry}: stop`)
43+
return
44+
}
45+
const e =
46+
(siteConfig &&
47+
(getPossibleElementByQuerySelector(siteConfig.sidebarContainerQuery) ||
48+
getPossibleElementByQuerySelector(siteConfig.appendContainerQuery) ||
49+
getPossibleElementByQuerySelector(siteConfig.resultsContainerQuery))) ||
50+
getPossibleElementByQuerySelector([userConfig.prependQuery]) ||
51+
getPossibleElementByQuerySelector([userConfig.appendQuery])
52+
if (e) {
53+
console.log(`SiteAdapters Retry ${i}/${retry}: found`)
54+
console.log(e)
55+
break
56+
} else {
57+
console.log(`SiteAdapters Retry ${i}/${retry}: not found`)
58+
if (i === retry) return
59+
else await new Promise((r) => setTimeout(r, 500))
60+
}
5861
}
5962
}
6063
document.querySelectorAll('.chatgptbox-container,#chatgptbox-container').forEach((e) => {
@@ -77,14 +80,17 @@ async function mountComponent(siteConfig, userConfig) {
7780
}
7881
const toolbarContainer = createElementAtPosition(position.x, position.y)
7982
toolbarContainer.className = 'chatgptbox-toolbar-container-not-queryable'
80-
if (userConfig.displayMode === 'floatingToolbar') {
83+
if (userConfig.alwaysFloatingSidebar && question) {
84+
let triggered = false
85+
if (userConfig.triggerMode === 'always') triggered = true
86+
else if (userConfig.triggerMode === 'questionMark' && endsWithQuestionMark(question.trim()))
87+
triggered = true
8188
render(
8289
<FloatingToolbar
8390
session={initSession({ modelName: userConfig.modelName })}
84-
selection={question}
91+
selection=""
8592
container={toolbarContainer}
86-
dockable={true}
87-
triggered={true}
93+
triggered={triggered}
8894
closeable={true}
8995
prompt={question}
9096
/>,

0 commit comments

Comments
 (0)