feat: replace electron to puppeteer (#2700)

Co-authored-by: himself65 <himself65@outlook.com>
(cherry picked from commit fda89b05e7)
This commit is contained in:
Qi
2023-06-08 17:51:45 +08:00
committed by himself65
parent 0cf3bc42e8
commit 336ff5220f
15 changed files with 134 additions and 982 deletions

View File

@@ -1,11 +1,56 @@
import { getMetaData } from './server/get-meta-data';
import { getLinkPreview } from 'link-preview-js';
type MetaData = {
title?: string;
description?: string;
icon?: string;
image?: string;
[x: string]: string | string[] | undefined;
};
export interface PreviewType {
url: string;
title: string;
siteName: string | undefined;
description: string | undefined;
mediaType: string;
contentType: string | undefined;
images: string[];
videos: {
url: string | undefined;
secureUrl: string | null | undefined;
type: string | null | undefined;
width: string | undefined;
height: string | undefined;
}[];
favicons: string[];
}
export default {
getBookmarkDataByLink: async (_: unknown, url: string) => {
return getMetaData(url, {
shouldReGetHTML: metaData => {
return !metaData.title && !metaData.description;
getBookmarkDataByLink: async (_: unknown, url: string): Promise<MetaData> => {
const previewData = (await getLinkPreview(url, {
timeout: 6000,
headers: {
'user-agent': 'googlebot',
},
});
followRedirects: 'follow',
}).catch(() => {
return {
title: '',
siteName: '',
description: '',
images: [],
videos: [],
contentType: `text/html`,
favicons: [],
};
})) as PreviewType;
return {
title: previewData.title,
description: previewData.description,
icon: previewData.favicons[0],
image: previewData.images[0],
};
},
};