popup fixes

This commit is contained in:
schmeeve
2026-09-23 21:18:30 -07:00
parent caf8187252
commit ff620a23ed
4 changed files with 70 additions and 4 deletions

View File

@@ -22,6 +22,8 @@ browser.runtime.onMessage.addListener((msg, sender) => {
return listUsers();
case 'save_note':
return saveNote(msg);
case 'upload_attachment':
return uploadAttachment(msg);
default:
return Promise.reject(new Error('Unknown message kind'));
}
@@ -69,3 +71,33 @@ async function saveNote(msg) {
body: JSON.stringify(body),
});
}
async function uploadAttachment(msg) {
const base = await getServerUrl();
if (!base) {
throw new Error('Server URL not set — open snotes and click "Capture" → "Apply to Extension", or set it in Options');
}
const blob = dataUrlToBlob(msg.dataUrl);
const form = new FormData();
form.append('file', blob, msg.filename || 'page.png');
if (msg.username) form.append('username', msg.username);
const res = await fetch(base + '/api/attachments', {
method: 'POST',
body: form,
credentials: 'include',
});
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.detail || 'HTTP ' + res.status);
}
return res.json();
}
function dataUrlToBlob(dataUrl) {
const [meta, b64] = dataUrl.split(',');
const mime = (meta.match(/data:(.*?);/) || [])[1] || 'image/png';
const bin = atob(b64);
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
return new Blob([bytes], { type: mime });
}