feat: replace browser login with imported Reddit sessions

This commit is contained in:
jjbrunton committed 2026-08-08 16:48:59 +01:00
1 parent 2b0545afcf
commit c483c7b8be
76 files changed
+2048 -2828

No files matched your search

@@ -0,0 +1,58 @@
import { createSessionExportDownload } from './session-export.js';
const MATRIX_URLS = [
'https://matrix.redditspace.com/*',
'https://matrix.reddit.com/*',
];
chrome.webRequest.onBeforeSendHeaders.addListener(
async (details) => {
const authorization = details.requestHeaders?.find(
(header) => header.name.toLowerCase() === 'authorization',
)?.value;
if (!authorization?.toLowerCase().startsWith('bearer ')) return;
const url = new URL(details.url);
await chrome.storage.local.set({
matrixCapture: {
homeserver: `${url.protocol}//${url.host}`,
accessToken: authorization.slice('bearer '.length).trim(),
capturedAt: Date.now(),
},
});
await chrome.action.setBadgeText({ text: 'OK' });
await chrome.action.setBadgeBackgroundColor({ color: '#198754' });
},
{ urls: MATRIX_URLS },
['requestHeaders', 'extraHeaders'],
);
async function showError(message) {
await chrome.action.setBadgeText({ text: 'ERR' });
await chrome.action.setBadgeBackgroundColor({ color: '#dc3545' });
await chrome.action.setTitle({ title: `RedditBot export not ready: ${message}` });
}
chrome.action.onClicked.addListener(async () => {
try {
const cookies = await chrome.cookies.getAll({ domain: 'reddit.com' });
const { matrixCapture } = await chrome.storage.local.get('matrixCapture');
if (!matrixCapture) {
throw new Error('Open Reddit Chat, wait for it to load, then click the extension again.');
}
const whoami = await fetch(`${matrixCapture.homeserver}/_matrix/client/v3/account/whoami`, {
headers: { Authorization: `Bearer ${matrixCapture.accessToken}` },
});
if (!whoami.ok) throw new Error('The captured Reddit Chat token has expired. Reload Reddit Chat and try again.');
const identity = await whoami.json();
if (!identity?.user_id) throw new Error('Reddit Chat did not return an account identity.');
await chrome.downloads.download({
...createSessionExportDownload(cookies, { ...matrixCapture, userId: identity.user_id }),
saveAs: true,
});
} catch (error) {
await showError(error instanceof Error ? error.message : 'Could not create the session dump.');
}
});