(url: string)
| 6 | export const isOnline = () => navigator.onLine === true; |
| 7 | |
| 8 | export async function getPageTitle(url: string) { |
| 9 | // No need to request the title when it's not url. |
| 10 | if (!url.startsWith('http')) |
| 11 | return ''; |
| 12 | |
| 13 | // No need to request the title when off line. |
| 14 | if (!isOnline()) |
| 15 | return ''; |
| 16 | |
| 17 | try { |
| 18 | const res = await fetch(url, { method: 'GET', mode: 'cors' }); |
| 19 | const contentType = res.headers.get('content-type'); |
| 20 | |
| 21 | if (res.status !== 200 || !contentType || !/text\/html/i.test(contentType)) |
| 22 | return ''; |
| 23 | |
| 24 | // The response is HTML — read it as text and pluck `<title>`. |
| 25 | // Pre-fix this called `res.json()`, which always threw and made |
| 26 | // the helper silently return '' (marktext 141d25d8 / #1344). |
| 27 | const body = await res.text(); |
| 28 | const match = body.match(/<title>([\s\S]*?)<\/title>/i); |
| 29 | |
| 30 | return match && match[1] ? match[1].trim() : ''; |
| 31 | } |
| 32 | catch { |
| 33 | return ''; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | export async function normalizePastedHTML(html: string) { |
| 38 | // Only extract the `body.innerHTML` when the `html` is a full HTML Document. |
no test coverage detected