This commit is contained in:
Kagami Sascha Rosylight
2023-03-11 20:51:46 +01:00
parent e02da09f9a
commit 1866d9929a
4 changed files with 16 additions and 35 deletions

View File

@ -10,7 +10,7 @@ import * as cheerio from 'cheerio';
* *
* Width should always be 100%. * Width should always be 100%.
*/ */
async function getOEmbedRich($, pageUrl) { async function getOEmbedPlayer($, pageUrl) {
const href = $('link[type="application/json+oembed"]').attr('href'); const href = $('link[type="application/json+oembed"]').attr('href');
if (!href) { if (!href) {
return null; return null;
@ -24,7 +24,7 @@ async function getOEmbedRich($, pageUrl) {
} }
catch { } catch { }
})(); })();
if (!body || body.version !== '1.0' || body.type !== 'rich') { if (!body || body.version !== '1.0' || !['rich', 'video'].includes(body.type)) {
// Not a well formed rich oEmbed // Not a well formed rich oEmbed
return null; return null;
} }
@ -42,14 +42,13 @@ async function getOEmbedRich($, pageUrl) {
// Should only have the body and html elements as the parents // Should only have the body and html elements as the parents
return null; return null;
} }
const src = iframe.attr('src'); const url = iframe.attr('src');
if (!src) { if (!url) {
// No src? // No src?
return null; return null;
} }
// XXX: Use global URL object instead of the deprecated `node:url` // XXX: Use global URL object instead of the deprecated `node:url`
const url = URL.parse(src); if (URL.parse(url).protocol !== 'https:') {
if (url.protocol !== 'https:') {
// Allow only HTTPS for best security // Allow only HTTPS for best security
return null; return null;
} }
@ -73,7 +72,8 @@ async function getOEmbedRich($, pageUrl) {
return null; return null;
} }
return { return {
src, url,
width: null,
height, height,
allow: allowedFeatures allow: allowedFeatures
}; };
@ -156,8 +156,7 @@ export default async (url, lang = null) => {
}; };
const [icon, oEmbed] = await Promise.all([ const [icon, oEmbed] = await Promise.all([
getIcon(), getIcon(),
// playerあるならoEmbedは必要ない getOEmbedPlayer($, url.href),
!playerUrl ? getOEmbedRich($, url.href) : null,
]); ]);
// Clean up the title // Clean up the title
title = cleanupTitle(title, siteName); title = cleanupTitle(title, siteName);
@ -169,13 +168,13 @@ export default async (url, lang = null) => {
icon: icon || null, icon: icon || null,
description: description || null, description: description || null,
thumbnail: image || null, thumbnail: image || null,
player: { player: oEmbed ?? {
url: playerUrl || null, url: playerUrl || null,
width: Number.isNaN(playerWidth) ? null : playerWidth, width: Number.isNaN(playerWidth) ? null : playerWidth,
height: Number.isNaN(playerHeight) ? null : playerHeight height: Number.isNaN(playerHeight) ? null : playerHeight,
allow: ['fullscreen', 'encrypted-media'],
}, },
sitename: siteName || null, sitename: siteName || null,
sensitive, sensitive,
oEmbed,
}; };
}; };

View File

@ -36,9 +36,9 @@ export async function summarize(url) {
player: { player: {
url: playerUrl || null, url: playerUrl || null,
width: playerWidth ? parseInt(playerWidth) : null, width: playerWidth ? parseInt(playerWidth) : null,
height: playerHeight ? parseInt(playerHeight) : null height: playerHeight ? parseInt(playerHeight) : null,
allow: playerUrl ? ['fullscreen', 'encrypted-media'] : [],
}, },
sitename: 'Amazon', sitename: 'Amazon',
oEmbed: null,
}; };
} }

View File

@ -29,9 +29,9 @@ export async function summarize(url) {
player: { player: {
url: null, url: null,
width: null, width: null,
height: null height: null,
allow: [],
}, },
sitename: 'Wikipedia', sitename: 'Wikipedia',
oEmbed: null,
}; };
} }

20
built/summary.d.ts vendored
View File

@ -27,10 +27,6 @@ declare type Summary = {
* Possibly sensitive * Possibly sensitive
*/ */
sensitive?: boolean; sensitive?: boolean;
/**
* The iframe information of oEmbed data from that web page
*/
oEmbed: OEmbedRichIframe | null;
}; };
export default Summary; export default Summary;
export declare type Player = { export declare type Player = {
@ -46,22 +42,8 @@ export declare type Player = {
* The height of the player * The height of the player
*/ */
height: number | null; height: number | null;
};
/** /**
* Extracted iframe information from OEmbed html field. * The allowed permissions of the iframe
* `width` is omitted here as it should always be 100%.
*/
export declare type OEmbedRichIframe = {
/**
* The src of the iframe
*/
src: string;
/**
* The height of the iframe
*/
height: number;
/**
* The allowed feature list of the iframe
*/ */
allow: string[]; allow: string[];
}; };