* refactor: use patchwright * refactor: migrate setRequestInterception and waitUntil * fix: overwrite ua * refactor: drop puppeteer cookie/requestfinished shims Migrate page.cookies/setCookie and browser.cookies/setCookie call sites to the native context APIs (page.context().cookies/addCookies), rewrite requestfinished handlers to await request.response() instead of consuming the synchronous puppeteer-shaped payload, and remove the now-redundant browser.close context-cleanup wrapper. Playwright already handles all of these natively; the shims existed only for the puppeteer -> playwright migration. * refactor: remove page.authenticate, page.setUserAgent, browser.newPage(), browser.userAgent() shims update cookie utility tests * refactor: replace browser instance with context in Playwright usage across multiple routes - Updated all instances of `browser` to `context` in Playwright-related functions for consistency and improved resource management. - Ensured proper closure of context after operations to prevent memory leaks. - Adjusted request interception methods to utilize the new context variable. * fix: direct type import * refactor: remove Puppeteer compatibility layers and unify page handling with Playwright * fix: test case * chore: bump patchright to v1.60.0 * chore: pin patchright to 1.59.1 nft can't resolve browser.json due to https://github.com/microsoft/playwright/pull/40093 * chore: pin to node 24.15 for playwright browser install on node 24.16 stucks due to https://github.com/microsoft/playwright/issues/40724 * fix: update patchright and patchright-core to version 1.60.2 and adjust minify-docker.js for compatibility * fix: import BrowserContext from patchright
89 lines
2.9 KiB
TypeScript
89 lines
2.9 KiB
TypeScript
import type { Route } from '@/types';
|
||
import cache from '@/utils/cache';
|
||
import { parseDate } from '@/utils/parse-date';
|
||
import playwright from '@/utils/playwright';
|
||
|
||
import utils from './utils';
|
||
|
||
export const route: Route = {
|
||
path: '/:section/:type?',
|
||
categories: ['bbs'],
|
||
example: '/dcard/funny/popular',
|
||
parameters: { section: '板塊名稱,URL 中獲得', type: '排序,popular 熱門;latest 最新,默認為 latest' },
|
||
features: {
|
||
requireConfig: false,
|
||
requirePuppeteer: true,
|
||
antiCrawler: true,
|
||
supportBT: false,
|
||
supportPodcast: false,
|
||
supportScihub: false,
|
||
},
|
||
name: '板塊帖子',
|
||
maintainers: ['HenryQW'],
|
||
handler,
|
||
};
|
||
|
||
async function handler(ctx) {
|
||
const { type = 'latest', section = 'posts' } = ctx.req.param();
|
||
const limit = ctx.req.query('limit') ? Number(ctx.req.query('limit')) : 30;
|
||
const context = await playwright();
|
||
|
||
let link = 'https://www.dcard.tw/f';
|
||
let api = 'https://www.dcard.tw/service/api/v2';
|
||
let title = 'Dcard - ';
|
||
|
||
if (section !== 'posts' && section !== 'popular' && section !== 'latest') {
|
||
link += `/${section}`;
|
||
api += `/forums/${section}`;
|
||
title += `${section} - `;
|
||
}
|
||
api += '/posts';
|
||
if (type === 'popular') {
|
||
link += '?latest=false';
|
||
api += '?popular=true';
|
||
title += '熱門';
|
||
} else {
|
||
link += '?latest=true';
|
||
api += '?popular=false';
|
||
title += '最新';
|
||
}
|
||
|
||
const page = await context.newPage();
|
||
await page.route('**/*', (route) => {
|
||
const request = route.request();
|
||
request.resourceType() === 'document' || request.resourceType() === 'script' ? route.continue() : route.abort();
|
||
});
|
||
await page.setExtraHTTPHeaders({
|
||
referer: `https://www.dcard.tw/f/${section}`,
|
||
});
|
||
|
||
await page.goto(`${api}&limit=100`);
|
||
await page.waitForSelector('body > pre');
|
||
const response = await page.evaluate(() => document.querySelector('body > pre').textContent);
|
||
const cookies = await cache.tryGet('dcard:cookies', () => page.context().cookies(), 3600, false);
|
||
await page.close();
|
||
|
||
const data = JSON.parse(response);
|
||
const items = data.map((item) => ({
|
||
title: `「${item.forumName}」${item.title}`,
|
||
link: `https://www.dcard.tw/f/${item.forumAlias}/p/${item.id}`,
|
||
description: item.excerpt,
|
||
author: `${item.school || '匿名'}.${item.gender === 'M' ? '男' : '女'}`,
|
||
pubDate: parseDate(item.createdAt),
|
||
category: [item.forumName, ...item.topics],
|
||
forumAlias: item.forumAlias,
|
||
id: item.id,
|
||
}));
|
||
|
||
// parse fulltext for first `limit` items
|
||
const result = await utils.ProcessFeed(items, cookies, context, limit, cache);
|
||
await context.close();
|
||
|
||
return {
|
||
title,
|
||
link,
|
||
description: '不想錯過任何有趣的話題嗎?趕快加入我們吧!',
|
||
item: result,
|
||
};
|
||
}
|