in scripts/deno/iframe-titles.ts [29:84]
body: string(),
}),
}),
),
}),
});
const {
response: { results },
} = await fetch(url.toString())
.then((r) => r.json())
.then(schema.parse);
const parser = new DOMParser();
const isElement = (n: Node): n is Element => n instanceof Element;
type Status = 'ok' | 'warning' | 'missing';
type Info = {
status: Status;
attr: string;
value: string;
};
type Ok = Info & { status: 'ok' };
type Warning = Info & { status: 'warning' };
type Missing = Info & { status: 'missing' };
type Article = {
url: string;
title: string;
iframes: Array<Ok | Warning | Missing>;
};
const getStatus = (iframes: Article['iframes']): Status => {
if (iframes.length === 0) return 'missing';
if (iframes.some(({ status }) => status === 'missing')) return 'missing';
if (iframes.some(({ status }) => status === 'warning')) return 'warning';
return 'ok';
};
const promises = results.map(async ({ id, webTitle: title }) => {
const url = `https://www.theguardian.com/${id}`;
const body = await fetch(url).then((r) => r.text());
const document = parser.parseFromString(body, 'text/html');
const attrs = ['title', 'srcDoc', 'src'] as const;
const iframes: Article['iframes'] = [
...(document?.querySelectorAll('iframe') ?? []),
]
.filter(isElement)
.map((iframe) => {
const attr = attrs.find((attr) => iframe.getAttribute(attr));
if (!attr) throw new Error('could not get any attribute');
const value = iframe.getAttribute(attr) ?? '';
switch (attr) {
case 'title': {