-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyt-comments-script.js
More file actions
85 lines (74 loc) · 3.15 KB
/
yt-comments-script.js
File metadata and controls
85 lines (74 loc) · 3.15 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
document.addEventListener('DOMContentLoaded', function() {
const wrapper = document.getElementById('yt-comments-wrapper');
if (!wrapper) {
return; // Exit if our container isn't on the page
}
const videoId = wrapper.dataset.videoId;
const apiKey = wrapper.dataset.apiKey;
const commentsContainer = document.getElementById('youtube-comments-container');
if (videoId && apiKey && commentsContainer) {
fetchComments(apiKey, videoId, commentsContainer);
}
});
async function fetchComments(apiKey, videoId, container) {
const apiUrl = `https://www.googleapis.com/youtube/v3/commentThreads?part=snippet,replies&videoId=${videoId}&key=${apiKey}&maxResults=50&textFormat=plainText`;
try {
const response = await fetch(apiUrl);
if (!response.ok) {
const errorData = await response.json();
const errorMessage = errorData.error?.message || `HTTP error! Status: ${response.status}`;
throw new Error(errorMessage);
}
const data = await response.json();
renderComments(data.items, container);
} catch (error) {
console.error('Error fetching comments:', error);
showError(`Failed to load comments. Check API key and shortcode. Error: ${error.message}`, container);
}
}
function renderComments(commentThreads, container) {
if (!commentThreads || commentThreads.length === 0) {
container.innerHTML = '<p class="yt-no-comments">No comments found for this video.</p>';
return;
}
let commentsHtml = '<div class="yt-comments-list">';
commentThreads.forEach(thread => {
const topLevelComment = thread.snippet.topLevelComment.snippet;
commentsHtml += '<div class="yt-comment-thread">';
commentsHtml += createCommentHtml(topLevelComment);
if (thread.replies) {
commentsHtml += '<div class="yt-comment-replies">';
thread.replies.comments.forEach(reply => {
commentsHtml += createCommentHtml(reply.snippet);
});
commentsHtml += '</div>';
}
commentsHtml += '</div>';
});
commentsHtml += '</div>';
container.innerHTML = commentsHtml;
}
function createCommentHtml(snippet) {
const publishedDate = new Date(snippet.publishedAt).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
// Simple sanitization
const safeText = snippet.textDisplay.replace(/</g, "<").replace(/>/g, ">");
return `
<div class="yt-comment">
<img src="${snippet.authorProfileImageUrl}" alt="" class="yt-author-avatar">
<div class="yt-comment-content">
<div class="yt-comment-header">
<a href="${snippet.authorChannelUrl}" target="_blank" class="yt-author-name">${snippet.authorDisplayName}</a>
<span class="yt-comment-date">${publishedDate}</span>
</div>
<p class="yt-comment-text">${safeText}</p>
</div>
</div>
`;
}
function showError(message, container) {
container.innerHTML = `<div class="yt-error-message"><p>${message}</p></div>`;
}