-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopup.js
More file actions
129 lines (109 loc) · 3.37 KB
/
popup.js
File metadata and controls
129 lines (109 loc) · 3.37 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
document.addEventListener('DOMContentLoaded', function() {
const convertBtn = document.getElementById('convertBtn');
const loading = document.getElementById('loading');
const message = document.getElementById('message');
const messageText = document.getElementById('messageText');
const closeMessage = document.getElementById('closeMessage');
const settingsLink = document.getElementById('settingsLink');
convertBtn.addEventListener('click', handleConvert);
closeMessage.addEventListener('click', hideMessage);
settingsLink.addEventListener('click', openSettings);
async function handleConvert() {
try {
showLoading(true);
hideMessage();
// Check if API key is configured
const settings = await getSettings();
if (!settings.apiKey) {
showError('API key not found, please add it in settings');
return;
}
// Read image from clipboard
const imageData = await readClipboardImage();
if (!imageData) {
showError('Copy an image of the data first');
return;
}
// Send to background script for conversion
const result = await sendToBackground(imageData, settings);
if (result.success) {
showSuccess('CSV file downloaded successfully');
} else {
throw new Error(result.error);
}
} catch (error) {
console.error('Conversion error:', error);
if (error.message.includes('API')) {
showError(error.message);
} else {
showError('Could not convert this image to a csv');
}
} finally {
showLoading(false);
}
}
async function readClipboardImage() {
try {
const clipboardItems = await navigator.clipboard.read();
for (const clipboardItem of clipboardItems) {
for (const type of clipboardItem.types) {
if (type.startsWith('image/')) {
const blob = await clipboardItem.getType(type);
return await blobToBase64(blob);
}
}
}
return null;
} catch (error) {
console.error('Clipboard read error:', error);
return null;
}
}
function blobToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
async function sendToBackground(imageData, settings) {
return new Promise((resolve) => {
chrome.runtime.sendMessage({
action: 'convertImage',
imageData: imageData,
settings: settings
}, (response) => {
resolve(response);
});
});
}
async function getSettings() {
return new Promise((resolve) => {
chrome.storage.sync.get(['apiKey', 'model', 'prompt'], (result) => {
resolve(result);
});
});
}
function showLoading(show) {
loading.style.display = show ? 'flex' : 'none';
convertBtn.disabled = show;
}
function showMessage(text, type) {
messageText.textContent = text;
message.className = `message ${type}`;
message.style.display = 'block';
}
function showError(text) {
showMessage(text, 'error');
}
function showSuccess(text) {
showMessage(text, 'success');
}
function hideMessage() {
message.style.display = 'none';
}
function openSettings() {
chrome.runtime.openOptionsPage();
}
});