class o{menuId="select-ai-ocr-image";screenshotMenuId="select-ai-screenshot-ocr";listenerRegistered=!1;constructor(){this.registerClickListener()}registerClickListener(){this.listenerRegistered||(this.listenerRegistered=!0,chrome.contextMenus.onClicked.addListener((e,t)=>{e.menuItemId===this.menuId?this.delegateToContentScript(t?.id,e.srcUrl):e.menuItemId===this.screenshotMenuId&&this.triggerScreenshot(t?.id)}))}async createMenus(){this.destroy(),chrome.contextMenus.create({id:this.menuId,title:"AI 图片解释",contexts:["image"]}),chrome.contextMenus.create({id:this.screenshotMenuId,title:"截图识别",contexts:["all"]})}delegateToContentScript(e,t){!e||!t||chrome.tabs.sendMessage(e,{action:"ocr-from-context-menu",imageUrl:t},r=>{chrome.runtime.lastError?(console.error("[ContextMenu] Failed to send to content script:",chrome.runtime.lastError.message),this.showNotification("操作失败，请刷新页面后重试")):r?.error&&(console.error("[ContextMenu] Content script error:",r.error),this.showNotification(r.error))})}triggerScreenshot(e){e&&chrome.tabs.sendMessage(e,{action:"start-screenshot"},t=>{chrome.runtime.lastError?(console.error("[ContextMenu] Failed to send screenshot message:",chrome.runtime.lastError.message),this.showNotification("操作失败，请刷新页面后重试")):t?.error&&(console.error("[ContextMenu] Content script error:",t.error),this.showNotification(t.error))})}showNotification(e){chrome.notifications.create({type:"basic",iconUrl:"app-icon.png",title:"Select AI",message:e})}destroy(){try{chrome.contextMenus.removeAll()}catch(e){console.warn("[ContextMenu] Failed to remove menus:",e)}}}const n=new o;export{n as contextMenuService};
