Files
snotes/browser-extension/background.js
2026-09-23 13:51:53 -07:00

72 lines
1.8 KiB
JavaScript

let serverUrl = '';
function norm(v) {
return (v || '').replace(/\/+$/, '');
}
async function getServerUrl() {
const r = await browser.storage.sync.get('serverUrl');
serverUrl = norm(r.serverUrl);
return serverUrl;
}
browser.storage.onChanged.addListener(changes => {
if (changes.serverUrl) {
serverUrl = norm(changes.serverUrl.newValue);
}
});
browser.runtime.onMessage.addListener((msg, sender) => {
switch (msg.kind) {
case 'list_users':
return listUsers();
case 'save_note':
return saveNote(msg);
default:
return Promise.reject(new Error('Unknown message kind'));
}
});
async function request(path, opts) {
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');
}
opts = opts || {};
opts.credentials = 'include';
let res;
try {
res = await fetch(base + path, opts);
} catch (e) {
throw new Error('Cannot reach ' + base + ' (' + e.message + ')');
}
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.detail || 'HTTP ' + res.status);
}
return res.json();
}
async function listUsers() {
const users = await request('/api/users', { headers: { 'Accept': 'application/json' } });
return { users: users };
}
async function saveNote(msg) {
const body = { body: msg.body };
if (msg.username) body.username = msg.username;
if (msg.id) {
return request('/api/notes/' + msg.id, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
}
return request('/api/notes', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
}