-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtestName.js
More file actions
30 lines (22 loc) · 1.04 KB
/
testName.js
File metadata and controls
30 lines (22 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const res = await fetch("https://www.youtube.com/watch?v=NjpmTL-ZM8E");
const html = await res.text();
function getYouTubeVideoTitle(htmlString) {
const metaTitleStartTag = '<meta name="title" content="';
const metaTitleEndTag = '">';
const startIndex = htmlString.indexOf(metaTitleStartTag);
// If the meta title tag is not found, return null or an empty string
if (startIndex === -1) {
return null;
}
// Calculate the starting index of the actual title content
const contentStartIndex = startIndex + metaTitleStartTag.length;
// Find the end index of the title content (the closing double quote)
const endIndex = htmlString.indexOf(metaTitleEndTag, contentStartIndex);
// If the closing quote is not found (which shouldn't happen with valid HTML), return null
if (endIndex === -1) {
return null;
}
// Extract the substring between the start of the content and the closing quote
return htmlString.substring(contentStartIndex, endIndex);
}
console.log(getYouTubeVideoTitle(html));