-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
28 lines (27 loc) · 933 Bytes
/
background.js
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
// Function to convert image to base64
async function getBase64Image(imageUrl) {
try {
const response = await fetch(imageUrl, {
mode: 'cors',
credentials: 'omit'
});
const blob = await response.blob();
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
} catch (error) {
throw new Error('Failed to fetch image');
}
}
// Listen for messages from content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'getBase64Image') {
getBase64Image(request.imageUrl)
.then(base64Data => sendResponse({ success: true, base64Data }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true; // Required to use sendResponse asynchronously
}
});