This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

semble / src / webapp / services / extensionService.ts
2.0 kB 65 lines
1export interface ExtensionTokens { 2 accessToken: string; 3 refreshToken: string; 4} 5 6export class ExtensionService { 7 private static readonly EXTENSION_MESSAGE_TYPE = 'EXTENSION_TOKENS'; 8 private static readonly EXTENSION_TOKENS_REQUESTED_KEY = 9 'EXTENSION_TOKENS_REQUESTED'; 10 private static readonly EXTENSION_TOKENS_TIMEOUT = 5 * 60 * 1000; // 5 minutes 11 12 static async sendTokensToExtension(tokens: ExtensionTokens): Promise<void> { 13 const extensionId = process.env.NEXT_PUBLIC_EXTENSION_ID; 14 15 try { 16 // Try Chrome extension API first 17 if (extensionId && window.chrome?.runtime) { 18 await window.chrome.runtime.sendMessage(extensionId, { 19 type: this.EXTENSION_MESSAGE_TYPE, 20 ...tokens, 21 }); 22 return; 23 } 24 25 // Fallback to postMessage - content script will forward to background 26 window.postMessage( 27 { 28 type: this.EXTENSION_MESSAGE_TYPE, 29 ...tokens, 30 }, 31 window.location.origin, 32 ); 33 } catch (error) { 34 console.error('Failed to send tokens to extension:', error); 35 throw new Error('Failed to communicate with extension'); 36 } 37 } 38 39 static isExtensionAvailable(): boolean { 40 const extensionId = process.env.NEXT_PUBLIC_EXTENSION_ID; 41 return !!(extensionId && window.chrome?.runtime); 42 } 43 44 static setExtensionTokensRequested(): void { 45 localStorage.setItem( 46 this.EXTENSION_TOKENS_REQUESTED_KEY, 47 Date.now().toString(), 48 ); 49 } 50 51 static isExtensionTokensRequested(): boolean { 52 const timestamp = localStorage.getItem(this.EXTENSION_TOKENS_REQUESTED_KEY); 53 if (!timestamp) return false; 54 55 const requestTime = parseInt(timestamp, 10); 56 const now = Date.now(); 57 58 // Check if request was made within the last 5 minutes 59 return now - requestTime < this.EXTENSION_TOKENS_TIMEOUT; 60 } 61 62 static clearExtensionTokensRequested(): void { 63 localStorage.removeItem(this.EXTENSION_TOKENS_REQUESTED_KEY); 64 } 65}