()
| 447 | let rightClickMenuInitialized = false |
| 448 | |
| 449 | async function prepareForRightClickMenu() { |
| 450 | if (rightClickMenuInitialized) { |
| 451 | console.debug('[content] Right-click menu already initialized, skipping.') |
| 452 | return |
| 453 | } |
| 454 | rightClickMenuInitialized = true |
| 455 | console.log('[content] Initializing right-click menu handler.') |
| 456 | document.addEventListener('contextmenu', (e) => { |
| 457 | menuX = e.clientX |
| 458 | menuY = e.clientY |
| 459 | console.debug(`[content] Context menu opened at X: ${menuX}, Y: ${menuY}`) |
| 460 | }) |
| 461 | |
| 462 | Browser.runtime.onMessage.addListener(async (message) => { |
| 463 | if (message.type === 'CREATE_CHAT') { |
| 464 | console.log('[content] Received CREATE_CHAT message:', message) |
| 465 | try { |
| 466 | const data = message.data |
| 467 | let prompt = '' |
| 468 | if (data.itemId in toolsConfig) { |
| 469 | console.debug('[content] Generating prompt from toolsConfig for item:', data.itemId) |
| 470 | prompt = await toolsConfig[data.itemId].genPrompt(data.selectionText) |
| 471 | } else if (data.itemId in menuConfig) { |
| 472 | console.debug('[content] Generating prompt from menuConfig for item:', data.itemId) |
| 473 | const menuItem = menuConfig[data.itemId] |
| 474 | if (!menuItem.genPrompt) { |
| 475 | console.warn('[content] No genPrompt for menu item:', data.itemId) |
| 476 | return |
| 477 | } |
| 478 | prompt = await menuItem.genPrompt() |
| 479 | if (prompt) { |
| 480 | const preferredLanguage = await getPreferredLanguage() |
| 481 | prompt = await cropText(`Reply in ${preferredLanguage}.\n` + prompt) |
| 482 | } |
| 483 | } else { |
| 484 | console.warn('[content] Unknown itemId for CREATE_CHAT:', data.itemId) |
| 485 | return |
| 486 | } |
| 487 | console.debug('[content] Generated prompt:', prompt) |
| 488 | |
| 489 | const useMenuPosition = |
| 490 | data.useMenuPosition && Number.isFinite(menuX) && Number.isFinite(menuY) |
| 491 | const position = useMenuPosition |
| 492 | ? { x: menuX, y: menuY } |
| 493 | : { x: window.innerWidth / 2 - 300, y: window.innerHeight / 2 - 200 } |
| 494 | console.debug('[content] Toolbar position for CREATE_CHAT:', position) |
| 495 | const container = createElementAtPosition(position.x, position.y) |
| 496 | container.className = 'chatgptbox-toolbar-container-not-queryable' |
| 497 | const userConfig = await getUserConfig() |
| 498 | render( |
| 499 | <FloatingToolbar |
| 500 | session={initSession({ |
| 501 | modelName: userConfig.modelName, |
| 502 | apiMode: userConfig.apiMode, |
| 503 | extraCustomModelName: userConfig.customModelName, |
| 504 | })} |
| 505 | selection={data.selectionText} |
| 506 | container={container} |
no test coverage detected