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(); return listUsers();
case 'save_note': case 'save_note':
return saveNote(msg); return saveNote(msg);
case 'upload_attachment':
return uploadAttachment(msg);
default: default:
return Promise.reject(new Error('Unknown message kind')); return Promise.reject(new Error('Unknown message kind'));
} }
@@ -69,3 +71,33 @@ async function saveNote(msg) {
body: JSON.stringify(body), 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 });
}

View File

@@ -4,10 +4,11 @@
"version": "1.0.2", "version": "1.0.2",
"description": "Quick-capture a note to snotes from any page, with the selected text and page link ready to use", "description": "Quick-capture a note to snotes from any page, with the selected text and page link ready to use",
"permissions": [ "permissions": [
"storage" "storage",
"activeTab"
], ],
"content_security_policy": { "content_security_policy": {
"extension_pages": "default-src 'self'; script-src 'self'; style-src 'self'; connect-src http://* https://*" "extension_pages": "default-src 'self'; script-src 'self'; style-src 'self'; connect-src http: https:"
}, },
"host_permissions": [ "host_permissions": [
"http://*/*", "http://*/*",

View File

@@ -6,7 +6,7 @@
</head> </head>
<body> <body>
<div class="header"> <div class="header">
<img src="icons/icon-48.png" alt="" class="logo"> <img src="icons/icon-48.png" alt="" class="logo" id="logo">
<select id="user" title="User / space"></select> <select id="user" title="User / space"></select>
</div> </div>
<textarea id="note" placeholder="Type a note…" spellcheck="false"></textarea> <textarea id="note" placeholder="Type a note…" spellcheck="false"></textarea>

View File

@@ -25,6 +25,7 @@
try { try {
const r = await browser.runtime.sendMessage({ kind: 'list_users' }); const r = await browser.runtime.sendMessage({ kind: 'list_users' });
const users = (r && r.users) || []; const users = (r && r.users) || [];
const saved = await browser.storage.sync.get('username').then(x => x.username);
userEl.innerHTML = ''; userEl.innerHTML = '';
for (const u of users) { for (const u of users) {
const opt = document.createElement('option'); const opt = document.createElement('option');
@@ -32,6 +33,9 @@
opt.textContent = u.name; opt.textContent = u.name;
userEl.appendChild(opt); userEl.appendChild(opt);
} }
if (saved && [...userEl.options].some(o => o.value === saved)) {
userEl.value = saved;
}
} catch (e) { } catch (e) {
showStatus('Cannot load users: ' + e.message, false); showStatus('Cannot load users: ' + e.message, false);
} }
@@ -54,7 +58,7 @@
} }
} }
function capturePage() { async function capturePage() {
var title = noteEl.dataset.pageTitle || ''; var title = noteEl.dataset.pageTitle || '';
var url = noteEl.dataset.pageUrl || ''; var url = noteEl.dataset.pageUrl || '';
if (!title && !url) { showStatus('No page to capture', false); return; } if (!title && !url) { showStatus('No page to capture', false); return; }
@@ -62,6 +66,24 @@
var v = noteEl.value; var v = noteEl.value;
if (v && !/\n$/.test(v)) v += '\n'; if (v && !/\n$/.test(v)) v += '\n';
noteEl.value = v + link + '\n'; noteEl.value = v + link + '\n';
captureBtn.disabled = true;
try {
const dataUrl = await browser.tabs.captureVisibleTab({ format: 'png' });
const r = await browser.runtime.sendMessage({
kind: 'upload_attachment',
dataUrl: dataUrl,
filename: 'page.png',
username: userEl.value,
});
noteEl.value += '![Screenshot](' + r.url + ')\n';
showStatus('Screenshot attached', true);
} catch (e) {
showStatus('Screenshot failed: ' + e.message, false);
} finally {
captureBtn.disabled = false;
}
noteEl.focus(); noteEl.focus();
scheduleAutosave(); scheduleAutosave();
} }
@@ -99,6 +121,17 @@
noteEl.addEventListener('input', scheduleAutosave); noteEl.addEventListener('input', scheduleAutosave);
userEl.addEventListener('change', function () {
browser.storage.sync.set({ username: userEl.value });
});
document.getElementById('logo').addEventListener('click', function () {
browser.storage.sync.get('serverUrl').then(function (r) {
var url = (r.serverUrl || '').replace(/\/+$/, '');
if (url) browser.tabs.create({ url: url });
});
});
saveBtn.addEventListener('click', function () { saveBtn.addEventListener('click', function () {
if (autosaveTimer) { clearTimeout(autosaveTimer); autosaveTimer = null; } if (autosaveTimer) { clearTimeout(autosaveTimer); autosaveTimer = null; }
save(); save();