Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

feat: paste images into chat from clipboard

Emre Sokullu9e173625eb77
diff --git a/src/chrome/src/ui/attachment-drop.js b/src/chrome/src/ui/attachment-drop.js
index c124693cb..da8cf0ee5 100644
--- a/src/chrome/src/ui/attachment-drop.js
+++ b/src/chrome/src/ui/attachment-drop.js
@@ -7,6 +7,33 @@ export function hasFileDragPayload(event) {
return types.includes(FILE_DRAG_TYPE) || Number(dataTransfer.files?.length || 0) > 0;
}
+export function clipboardImageFiles(event) {
+ const clipboardData = event?.clipboardData;
+ if (!clipboardData) return [];
+ const fromFiles = Array.from(clipboardData.files || []);
+ const fromItems = Array.from(clipboardData.items || [])
+ .filter(item => item?.kind === 'file')
+ .map(item => item.getAsFile?.())
+ .filter(Boolean);
+ return [...fromFiles, ...fromItems].filter(file => (
+ String(file?.type || '').startsWith('image/')
+ ));
+}
+
+export function installClipboardImagePasteHandler(target, onFiles) {
+ if (!target?.addEventListener || typeof onFiles !== 'function') return () => {};
+
+ const onPaste = (event) => {
+ const files = clipboardImageFiles(event);
+ if (!files.length) return;
+ event.preventDefault();
+ onFiles(files);
+ };
+
+ target.addEventListener('paste', onPaste);
+ return () => target.removeEventListener?.('paste', onPaste);
+}
+
export function installFileDropHandlers(target, onFiles) {
if (!target?.addEventListener || typeof onFiles !== 'function') return () => {};
diff --git a/src/chrome/src/ui/sidepanel.js b/src/chrome/src/ui/sidepanel.js
index 95df8476f..de7f11802 100644
--- a/src/chrome/src/ui/sidepanel.js
+++ b/src/chrome/src/ui/sidepanel.js
@@ -80,7 +80,7 @@ import {
removeStagedScreenshots,
saveStagedScreenshot,
} from './staged-screenshot-store.js';
-import { installFileDropHandlers } from './attachment-drop.js';
+import { installClipboardImagePasteHandler, installFileDropHandlers } from './attachment-drop.js';
import { isTextAttachment } from './attachment-file.js';
const isStandaloneWindow = new URLSearchParams(window.location.search).get('standalone') === 'true';
@@ -14246,6 +14246,10 @@ installFileDropHandlers(inputArea, (files) => {
handleAttachedFiles(files, renderedTabId ?? currentTabId);
});
+installClipboardImagePasteHandler(inputEl, (files) => {
+ handleAttachedFiles(files, renderedTabId ?? currentTabId);
+});
+
// --- Event Listeners ---
ensureSelectionAskActionEl();
diff --git a/src/firefox/src/ui/attachment-drop.js b/src/firefox/src/ui/attachment-drop.js
index c124693cb..da8cf0ee5 100644
--- a/src/firefox/src/ui/attachment-drop.js
+++ b/src/firefox/src/ui/attachment-drop.js
@@ -7,6 +7,33 @@ export function hasFileDragPayload(event) {
return types.includes(FILE_DRAG_TYPE) || Number(dataTransfer.files?.length || 0) > 0;
}
+export function clipboardImageFiles(event) {
+ const clipboardData = event?.clipboardData;
+ if (!clipboardData) return [];
+ const fromFiles = Array.from(clipboardData.files || []);
+ const fromItems = Array.from(clipboardData.items || [])
+ .filter(item => item?.kind === 'file')
+ .map(item => item.getAsFile?.())
+ .filter(Boolean);
+ return [...fromFiles, ...fromItems].filter(file => (
+ String(file?.type || '').startsWith('image/')
+ ));
+}
+
+export function installClipboardImagePasteHandler(target, onFiles) {
+ if (!target?.addEventListener || typeof onFiles !== 'function') return () => {};
+
+ const onPaste = (event) => {
+ const files = clipboardImageFiles(event);
+ if (!files.length) return;
+ event.preventDefault();
+ onFiles(files);
+ };
+
+ target.addEventListener('paste', onPaste);
+ return () => target.removeEventListener?.('paste', onPaste);
+}
+
export function installFileDropHandlers(target, onFiles) {
if (!target?.addEventListener || typeof onFiles !== 'function') return () => {};
diff --git a/src/firefox/src/ui/sidepanel.js b/src/firefox/src/ui/sidepanel.js
index 77a086927..5e61977d7 100644
--- a/src/firefox/src/ui/sidepanel.js
+++ b/src/firefox/src/ui/sidepanel.js
@@ -84,7 +84,7 @@ import {
removeStagedScreenshots,
saveStagedScreenshot,
} from './staged-screenshot-store.js';
-import { installFileDropHandlers } from './attachment-drop.js';
+import { installClipboardImagePasteHandler, installFileDropHandlers } from './attachment-drop.js';
import { isTextAttachment } from './attachment-file.js';
const isStandaloneWindow = new URLSearchParams(window.location.search).get('standalone') === 'true';
@@ -13709,6 +13709,10 @@ installFileDropHandlers(inputArea, (files) => {
handleAttachedFiles(files, currentTabId);
});
+installClipboardImagePasteHandler(inputEl, (files) => {
+ handleAttachedFiles(files, currentTabId);
+});
+
// --- Event Listeners ---
ensureSelectionAskActionEl();
diff --git a/test/attachment-drop.mjs b/test/attachment-drop.mjs
index 844c583f6..31cfd0149 100644
--- a/test/attachment-drop.mjs
+++ b/test/attachment-drop.mjs
@@ -180,6 +180,32 @@ for (const [label, relativeModule] of implementations) {
`${label}: file-list fallback should recognize a drop with no exposed type`
);
+ const pastedImage = { name: 'clipboard.png', type: 'image/png' };
+ assert.deepEqual(
+ module.clipboardImageFiles({ clipboardData: { files: [pastedImage, { name: 'notes.txt', type: 'text/plain' }] } }),
+ [pastedImage],
+ `${label}: clipboard images should be selected without treating text files as attachments`
+ );
+ assert.deepEqual(
+ module.clipboardImageFiles({ clipboardData: { files: [] } }),
+ [],
+ `${label}: text-only clipboard payloads should not produce attachments`
+ );
+ const itemsImage = { name: 'screenshot.png', type: 'image/png' };
+ assert.deepEqual(
+ module.clipboardImageFiles({
+ clipboardData: {
+ files: [],
+ items: [
+ { kind: 'file', getAsFile: () => itemsImage },
+ { kind: 'string', getAsFile: () => null },
+ ],
+ },
+ }),
+ [itemsImage],
+ `${label}: clipboard images from items should be recognized when files is empty`
+ );
+
const target = createTarget();
const files = [{ name: 'notes.txt', type: 'text/plain' }];
const received = [];
@@ -238,6 +264,22 @@ for (const [label, relativeModule] of implementations) {
globalThis.document.dispatchEvent('dragleave', { relatedTarget: null });
assert.equal(target2.classList.contains('drag-over'), false, `${label}: document dragleave should reset a cancelled OS drag`);
cleanup2();
+
+ const pasted = [];
+ const pasteTarget = createTarget();
+ const cleanupPaste = module.installClipboardImagePasteHandler(pasteTarget, files => pasted.push(files));
+ const textPaste = pasteTarget.dispatch('paste', null, {
+ clipboardData: { files: [{ name: 'notes.txt', type: 'text/plain' }] },
+ });
+ assert.equal(textPaste.defaultPrevented, false, `${label}: text paste should remain native composer behavior`);
+ const imagePaste = pasteTarget.dispatch('paste', null, {
+ clipboardData: { files: [pastedImage] },
+ });
+ assert.equal(imagePaste.defaultPrevented, true, `${label}: image paste should be consumed as an attachment`);
+ assert.deepEqual(pasted, [[pastedImage]], `${label}: image paste should forward clipboard files to the attachment reader`);
+ cleanupPaste();
+ pasteTarget.dispatch('paste', null, { clipboardData: { files: [pastedImage] } });
+ assert.deepEqual(pasted, [[pastedImage]], `${label}: paste cleanup should remove its listener`);
}
console.log('attachment drag-and-drop tests passed for Chrome and Firefox');