Something went wrong. Try again.
A social knowledge tool for research trails, built on ATProto semble.so
research-tool social-network tools-for-thought atproto
Something went wrong. Try again.
TypeScript
at development
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124// Background script for handling extension messaging and auth statechrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.type === 'EXTENSION_TOKENS') { handleExtensionTokens(message, sendResponse); return true; // Keep message channel open for async response }
if (message.type === 'WEBAPP_TOKENS_RECEIVED') { handleWebappTokens(message, sendResponse); return true; }});
// Handle tokens received directly via chrome.runtime.sendMessageasync function handleExtensionTokens( message: any, sendResponse: (response: any) => void,) { try { const { accessToken, refreshToken } = message;
if (!accessToken) { sendResponse({ success: false, error: 'No access token provided' }); return; }
// Store tokens in chrome.storage await chrome.storage.local.set({ accessToken, refreshToken, isAuthenticated: true, authTimestamp: Date.now(), });
// Notify all extension pages about the auth change chrome.runtime .sendMessage({ type: 'AUTH_STATE_CHANGED', isAuthenticated: true, }) .catch(() => { // Ignore errors if no listeners });
sendResponse({ success: true }); } catch (error) { console.error('Failed to handle extension tokens:', error); sendResponse({ success: false, error: (error as Error).message }); }}
// Handle tokens forwarded from content script (postMessage)async function handleWebappTokens( message: any, sendResponse: (response: any) => void,) { try { const { accessToken, refreshToken } = message;
if (!accessToken) { sendResponse({ success: false, error: 'No access token provided' }); return; }
// Store tokens in chrome.storage await chrome.storage.local.set({ accessToken, refreshToken, isAuthenticated: true, authTimestamp: Date.now(), });
// Notify all extension pages about the auth change chrome.runtime .sendMessage({ type: 'AUTH_STATE_CHANGED', isAuthenticated: true, }) .catch(() => { // Ignore errors if no listeners });
sendResponse({ success: true }); } catch (error) { console.error('Failed to handle webapp tokens:', error); sendResponse({ success: false, error: (error as Error).message }); }}
// Handle auth logoutchrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.type === 'LOGOUT') { handleLogout(sendResponse); return true; }});
async function handleLogout(sendResponse: (response: any) => void) { try { // Clear all auth data await chrome.storage.local.remove([ 'accessToken', 'refreshToken', 'isAuthenticated', 'authTimestamp', ]);
// Notify all extension pages about the auth change chrome.runtime .sendMessage({ type: 'AUTH_STATE_CHANGED', isAuthenticated: false, }) .catch(() => { // Ignore errors if no listeners });
sendResponse({ success: true }); } catch (error) { console.error('Failed to handle logout:', error); sendResponse({ success: false, error: (error as Error).message }); }}