Skip to content

Commit bf8b882

Browse files
committed
fix: Improve capture handling in CameraButton and ensure permission request is processed correctly
1 parent 0d9e92a commit bf8b882

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

entrypoints/sidepanel/components/Chat/CameraButton.vue

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,25 @@ watch(currentModel, async () => {
6767
const showButton = computed(() => supportsVision.value)
6868
6969
const handleCapture = async () => {
70-
if (isCapturing.value || !props.attachmentSelectorRef) return
70+
try {
71+
if (isCapturing.value || !props.attachmentSelectorRef) return
7172
72-
// Check if adding a screenshot would exceed the combined limit
73-
const currentAttachments = props.contextAttachmentStorage?.attachments ?? []
74-
const imageAndScreenshotCount = currentAttachments.filter(
75-
(attachment) => attachment.type === 'image' || attachment.type === 'captured-page',
76-
).length
73+
// Permission request should be handled instantly when user clicks the button, so put it here
74+
// Request permission and Capture the visible tab
75+
const dataUrl = await s2bRpc.captureVisibleTab()
7776
78-
if (imageAndScreenshotCount >= MAX_IMAGE_COUNT) {
79-
props.attachmentSelectorRef?.showErrorMessage(t('chat.input.attachment_selector.too_many_images', { max: MAX_IMAGE_COUNT }))
80-
return
81-
}
77+
// Check if adding a screenshot would exceed the combined limit
78+
const currentAttachments = props.contextAttachmentStorage?.attachments ?? []
79+
const imageAndScreenshotCount = currentAttachments.filter(
80+
(attachment) => attachment.type === 'image' || attachment.type === 'captured-page',
81+
).length
8282
83-
isCapturing.value = true
83+
if (imageAndScreenshotCount >= MAX_IMAGE_COUNT) {
84+
props.attachmentSelectorRef?.showErrorMessage(t('chat.input.attachment_selector.too_many_images', { max: MAX_IMAGE_COUNT }))
85+
return
86+
}
8487
85-
try {
86-
// Capture the visible tab
87-
const dataUrl = await s2bRpc.captureVisibleTab()
88+
isCapturing.value = true
8889
8990
if (!dataUrl) {
9091
throw new Error('Failed to capture screenshot')

utils/rpc/background-fns.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,9 @@ function getTabCaptureMediaStreamId(tabId: number, consumerTabId?: number) {
762762
}
763763

764764
function captureVisibleTab(options?: Browser.tabs.CaptureVisibleTabOptions) {
765-
const cachedWindowId = BackgroundWindowManager.getCurrentWindowId()
765+
// For firefox, some feature need reload extension after permission granted
766766
browser.permissions.request({ origins: ['<all_urls>'] })
767+
const cachedWindowId = BackgroundWindowManager.getCurrentWindowId()
767768
if (cachedWindowId) {
768769
const screenCaptureBase64Url = browser.tabs.captureVisibleTab(cachedWindowId, options ?? {})
769770
return screenCaptureBase64Url

0 commit comments

Comments
 (0)