diff --git a/.changeset/wiki-hierarchy-refresh.md b/.changeset/wiki-hierarchy-refresh.md new file mode 100644 index 0000000..718df16 --- /dev/null +++ b/.changeset/wiki-hierarchy-refresh.md @@ -0,0 +1,5 @@ +--- +'@dolphin/chrome-extension': minor +--- + +Batch Download: optionally rebuild the tree from a Feishu/Lark wiki (in sidebar order) before each export, with a diff preview before anything is replaced. diff --git a/README.md b/README.md index 99bd162..597d7f6 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,14 @@ Install Cloud Document Converter from [Chrome Web Store](https://chromewebstore. > ❗Note: The URL of the image generated by this way only has two hours of accessibility. This means that after two hours, the image resource will no longer be accessible. +- Batch download many Lark Docs as Markdown in one ZIP (extension popup → **Batch Download**) + + - **Refresh from Feishu wiki**: paste a wiki page URL and click **Refresh now**. The extension reads that page and every page under it, in sidebar order, and builds the download tree: each page becomes `001 - title`, and a page with children is followed by a folder of the same name. You see a preview of added, removed, renamed and moved pages, and the tree changes only when you click **Apply**. + - **Refresh before each export** (off by default): if the wiki is unchanged, the export starts at once. If it changed, choose **Apply and export**, **Export current tree** or **Cancel export**. + - Pages are matched by their wiki ID, so pages already exported in this session keep their result. Uploaded files (such as `.py` or `.pdf` attachments) are skipped and listed with links. + - **Advanced options**, saved per wiki: pages to place before the root, title overrides, and a filter to keep only some top-level sections (numbers stay as in the full tree). + - You need to be signed in to Feishu in the same browser. No extra permissions are requested. + # Compatibility ## Block diff --git a/README_ZH.md b/README_ZH.md index b1843c3..c4f49de 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -21,6 +21,14 @@ Cloud Document Converter 是一个浏览器扩展, 支持下载、复制飞书 > ❗注意: 通过这种方式生成的图像 URL 只有两个小时的访问时间。这意味着两小时后,将无法再访问图像资源。 +- 批量下载多篇飞书云文档为 Markdown,打包成一个 ZIP(扩展弹窗 → **批量下载**) + + - **从飞书知识库刷新**:粘贴知识库页面链接,点击 **立即刷新**。扩展会按侧边栏顺序读取该页面及其下所有页面,生成下载目录树:每个页面命名为 `001 - 标题`,有子页面的页面后面跟一个同名文件夹。刷新后先显示新增、删除、改名和移动的页面,点击 **应用** 后才会替换目录树。 + - **每次导出前刷新**(默认关闭):知识库没有变化时直接开始导出;有变化时可选择 **应用并导出**、**按当前目录树导出** 或 **取消导出**。 + - 页面按知识库 ID 匹配,本次会话中已导出的页面保留原有结果。上传的文件(如 `.py`、`.pdf` 附件)会被跳过,并附链接列出。 + - **高级选项**按知识库分别保存:放在根页面之前的页面、标题覆盖、仅保留部分顶级章节(编号仍与完整目录树一致)。 + - 需要在同一浏览器中登录飞书。不需要额外权限。 + # 兼容性 ## 块级元素 diff --git a/apps/chrome-extension-e2e/tests/batch-download-wiki-refresh.e2e.test.ts b/apps/chrome-extension-e2e/tests/batch-download-wiki-refresh.e2e.test.ts new file mode 100644 index 0000000..8ddaddb --- /dev/null +++ b/apps/chrome-extension-e2e/tests/batch-download-wiki-refresh.e2e.test.ts @@ -0,0 +1,310 @@ +import fs from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import { + chromium, + expect, + test, + type BrowserContext, + type Page, + type Route, +} from '@playwright/test' + +/** + * Drives the real built extension's Batch Download page against a synthetic + * Feishu wiki served by request interception. No Feishu account is used. + */ + +const dirname = path.dirname(fileURLToPath(import.meta.url)) +const extensionPath = path.resolve(dirname, '../.cache/extension') +const ORIGIN = 'https://e2e.feishu.cn' +const ROOT_URL = `${ORIGIN}/wiki/ROOT` +const SPACE = '7000000000000000001' + +interface FakePage { + title: string + children?: string[] +} + +const makeWiki = (): Partial> => ({ + ROOT: { title: 'Handbook', children: ['A', 'B'] }, + A: { title: '1. Basics', children: ['A1', 'A2'] }, + A1: { title: '1.1 Tokens' }, + A2: { title: '1.2 Attention' }, + B: { title: '2. Training' }, +}) + +interface Harness { + context: BrowserContext + page: Page + wiki: Partial> + apiCalls: { fromExtension: number; fromTab: number } + cleanup: () => Promise +} + +/** + * @param cookiesOnlyInTab When true, API calls made directly by the extension + * page get a login page (as if the session cookie were not sent), so the + * page must fall back to fetching inside a Feishu tab. + */ +async function launch(cookiesOnlyInTab = false): Promise { + const userDataDir = await fs.mkdtemp( + path.join(os.tmpdir(), 'cdc-e2e-wiki-refresh-'), + ) + const context = await chromium.launchPersistentContext(userDataDir, { + channel: 'chromium', + // Optional: point at a locally cached Chromium when the pinned one is missing. + executablePath: process.env['CDC_E2E_CHROMIUM_PATH'], + headless: true, + args: [ + `--disable-extensions-except=${extensionPath}`, + `--load-extension=${extensionPath}`, + ], + }) + const wiki = makeWiki() + const apiCalls = { fromExtension: 0, fromTab: 0 } + + await context.route( + /^https:\/\/[^/]+\.feishu\.cn\//, + async (route: Route) => { + const request = route.request() + const url = new URL(request.url()) + let frameUrl = '' + try { + frameUrl = request.frame().url() + } catch { + // service-worker requests have no frame + } + const fromExtension = frameUrl.startsWith('chrome-extension://') + + if (url.pathname.startsWith('/space/api/wiki/v2/tree/')) { + if (fromExtension) apiCalls.fromExtension++ + else apiCalls.fromTab++ + if (cookiesOnlyInTab && fromExtension) { + await route.fulfill({ + contentType: 'text/html', + body: 'login', + }) + return + } + const token = url.searchParams.get('wiki_token') ?? '' + const node = wiki[token] + if (!node) { + await route.fulfill({ + contentType: 'application/json', + body: JSON.stringify({ code: 920004012, msg: 'no permission' }), + }) + return + } + const body = url.pathname.endsWith('/get_info/') + ? { + code: 0, + data: { + tree: { + nodes: { + [token]: { + title: node.title, + space_id: SPACE, + has_child: Boolean(node.children?.length), + }, + }, + }, + }, + } + : { + code: 0, + data: { + [token]: (node.children ?? []).map((t, i) => ({ + wiki_token: t, + title: wiki[t]?.title, + sort_id: i + 1, + has_child: Boolean(wiki[t]?.children?.length), + obj_type: 22, + })), + }, + } + await route.fulfill({ + contentType: 'application/json', + headers: fromExtension + ? { + 'access-control-allow-origin': + request.headers()['origin'] ?? '*', + 'access-control-allow-credentials': 'true', + } + : {}, + body: JSON.stringify(body), + }) + return + } + if (url.pathname.startsWith('/wiki/')) { + await route.fulfill({ + contentType: 'text/html', + body: 'wikiwiki', + }) + return + } + await route.fulfill({ status: 404, body: '' }) + }, + ) + + let serviceWorker = context.serviceWorkers().at(0) + serviceWorker ??= await context.waitForEvent('serviceworker') + const extensionId = new URL(serviceWorker.url()).host + // An open (stubbed) Feishu tab, so the tab fallback reuses it. Tabs opened + // by the extension itself would bypass request interception and load the + // real site, so the test never lets the fetcher open its own tab. + const feishuTab = await context.newPage() + await feishuTab.goto(ROOT_URL) + const page = await context.newPage() + await page.goto(`chrome-extension://${extensionId}/pages/batch-download.html`) + + return { + context, + page, + wiki, + apiCalls, + cleanup: async () => { + await context.close() + await fs.rm(userDataDir, { recursive: true, force: true }) + }, + } +} + +const urlInput = (page: Page) => + page.getByPlaceholder('https://xxx.feishu.cn/wiki/...') + +test('refresh now previews the wiki, applies it, and remembers the wiki', async () => { + const h = await launch() + try { + const { page } = h + await urlInput(page).fill(ROOT_URL) + await page.getByTestId('refresh-now').click() + + const preview = page.getByTestId('refresh-preview') + await expect(preview).toBeVisible() + await expect(preview).toContainText('0 → 5 pages: 5 added') + await expect(preview).toContainText('+ 1.1 Tokens [1. Basics]') + + await page.getByTestId('refresh-apply').click() + await expect(preview).toBeHidden() + await expect(page.getByText('000 - Handbook')).toBeVisible() + await expect( + page.getByText('Applied refreshed tree: 5 files'), + ).toBeVisible() + + // The direct (extension page) path worked, so no tab was needed. + expect(h.apiCalls.fromExtension).toBeGreaterThan(0) + expect(h.apiCalls.fromTab).toBe(0) + + // Settings and tree survive a reload. + await page.reload() + await expect(urlInput(page)).toHaveValue(ROOT_URL) + await expect(page.getByText('000 - Handbook')).toBeVisible() + } finally { + await h.cleanup() + } +}) + +test('refresh before export waits for a decision when the wiki changed', async () => { + const h = await launch() + try { + const { page, wiki } = h + await urlInput(page).fill(ROOT_URL) + await page.getByTestId('refresh-now').click() + await page.getByTestId('refresh-apply').click() + await page.getByTestId('refresh-before-run').check() + + // Feishu renames a page after the tree was built. + wiki['A2'] = { title: '1.2 Self-Attention' } + + const exportButton = page.getByRole('button', { name: 'Export ZIP' }) + await exportButton.click() + const preview = page.getByTestId('refresh-preview') + await expect(preview).toContainText('1 renamed') + await expect(preview).toContainText( + '~ 1.2 Attention → 1.2 Self-Attention', + ) + + // Cancel: no run starts and the tree is unchanged. + await page.getByRole('button', { name: 'Cancel export' }).click() + await expect(preview).toBeHidden() + await expect(page.getByRole('button', { name: 'Pause' })).toHaveCount(0) + await expect(exportButton).toBeEnabled() + + // Apply and export: the new tree is applied and the run starts. + await exportButton.click() + await page.getByRole('button', { name: 'Apply and export' }).click() + await expect(page.getByRole('button', { name: 'Pause' })).toBeVisible() + await expect( + page.getByText('Applied refreshed tree: 5 files'), + ).toBeVisible() + // Refreshing is locked while the run is active. + await expect(page.getByTestId('refresh-now')).toBeDisabled() + } finally { + await h.cleanup() + } +}) + +test('refresh before export starts at once when nothing changed', async () => { + const h = await launch() + try { + const { page } = h + await urlInput(page).fill(ROOT_URL) + await page.getByTestId('refresh-now').click() + await page.getByTestId('refresh-apply').click() + await page.getByTestId('refresh-before-run').check() + + await page.getByRole('button', { name: 'Export ZIP' }).click() + await expect(page.getByTestId('refresh-preview')).toBeHidden() + await expect( + page.getByText('The wiki matches the current tree.'), + ).toBeVisible() + await expect(page.getByRole('button', { name: 'Pause' })).toBeVisible() + } finally { + await h.cleanup() + } +}) + +test('a failed walk leaves the tree untouched and does not export', async () => { + const h = await launch() + try { + const { page, wiki } = h + await urlInput(page).fill(ROOT_URL) + await page.getByTestId('refresh-now').click() + await page.getByTestId('refresh-apply').click() + await page.getByTestId('refresh-before-run').check() + + // The root page disappears from the wiki: get_info returns no node. + delete wiki['ROOT'] + await page.getByRole('button', { name: 'Export ZIP' }).click() + await expect(page.getByText('Wiki refresh failed')).toBeVisible({ + timeout: 40_000, + }) + await expect(page.getByRole('button', { name: 'Pause' })).toHaveCount(0) + await expect(page.getByText('000 - Handbook')).toBeVisible() + } finally { + await h.cleanup() + } +}) + +test('falls back to fetching inside a Feishu tab when cookies are not sent', async () => { + const h = await launch(true) + try { + const { page, context } = h + const pagesBefore = context.pages().length + await urlInput(page).fill(ROOT_URL) + await page.getByTestId('refresh-now').click() + await expect(page.getByTestId('refresh-preview')).toContainText('5 added', { + timeout: 40_000, + }) + await expect(page.getByText('(via tab)')).toBeVisible() + expect(h.apiCalls.fromTab).toBeGreaterThan(0) + // It reused the open Feishu tab and left it open. + expect(context.pages().length).toBe(pagesBefore) + await page.getByTestId('refresh-apply').click() + await expect(page.getByText('000 - Handbook')).toBeVisible() + } finally { + await h.cleanup() + } +}) diff --git a/apps/chrome-extension/src/pages/batch-download/batch-download.vue b/apps/chrome-extension/src/pages/batch-download/batch-download.vue index 090ffb4..c9337dc 100644 --- a/apps/chrome-extension/src/pages/batch-download/batch-download.vue +++ b/apps/chrome-extension/src/pages/batch-download/batch-download.vue @@ -13,6 +13,7 @@ import { import { useInitLocale } from '../shared/i18n' import { useInitTheme } from '../shared/theme' import FileTree from './components/FileTree.vue' +import WikiRefreshCard from './components/WikiRefreshCard.vue' import type { TreeNode, FolderNode, @@ -672,8 +673,28 @@ const packageZip = async () => { } } +// Optional: rebuild the tree from a Feishu wiki before a run +const wikiRefreshCard = ref | null>(null) +const preparingRun = ref(false) + +const applyRefreshedTree = (tree: FolderNode) => { + rootNode.value = tree + selectedNodeId.value = 'root' +} + // Actions from control panel const startBatchDownload = async () => { + if (preparingRun.value || exporting.value) return + // Clear first so a refresh's log lines stay visible for this run. + clearLogs() + preparingRun.value = true + try { + const proceed = (await wikiRefreshCard.value?.prepareRun()) ?? true + if (!proceed) return + } finally { + preparingRun.value = false + } + const files = collectFileNodes(rootNode.value) if (files.length === 0) { addLog( @@ -701,7 +722,6 @@ const startBatchDownload = async () => { exporting.value = true isPaused.value = false - clearLogs() addLog('Starting batch download process...', 'info') processNext() @@ -1055,7 +1075,7 @@ const handleImportFileChange = (event: Event) => {