(
tabId: number,
target: CaptureTarget,
context: { linkUrl?: string, selectionText?: string },
)
| 48 | }) |
| 49 | |
| 50 | async function captureFromTab( |
| 51 | tabId: number, |
| 52 | target: CaptureTarget, |
| 53 | context: { linkUrl?: string, selectionText?: string }, |
| 54 | ): Promise<void> { |
| 55 | try { |
| 56 | const settings = await getSettings() |
| 57 | if (!settings.apiToken.trim()) { |
| 58 | throw new Error('Set the massCode API token in the extension popup.') |
| 59 | } |
| 60 | |
| 61 | const payload = await getPageCapture(tabId) |
| 62 | const request = buildCaptureRequest(target, { |
| 63 | ...payload, |
| 64 | selectedText: payload.selectedText || context.selectionText?.trim() || '', |
| 65 | url: context.linkUrl || payload.url, |
| 66 | }) |
| 67 | |
| 68 | const result = await postCapture(settings, request) |
| 69 | await notify( |
| 70 | 'Saved to massCode', |
| 71 | `Created ${result.target} item #${result.id}.`, |
| 72 | ) |
| 73 | } |
| 74 | catch (error) { |
| 75 | await notify('massCode capture failed', getErrorMessage(error)) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | async function getPageCapture(tabId: number): Promise<PageCapturePayload> { |
| 80 | const [response] = await chrome.scripting.executeScript<PageCapturePayload>({ |
no test coverage detected