Brain Service: - Playwright stealth crawler replacing browserless (og:image, Readability, Reddit JSON API) - AI classification with tag definitions and folder assignment - YouTube video download via yt-dlp - Karakeep migration complete (96 items) - Taxonomy management (folders with icons/colors, tags) - Discovery shuffle, sort options, search (Meilisearch + pgvector) - Item tag/folder editing, card color accents RSS Reader Service: - Custom FastAPI reader replacing Miniflux - Feed management (add/delete/refresh), category support - Full article extraction via Readability - Background content fetching for new entries - Mark all read with confirmation - Infinite scroll, retention cleanup (30/60 day) - 17 feeds migrated from Miniflux iOS App (SwiftUI): - Native iOS 17+ app with @Observable architecture - Cookie-based auth, configurable gateway URL - Dashboard with custom background photo + frosted glass widgets - Full fitness module (today/templates/goals/food library) - AI assistant chat (fitness + brain, raw JSON state management) - 120fps ProMotion support AI Assistants (Gateway): - Unified dispatcher with fitness/brain domain detection - Fitness: natural language food logging, photo analysis, multi-item splitting - Brain: save/append/update/delete notes, search & answer, undo support - Madiha user gets fitness-only (brain disabled) Firefox Extension: - One-click save to Brain from any page - Login with platform credentials - Right-click context menu (save page/link/image) - Notes field for URL saves - Signed and published on AMO Other: - Reader bookmark button routes to Brain (was Karakeep) - Fitness food library with "Add" button + add-to-meal popup - Kindle send file size check (25MB SMTP2GO limit) - Atelier UI as default (useAtelierShell=true) - Mobile upload box in nav drawer Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
251 lines
7.0 KiB
JavaScript
251 lines
7.0 KiB
JavaScript
"use strict";
|
|
|
|
var API_BASE = "https://dash.quadjourney.com";
|
|
|
|
console.log("[Brain] Background script loaded");
|
|
|
|
// ── Auth helpers ──
|
|
|
|
function getSession() {
|
|
return browser.storage.local.get("brainSession").then(function(data) {
|
|
return data.brainSession || null;
|
|
});
|
|
}
|
|
|
|
function apiRequest(path, opts) {
|
|
return getSession().then(function(session) {
|
|
if (!session) throw new Error("Not logged in");
|
|
|
|
opts = opts || {};
|
|
opts.headers = opts.headers || {};
|
|
opts.headers["Cookie"] = "platform_session=" + session;
|
|
opts.credentials = "include";
|
|
|
|
return fetch(API_BASE + path, opts).then(function(resp) {
|
|
if (resp.status === 401 || resp.status === 403) {
|
|
browser.storage.local.remove("brainSession");
|
|
throw new Error("Session expired");
|
|
}
|
|
if (!resp.ok) throw new Error("API error: " + resp.status);
|
|
return resp.json();
|
|
});
|
|
});
|
|
}
|
|
|
|
// ── Save functions ──
|
|
|
|
function saveLink(url, title, note) {
|
|
var body = { type: "link", url: url };
|
|
if (title) body.title = title;
|
|
if (note && note.trim()) body.raw_content = note.trim();
|
|
return apiRequest("/api/brain/items", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
function saveNote(text) {
|
|
return apiRequest("/api/brain/items", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ type: "note", raw_content: text }),
|
|
});
|
|
}
|
|
|
|
function saveImage(imageUrl) {
|
|
return fetch(imageUrl).then(function(resp) {
|
|
if (!resp.ok) throw new Error("Failed to download image");
|
|
return resp.blob();
|
|
}).then(function(blob) {
|
|
var parts = imageUrl.split("/");
|
|
var filename = (parts[parts.length - 1] || "image.jpg").split("?")[0];
|
|
var formData = new FormData();
|
|
formData.append("file", blob, filename);
|
|
|
|
return getSession().then(function(session) {
|
|
if (!session) throw new Error("Not logged in");
|
|
return fetch(API_BASE + "/api/brain/items/upload", {
|
|
method: "POST",
|
|
headers: { "Cookie": "platform_session=" + session },
|
|
credentials: "include",
|
|
body: formData,
|
|
});
|
|
});
|
|
}).then(function(resp) {
|
|
if (!resp.ok) throw new Error("Upload failed: " + resp.status);
|
|
return resp.json();
|
|
});
|
|
}
|
|
|
|
// ── Badge helpers ──
|
|
|
|
function showBadge(tabId, text, color, duration) {
|
|
browser.action.setBadgeText({ text: text, tabId: tabId });
|
|
browser.action.setBadgeBackgroundColor({ color: color, tabId: tabId });
|
|
setTimeout(function() {
|
|
browser.action.setBadgeText({ text: "", tabId: tabId });
|
|
}, duration || 2000);
|
|
}
|
|
|
|
// ── Context menus ──
|
|
|
|
browser.runtime.onInstalled.addListener(function() {
|
|
console.log("[Brain] Creating context menus");
|
|
browser.contextMenus.create({
|
|
id: "save-page",
|
|
title: "Save page to Brain",
|
|
contexts: ["page"],
|
|
});
|
|
browser.contextMenus.create({
|
|
id: "save-link",
|
|
title: "Save link to Brain",
|
|
contexts: ["link"],
|
|
});
|
|
browser.contextMenus.create({
|
|
id: "save-image",
|
|
title: "Save image to Brain",
|
|
contexts: ["image"],
|
|
});
|
|
});
|
|
|
|
browser.contextMenus.onClicked.addListener(function(info, tab) {
|
|
getSession().then(function(session) {
|
|
if (!session) {
|
|
browser.action.openPopup();
|
|
return;
|
|
}
|
|
|
|
var promise;
|
|
if (info.menuItemId === "save-page") {
|
|
promise = saveLink(tab.url, tab.title);
|
|
} else if (info.menuItemId === "save-link") {
|
|
promise = saveLink(info.linkUrl, info.linkText);
|
|
} else if (info.menuItemId === "save-image") {
|
|
promise = saveImage(info.srcUrl);
|
|
}
|
|
|
|
if (promise) {
|
|
promise.then(function() {
|
|
showBadge(tab.id, "OK", "#059669", 2000);
|
|
}).catch(function(e) {
|
|
showBadge(tab.id, "ERR", "#DC2626", 3000);
|
|
console.error("[Brain] Save failed:", e);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
// ── Keyboard shortcut ──
|
|
|
|
browser.commands.onCommand.addListener(function(command) {
|
|
if (command === "save-page") {
|
|
browser.tabs.query({ active: true, currentWindow: true }).then(function(tabs) {
|
|
var tab = tabs[0];
|
|
if (!tab || !tab.url) return;
|
|
|
|
getSession().then(function(session) {
|
|
if (!session) {
|
|
browser.action.openPopup();
|
|
return;
|
|
}
|
|
saveLink(tab.url, tab.title).then(function() {
|
|
showBadge(tab.id, "OK", "#059669", 2000);
|
|
}).catch(function(e) {
|
|
showBadge(tab.id, "ERR", "#DC2626", 3000);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
// ── Message handler (from popup) ──
|
|
|
|
browser.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
|
|
console.log("[Brain] Got message:", msg.action);
|
|
|
|
if (msg.action === "login") {
|
|
fetch(API_BASE + "/api/auth/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
username: msg.username,
|
|
password: msg.password,
|
|
}),
|
|
credentials: "include",
|
|
}).then(function(resp) {
|
|
if (!resp.ok) {
|
|
sendResponse({ success: false, error: "Invalid credentials" });
|
|
return;
|
|
}
|
|
// Wait for cookie to be set
|
|
return new Promise(function(r) { setTimeout(r, 300); }).then(function() {
|
|
return browser.cookies.get({
|
|
url: API_BASE,
|
|
name: "platform_session",
|
|
});
|
|
}).then(function(cookie) {
|
|
if (cookie && cookie.value) {
|
|
return browser.storage.local.set({ brainSession: cookie.value }).then(function() {
|
|
sendResponse({ success: true });
|
|
});
|
|
}
|
|
sendResponse({ success: false, error: "Login OK but could not capture session" });
|
|
});
|
|
}).catch(function(e) {
|
|
console.error("[Brain] Login error:", e);
|
|
sendResponse({ success: false, error: e.message || "Connection failed" });
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (msg.action === "logout") {
|
|
browser.storage.local.remove("brainSession").then(function() {
|
|
sendResponse({ success: true });
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (msg.action === "check-auth") {
|
|
getSession().then(function(session) {
|
|
if (!session) {
|
|
sendResponse({ authenticated: false });
|
|
return;
|
|
}
|
|
return apiRequest("/api/auth/me").then(function(data) {
|
|
sendResponse({ authenticated: true, user: data.user || data });
|
|
}).catch(function() {
|
|
sendResponse({ authenticated: false });
|
|
});
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (msg.action === "save-link") {
|
|
saveLink(msg.url, msg.title, msg.note).then(function(result) {
|
|
sendResponse(result);
|
|
}).catch(function(e) {
|
|
sendResponse({ error: e.message });
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (msg.action === "save-note") {
|
|
saveNote(msg.text).then(function(result) {
|
|
sendResponse(result);
|
|
}).catch(function(e) {
|
|
sendResponse({ error: e.message });
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (msg.action === "save-image") {
|
|
saveImage(msg.url).then(function(result) {
|
|
sendResponse(result);
|
|
}).catch(function(e) {
|
|
sendResponse({ error: e.message });
|
|
});
|
|
return true;
|
|
}
|
|
});
|