As I work on a plugin for Obsidian that expands shortened urls like bit.ly or t.co to their full-length versions in Markdown, I encounter a problem. I need to fetch the page title in order to properly create a Markdown link [title](web link).
Unfortunately, I face a CORS issue that prevents me from retrieving the title. I have explored Cors Proxy solutions, but the free ones seem either unreliable, insecure, or meant for demonstration purposes only.
You can find my open-source code here: https://github.com/odebroqueville/obsidian-url-expander
The specific code responsible for fetching the web page title is:
// Helper function to get the title of a web page
export async function getTitle(url:string){
try {
const request = new Request(url, {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'text/html'
}
});
const response = await fetch(request);
const html = await response.text();
let title = '';
const titleMatches:string[] = html.match(/<title.*?>.*?<\/title>/gmi)||[];
if (titleMatches.length > 0) {
title = titleMatches[0];
console.log(title);
}
if (title.search(/<title/gi) !== -1){
const titleText = title.substring(title.indexOf('>')+1);
const res = titleText.replace('</title>','');
console.log(res);
return res;
}
return '';
} catch (err) {
console.error(`Failed to retrieve title with error: ${err}`);
return '';
}
}
I have experimented with various proxies, but either the solutions were outdated and unsafe, or they required payment for access.