Skip to content

Commit 1458a05

Browse files
committed
add getWebsiteTitle function to fetch and return the title of a webpage
1 parent e86009f commit 1458a05

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/utils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,21 @@ export function formatDateString(
102102
// day: 'numeric',
103103
year: 'numeric',
104104
});
105-
}
105+
}
106+
107+
108+
export async function getWebsiteTitle(url: string): Promise<string | null> {
109+
try {
110+
const response = await fetch(url);
111+
const html = await response.text();
112+
113+
const parser = new DOMParser();
114+
const doc = parser.parseFromString(html, 'text/html');
115+
116+
const title = doc.querySelector('title');
117+
return title?.innerText || null;
118+
} catch (error) {
119+
console.error('Failed to fetch website title:', error);
120+
return null;
121+
}
122+
}

0 commit comments

Comments
 (0)