Skip to content

Commit

Permalink
Merge pull request #18 from Ray-D-Song/plugin
Browse files Browse the repository at this point in the history
Plugin
  • Loading branch information
Ray-D-Song authored Oct 30, 2024
2 parents 373f70f + 4e46259 commit c8503a2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
21 changes: 21 additions & 0 deletions packages/plugin/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin/background/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface SeriableSingleFileTask {
folderId: string
startTimeStamp: number
endTimeStamp?: number
errorMessage?: string
}

const taskList: SeriableSingleFileTask[] = []
Expand All @@ -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)
Expand All @@ -39,6 +41,7 @@ async function initTask() {
}

Browser.runtime.onStartup.addListener(async () => {
console.log('onStartup')
await initTask()
})

Expand Down Expand Up @@ -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()
}
}
Expand Down
22 changes: 16 additions & 6 deletions packages/plugin/popup/components/HistoryTaskList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ function TaskListItem({ task }: { task: SeriableSingleFileTask }) {
<div className="font-bold cursor-pointer overflow-hidden text-ellipsis text-nowrap" onClick={openOriginalPage}>{task.title}</div>
<div>{runningTimeText}</div>
<div className="flex mt-0.5">
<TaskStatusIcon status={task.status}></TaskStatusIcon>
<TaskStatusIcon status={task.status} errorMessage={task.errorMessage}></TaskStatusIcon>
</div>
</div>
)
}

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 (
<LoaderCircle
Expand All @@ -107,10 +107,20 @@ function TaskStatusIcon({ status }: { status: 'init' | 'scraping' | 'uploading'

if (status === 'failed') {
return (
<ClockAlert
size={14}
className="text-destructive"
/>
<TooltipProvider delayDuration={200}>
<Tooltip>
<TooltipTrigger asChild>
<ClockAlert
size={14}
className="text-destructive"
/>
</TooltipTrigger>
<TooltipContent side="top" className="max-w-60">
{errorMessage}
</TooltipContent>
</Tooltip>
</TooltipProvider>

)
}
return (
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/popup/components/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function LoginPage({ setActivePage }: { setActivePage: (tab: PageType) => void }
<Input
id="token"
placeholder="Enter the token"
type="password"
value={token}
onChange={saveToken}
/>
Expand Down

0 comments on commit c8503a2

Please sign in to comment.