diff options
| author | Marie <github@yuugi.dev> | 2025-05-08 16:09:36 +0000 |
|---|---|---|
| committer | Marie <github@yuugi.dev> | 2025-05-08 16:09:36 +0000 |
| commit | c5f5c6fef07b98ef2aa89e19c7cd70bfecc695e6 (patch) | |
| tree | 06da4f5e20e9b2fca54e605f3db2fc7fc2526cfd /packages/backend/src/misc | |
| parent | merge: Improve inline quote detection and link previews (resolves #1047 and #... (diff) | |
| parent | separate type imports for Cheerio (diff) | |
| download | sharkey-c5f5c6fef07b98ef2aa89e19c7cd70bfecc695e6.tar.gz sharkey-c5f5c6fef07b98ef2aa89e19c7cd70bfecc695e6.tar.bz2 sharkey-c5f5c6fef07b98ef2aa89e19c7cd70bfecc695e6.zip | |
merge: Replace JSDOM with cheerio (!973)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/973
Approved-by: dakkar <dakkar@thenautilus.net>
Approved-by: Marie <github@yuugi.dev>
Diffstat (limited to 'packages/backend/src/misc')
| -rw-r--r-- | packages/backend/src/misc/verify-field-link.ts | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/packages/backend/src/misc/verify-field-link.ts b/packages/backend/src/misc/verify-field-link.ts index f519acfba0..62542eaaa0 100644 --- a/packages/backend/src/misc/verify-field-link.ts +++ b/packages/backend/src/misc/verify-field-link.ts @@ -3,32 +3,29 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { JSDOM } from 'jsdom'; +import { load as cheerio } from 'cheerio'; import type { HttpRequestService } from '@/core/HttpRequestService.js'; type Field = { name: string, value: string }; export async function verifyFieldLinks(fields: Field[], profile_url: string, httpRequestService: HttpRequestService): Promise<string[]> { const verified_links = []; - for (const field_url of fields - .filter(x => URL.canParse(x.value) && ['http:', 'https:'].includes((new URL(x.value).protocol)))) { + for (const field_url of fields.filter(x => URL.canParse(x.value) && ['http:', 'https:'].includes((new URL(x.value).protocol)))) { try { const html = await httpRequestService.getHtml(field_url.value); - const { window } = new JSDOM(html); - const doc: Document = window.document; + const doc = cheerio(html); - const aEls = Array.from(doc.getElementsByTagName('a')); - const linkEls = Array.from(doc.getElementsByTagName('link')); + const links = doc('a[rel~="me"][href], link[rel~="me"][href]').toArray(); - const includesProfileLinks = [...aEls, ...linkEls].some(link => link.rel === 'me' && link.href === profile_url); - if (includesProfileLinks) { verified_links.push(field_url.value); } - - window.close(); - } catch (err) { + const includesProfileLinks = links.some(link => link.attribs.href === profile_url); + if (includesProfileLinks) { + verified_links.push(field_url.value); + } + } catch { // don't do anything. - continue; } } + return verified_links; } |