diff --git a/packages/plugin/background/background.ts b/packages/plugin/background/background.ts index d3a95bf..002c10d 100644 --- a/packages/plugin/background/background.ts +++ b/packages/plugin/background/background.ts @@ -2,9 +2,30 @@ import Browser from 'webextension-polyfill' import '~/lib/browser-polyfill.min.js' import '~/lib/single-file-background.js' import { onMessage } from 'webext-bridge/background' +import { isNotNil } from '@web-archive/shared/utils' import { clearFinishedTaskList, createAndRunTask, getTaskList } from './processor' import { checkLoginStatus, getCacheLoginStatus, resetLoginStatus } from './login' +Browser.runtime.onInstalled.addListener(async () => { + const tags = await Browser.tabs.query({}) + + // inject content script to all tabs when installed + tags + .filter(tag => isNotNil(tag.id)) + .forEach(async (tag) => { + try { + console.log('inject content when installed', tag.title) + await Browser.scripting.executeScript({ + target: { tabId: tag.id! }, + files: ['lib/browser-polyfill.min.js', 'contentScripts/main.js'], + }) + } + catch (e) { + // ignore inject error + } + }) +}) + async function appendAuthHeader(options?: RequestInit) { const { token } = await Browser.storage.local.get('token') ?? {} if (!options) { diff --git a/packages/plugin/background/processor.ts b/packages/plugin/background/processor.ts index fb62b4a..6eca53f 100644 --- a/packages/plugin/background/processor.ts +++ b/packages/plugin/background/processor.ts @@ -15,6 +15,7 @@ export interface SeriableSingleFileTask { folderId: string startTimeStamp: number endTimeStamp?: number + errorMessage?: string } const taskList: SeriableSingleFileTask[] = [] @@ -31,6 +32,7 @@ async function initTask() { if (task.status !== 'done' && task.status !== 'failed') { task.status = 'failed' task.endTimeStamp = Date.now() + task.errorMessage = 'unexpected shutdown' } }) taskList.splice(0, taskList.length, ...tasks) @@ -39,6 +41,7 @@ async function initTask() { } Browser.runtime.onStartup.addListener(async () => { + console.log('onStartup') await initTask() }) @@ -133,9 +136,11 @@ async function createAndRunTask(options: CreateTaskOptions) { try { await run() } - catch (e) { + catch (e: any) { task.status = 'failed' task.endTimeStamp = Date.now() + console.error('tsak failed', e, task) + task.errorMessage = typeof e === 'string' ? e : e.message await saveTaskList() } } diff --git a/packages/plugin/popup/components/HistoryTaskList.tsx b/packages/plugin/popup/components/HistoryTaskList.tsx index 2f27111..7015201 100644 --- a/packages/plugin/popup/components/HistoryTaskList.tsx +++ b/packages/plugin/popup/components/HistoryTaskList.tsx @@ -80,13 +80,13 @@ function TaskListItem({ task }: { task: SeriableSingleFileTask }) {
{task.title}
{runningTimeText}
- +
) } -function TaskStatusIcon({ status }: { status: 'init' | 'scraping' | 'uploading' | 'done' | 'failed' }) { +function TaskStatusIcon({ status, errorMessage }: { status: 'init' | 'scraping' | 'uploading' | 'done' | 'failed', errorMessage?: string }) { if (status === 'init' || status === 'scraping' || status === 'uploading') { return ( + + + + + + + {errorMessage} + + + + ) } return ( diff --git a/packages/plugin/popup/components/LoginPage.tsx b/packages/plugin/popup/components/LoginPage.tsx index 8bed063..e40628f 100644 --- a/packages/plugin/popup/components/LoginPage.tsx +++ b/packages/plugin/popup/components/LoginPage.tsx @@ -38,6 +38,7 @@ function LoginPage({ setActivePage }: { setActivePage: (tab: PageType) => void }