import { createController } from './controller.js' const controller = createController({ chrome }) chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }).catch(() => {}) // The side panel talks to the controller; nothing else may. chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { const type = String(message?.type || '') if (sender.id !== chrome.runtime.id || !type.startsWith('control.') || type === 'control.state') return false controller.onMessage(message, sender).then(sendResponse, error => sendResponse({ failure: String(error?.message || error) })) return true }) chrome.debugger.onDetach.addListener((source, reason) => controller.onDebuggerDetach(source, reason)) chrome.tabs.onRemoved.addListener(tabId => { void controller.onTabRemoved(tabId) }) chrome.alarms.onAlarm.addListener(alarm => controller.onAlarm(alarm.name)) const MENU = [ { id: 'aiturk-selection', title: 'AITURK’e gönder: seçili metin', contexts: ['selection'] }, { id: 'aiturk-page', title: 'AITURK’e gönder: bu sayfa', contexts: ['page'] }, { id: 'aiturk-share', title: 'Bu sekmeyi AITURK ajanına ver', contexts: ['page'] }, ] chrome.runtime.onInstalled.addListener(() => { chrome.contextMenus.removeAll(() => { for (const item of MENU) chrome.contextMenus.create(item) }) }) chrome.contextMenus.onClicked.addListener((info, tab) => { if (!tab?.windowId) return // Opening the panel must happen inside the click's user gesture, before any await. chrome.sidePanel.open({ windowId: tab.windowId }).catch(() => {}) if (info.menuItemId === 'aiturk-share') { void chrome.storage.local.get('selectedSession') .then(saved => controller.shareTab(tab, typeof saved.selectedSession === 'string' ? saved.selectedSession : null)) .catch(error => chrome.storage.session.set({ pendingNotice: String(error?.message || error) })) return } void chrome.storage.session.set({ pendingContext: { kind: info.menuItemId === 'aiturk-selection' ? 'selection' : 'page', tabId: tab.id, text: info.selectionText ? info.selectionText.slice(0, 24000) : '', at: Date.now(), } }) }) void controller.connect()