')[0] ||
html.split('window["ytInitialData"] = ')[1]?.split(';')[0];
if (!dataRaw) throw new Error("YouTube updated their security. Please try again in a few minutes.");
const ytData = JSON.parse(dataRaw);
// Navigate the complex JSON tree to find video items
const sectionList = ytData.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents;
const videoItems = sectionList.find(c => c.itemSectionRenderer)?.itemSectionRenderer.contents || [];
resultsDiv.innerHTML = "";
if (videoItems.length === 0) {
resultsDiv.innerHTML = "No videos found.";
return;
}
videoItems.forEach(item => {
const v = item.videoRenderer;
if (!v) return;
const videoId = v.videoId;
const title = v.title.runs[0].text;
const thumb = v.thumbnail.thumbnails[0].url;
const channel = v.ownerText.runs[0].text;
const card = document.createElement('div');
card.className = 'card';
card.onclick = () => window.open(`https://www.youtube.com{videoId}`, '_blank');
card.innerHTML = `
`;
resultsDiv.appendChild(card);
});
} catch (err) {
console.error(err);
resultsDiv.innerHTML = `Error: ${err.message}
Try a different search term or check your internet connection.
`;
}
}