-
Notifications
You must be signed in to change notification settings - Fork 853
Expand file tree
/
Copy pathindex.jsx
More file actions
512 lines (464 loc) · 15.8 KB
/
index.jsx
File metadata and controls
512 lines (464 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import './styles.scss'
import { unmountComponentAtNode } from 'react-dom'
import { render } from 'preact'
import DecisionCard from '../components/DecisionCard'
import { config as siteConfig } from './site-adapters'
import { config as toolsConfig } from './selection-tools'
import { config as menuConfig } from './menu-tools'
import {
chatgptWebModelKeys,
getPreferredLanguageKey,
getUserConfig,
isUsingChatgptWebModel,
setAccessToken,
setUserConfig,
} from '../config/index.mjs'
import {
createElementAtPosition,
cropText,
endsWithQuestionMark,
getApiModesStringArrayFromConfig,
getClientPosition,
getPossibleElementByQuerySelector,
} from '../utils'
import FloatingToolbar from '../components/FloatingToolbar'
import Browser from 'webextension-polyfill'
import { getPreferredLanguage } from '../config/language.mjs'
import '../_locales/i18n-react'
import { changeLanguage } from 'i18next'
import { initSession } from '../services/init-session.mjs'
import { getChatGptAccessToken, registerPortListener } from '../services/wrappers.mjs'
import { generateAnswersWithChatgptWebApi } from '../services/apis/chatgpt-web.mjs'
import WebJumpBackNotification from '../components/WebJumpBackNotification'
/**
* @param {string} siteName
* @param {SiteConfig} siteConfig
*/
async function mountComponent(siteName, siteConfig) {
if (siteName === 'github' && location.href.includes('/wiki')) {
return
}
const userConfig = await getUserConfig()
if (!userConfig.alwaysFloatingSidebar) {
const retry = 10
let oldUrl = location.href
for (let i = 1; i <= retry; i++) {
if (location.href !== oldUrl) {
console.log(`SiteAdapters Retry ${i}/${retry}: stop`)
return
}
const e =
(siteConfig &&
(getPossibleElementByQuerySelector(siteConfig.sidebarContainerQuery) ||
getPossibleElementByQuerySelector(siteConfig.appendContainerQuery) ||
getPossibleElementByQuerySelector(siteConfig.resultsContainerQuery))) ||
getPossibleElementByQuerySelector([userConfig.prependQuery]) ||
getPossibleElementByQuerySelector([userConfig.appendQuery])
if (e) {
console.log(`SiteAdapters Retry ${i}/${retry}: found`)
console.log(e)
break
} else {
console.log(`SiteAdapters Retry ${i}/${retry}: not found`)
if (i === retry) return
else await new Promise((r) => setTimeout(r, 500))
}
}
}
document.querySelectorAll('.chatgptbox-container,#chatgptbox-container').forEach((e) => {
unmountComponentAtNode(e)
e.remove()
})
let question
if (userConfig.inputQuery) question = await getInput([userConfig.inputQuery])
if (!question && siteConfig) question = await getInput(siteConfig.inputQuery)
document.querySelectorAll('.chatgptbox-container,#chatgptbox-container').forEach((e) => {
unmountComponentAtNode(e)
e.remove()
})
if (userConfig.alwaysFloatingSidebar && question) {
const position = {
x: window.innerWidth - 300 - Math.floor((20 / 100) * window.innerWidth),
y: window.innerHeight / 2 - 200,
}
const toolbarContainer = createElementAtPosition(position.x, position.y)
toolbarContainer.className = 'chatgptbox-toolbar-container-not-queryable'
let triggered = false
if (userConfig.triggerMode === 'always') triggered = true
else if (userConfig.triggerMode === 'questionMark' && endsWithQuestionMark(question.trim()))
triggered = true
render(
<FloatingToolbar
session={initSession({
modelName: userConfig.modelName,
apiMode: userConfig.apiMode,
extraCustomModelName: userConfig.customModelName,
})}
selection=""
container={toolbarContainer}
triggered={triggered}
closeable={true}
prompt={question}
/>,
toolbarContainer,
)
return
}
const container = document.createElement('div')
container.id = 'chatgptbox-container'
if (siteName === 'google' || siteName === 'kagi') {
container.style.width = '350px'
}
render(
<DecisionCard
session={initSession({
modelName: userConfig.modelName,
apiMode: userConfig.apiMode,
extraCustomModelName: userConfig.customModelName,
})}
question={question}
siteConfig={siteConfig}
container={container}
/>,
container,
)
}
/**
* @param {string[]|function} inputQuery
* @returns {Promise<string>}
*/
async function getInput(inputQuery) {
let input
if (typeof inputQuery === 'function') {
input = await inputQuery()
const replyPromptBelow = `Reply in ${await getPreferredLanguage()}. Regardless of the language of content I provide below. !!This is very important!!`
const replyPromptAbove = `Reply in ${await getPreferredLanguage()}. Regardless of the language of content I provide above. !!This is very important!!`
if (input) return `${replyPromptBelow}\n\n` + input + `\n\n${replyPromptAbove}`
return input
}
const searchInput = getPossibleElementByQuerySelector(inputQuery)
if (searchInput) {
if (searchInput.value) input = searchInput.value
else if (searchInput.textContent) input = searchInput.textContent
if (input)
return (
`Reply in ${await getPreferredLanguage()}.\nThe following is a search input in a search engine, ` +
`giving useful content or solutions and as much information as you can related to it, ` +
`use markdown syntax to make your answer more readable, such as code blocks, bold, list:\n` +
input
)
}
}
let toolbarContainer
const deleteToolbar = () => {
if (toolbarContainer && toolbarContainer.className === 'chatgptbox-toolbar-container')
toolbarContainer.remove()
}
const createSelectionTools = async (toolbarContainer, selection) => {
toolbarContainer.className = 'chatgptbox-toolbar-container'
const userConfig = await getUserConfig()
render(
<FloatingToolbar
session={initSession({
modelName: userConfig.modelName,
apiMode: userConfig.apiMode,
extraCustomModelName: userConfig.customModelName,
})}
selection={selection}
container={toolbarContainer}
dockable={true}
/>,
toolbarContainer,
)
}
async function prepareForSelectionTools() {
document.addEventListener('mouseup', (e) => {
if (toolbarContainer && toolbarContainer.contains(e.target)) return
const selectionElement =
window.getSelection()?.rangeCount > 0 &&
window.getSelection()?.getRangeAt(0).endContainer.parentElement
if (toolbarContainer && selectionElement && toolbarContainer.contains(selectionElement)) return
deleteToolbar()
setTimeout(async () => {
const selection = window
.getSelection()
?.toString()
.trim()
.replace(/^-+|-+$/g, '')
if (selection) {
let position
const config = await getUserConfig()
if (!config.selectionToolsNextToInputBox) position = { x: e.pageX + 20, y: e.pageY + 20 }
else {
const inputElement = selectionElement.querySelector('input, textarea')
if (inputElement) {
position = getClientPosition(inputElement)
position = {
x: position.x + window.scrollX + inputElement.offsetWidth + 50,
y: e.pageY + 30,
}
} else {
position = { x: e.pageX + 20, y: e.pageY + 20 }
}
}
toolbarContainer = createElementAtPosition(position.x, position.y)
await createSelectionTools(toolbarContainer, selection)
}
})
})
document.addEventListener('mousedown', (e) => {
if (toolbarContainer && toolbarContainer.contains(e.target)) return
document.querySelectorAll('.chatgptbox-toolbar-container').forEach((e) => e.remove())
})
document.addEventListener('keydown', (e) => {
if (
toolbarContainer &&
!toolbarContainer.contains(e.target) &&
(e.target.nodeName === 'INPUT' || e.target.nodeName === 'TEXTAREA')
) {
setTimeout(() => {
if (!window.getSelection()?.toString().trim()) deleteToolbar()
})
}
})
}
async function prepareForSelectionToolsTouch() {
document.addEventListener('touchend', (e) => {
if (toolbarContainer && toolbarContainer.contains(e.target)) return
if (
toolbarContainer &&
window.getSelection()?.rangeCount > 0 &&
toolbarContainer.contains(window.getSelection()?.getRangeAt(0).endContainer.parentElement)
)
return
deleteToolbar()
setTimeout(() => {
const selection = window
.getSelection()
?.toString()
.trim()
.replace(/^-+|-+$/g, '')
if (selection) {
toolbarContainer = createElementAtPosition(
e.changedTouches[0].pageX + 20,
e.changedTouches[0].pageY + 20,
)
createSelectionTools(toolbarContainer, selection)
}
})
})
document.addEventListener('touchstart', (e) => {
if (toolbarContainer && toolbarContainer.contains(e.target)) return
document.querySelectorAll('.chatgptbox-toolbar-container').forEach((e) => e.remove())
})
}
let menuX, menuY
async function prepareForRightClickMenu() {
document.addEventListener('contextmenu', (e) => {
menuX = e.clientX
menuY = e.clientY
})
Browser.runtime.onMessage.addListener(async (message) => {
if (message.type === 'CREATE_CHAT') {
const data = message.data
let prompt = ''
const userConfig = await getUserConfig()
if (data.itemId in toolsConfig) {
prompt = await toolsConfig[data.itemId].genPrompt(data.selectionText)
} else if (data.itemId.startsWith('custom_')) {
// Handle custom selection tools from context menu
const customIndex = parseInt(data.itemId.replace('custom_', ''), 10)
if (!isNaN(customIndex) && customIndex >= 0) {
const customTool = userConfig.customSelectionTools?.[customIndex]
if (customTool?.active && customTool?.name) {
prompt = customTool.prompt.replace('{{selection}}', data.selectionText)
}
}
} else if (data.itemId in menuConfig) {
const menuItem = menuConfig[data.itemId]
if (!menuItem.genPrompt) return
else prompt = await menuItem.genPrompt()
if (prompt) prompt = await cropText(`Reply in ${await getPreferredLanguage()}.\n` + prompt)
}
const position = data.useMenuPosition
? { x: menuX, y: menuY }
: { x: window.innerWidth / 2 - 300, y: window.innerHeight / 2 - 200 }
const container = createElementAtPosition(position.x, position.y)
container.className = 'chatgptbox-toolbar-container-not-queryable'
render(
<FloatingToolbar
session={initSession({
modelName: userConfig.modelName,
apiMode: userConfig.apiMode,
extraCustomModelName: userConfig.customModelName,
})}
selection={data.selectionText}
container={container}
triggered={true}
closeable={true}
prompt={prompt}
/>,
container,
)
}
})
}
async function prepareForStaticCard() {
const userConfig = await getUserConfig()
let siteRegex
if (userConfig.useSiteRegexOnly) siteRegex = userConfig.siteRegex
else
siteRegex = new RegExp(
(userConfig.siteRegex && userConfig.siteRegex + '|') + Object.keys(siteConfig).join('|'),
)
const matches = location.hostname.match(siteRegex)
if (matches) {
const siteName = matches[0]
if (
userConfig.siteAdapters.includes(siteName) &&
!userConfig.activeSiteAdapters.includes(siteName)
)
return
let initSuccess = true
if (siteName in siteConfig) {
const siteAction = siteConfig[siteName].action
if (siteAction && siteAction.init) {
initSuccess = await siteAction.init(location.hostname, userConfig, getInput, mountComponent)
}
}
if (initSuccess) mountComponent(siteName, siteConfig[siteName])
}
}
async function overwriteAccessToken() {
if (location.hostname !== 'chatgpt.com') {
if (location.hostname === 'kimi.moonshot.cn' || location.hostname.includes('kimi.com')) {
setUserConfig({
kimiMoonShotRefreshToken: window.localStorage.refresh_token,
})
}
return
}
let data
if (location.pathname === '/api/auth/session') {
const response = document.querySelector('pre').textContent
try {
data = JSON.parse(response)
} catch (error) {
console.error('json error', error)
}
} else {
const resp = await fetch('https://chatgpt.com/api/auth/session')
data = await resp.json().catch(() => ({}))
}
if (data && data.accessToken) {
await setAccessToken(data.accessToken)
console.log(data.accessToken)
}
}
async function prepareForForegroundRequests() {
if (location.hostname !== 'chatgpt.com' || location.pathname === '/auth/login') return
const userConfig = await getUserConfig()
if (
!chatgptWebModelKeys.some((model) =>
getApiModesStringArrayFromConfig(userConfig, true).includes(model),
)
)
return
// if (location.pathname === '/') {
// const input = document.querySelector('#prompt-textarea')
// if (input) {
// input.textContent = ' '
// input.dispatchEvent(new Event('input', { bubbles: true }))
// setTimeout(() => {
// input.textContent = ''
// input.dispatchEvent(new Event('input', { bubbles: true }))
// }, 300)
// }
// }
await Browser.runtime.sendMessage({
type: 'SET_CHATGPT_TAB',
data: {},
})
registerPortListener(async (session, port) => {
if (isUsingChatgptWebModel(session)) {
const accessToken = await getChatGptAccessToken()
await generateAnswersWithChatgptWebApi(port, session.question, session, accessToken)
}
})
}
async function getClaudeSessionKey() {
return Browser.runtime.sendMessage({
type: 'GET_COOKIE',
data: { url: 'https://claude.ai/', name: 'sessionKey' },
})
}
async function prepareForJumpBackNotification() {
if (
location.hostname === 'chatgpt.com' &&
document.querySelector('button[data-testid=login-button]')
) {
console.log('chatgpt not logged in')
return
}
const url = new URL(window.location.href)
if (url.searchParams.has('chatgptbox_notification')) {
if (location.hostname === 'claude.ai' && !(await getClaudeSessionKey())) {
console.log('claude not logged in')
await new Promise((resolve) => {
const timer = setInterval(async () => {
const token = await getClaudeSessionKey()
if (token) {
clearInterval(timer)
resolve()
}
}, 500)
})
}
if (
(location.hostname === 'kimi.moonshot.cn' || location.hostname.includes('kimi.com')) &&
!window.localStorage.refresh_token
) {
console.log('kimi not logged in')
setTimeout(() => {
document.querySelector('.user-info-container').click()
}, 1000)
await new Promise((resolve) => {
const timer = setInterval(() => {
const token = window.localStorage.refresh_token
if (token) {
setUserConfig({
kimiMoonShotRefreshToken: token,
})
clearInterval(timer)
resolve()
}
}, 500)
})
}
const div = document.createElement('div')
document.body.append(div)
render(
<WebJumpBackNotification container={div} chatgptMode={location.hostname === 'chatgpt.com'} />,
div,
)
}
}
async function run() {
await getPreferredLanguageKey().then((lang) => {
changeLanguage(lang)
})
Browser.runtime.onMessage.addListener(async (message) => {
if (message.type === 'CHANGE_LANG') {
const data = message.data
changeLanguage(data.lang)
}
})
await overwriteAccessToken()
await prepareForForegroundRequests()
prepareForSelectionTools()
prepareForSelectionToolsTouch()
prepareForStaticCard()
prepareForRightClickMenu()
prepareForJumpBackNotification()
}
run()