-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutubeTranscript.js
More file actions
135 lines (118 loc) · 4.92 KB
/
youtubeTranscript.js
File metadata and controls
135 lines (118 loc) · 4.92 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// youtubeTranscript.js
// Function to extract metadata and then initiate transcript extraction
function extractYouTubeData(sendResponseCallback) {
// Extract metadata
const titleElement = document.querySelector('h1.ytd-watch-metadata');
const title = titleElement ? titleElement.innerText.trim() : document.title.replace(' - YouTube', '').trim();
const channelElement = document.querySelector('#channel-name #text a');
const channel = channelElement ? channelElement.innerText.trim() : 'N/A';
const publishedDateElement = document.querySelector('#info-strings yt-formatted-string');
const publishedDate = publishedDateElement ? publishedDateElement.innerText.trim() : 'N/A';
const url = window.location.href;
const metadata = { title, channel, publishedDate, url };
const transcriptButton = document.querySelector(
'button[aria-label="Show transcript"], button[aria-label="Open transcript"], button[aria-label*="ranscript" i]'
);
if (transcriptButton) {
transcriptButton.click();
setTimeout(() => waitForTranscript(metadata, sendResponseCallback), 1200);
} else {
console.error("Transcript button not found");
sendResponseCallback({ action: "transcriptError", error: "Transcript button not found" });
}
}
// Function to wait for and extract transcript text
function waitForTranscript(metadata, sendResponseCallback) {
const maxWaitTime = 15000; // Maximum wait time in milliseconds
const startTime = Date.now();
function getTranscriptFromItems(transcriptItems) {
return Array.from(transcriptItems)
.map(item => {
const timeElement = item.querySelector('.segment-timestamp, .ytwTranscriptSegmentViewModelTimestamp');
const textElement = item.querySelector('.segment-text, .yt-core-attributed-string[role="text"], span[role="text"]');
const time = timeElement ? timeElement.innerText.trim() : '';
const text = textElement ? textElement.innerText.trim() : '';
return `${time} ${text}`.trim();
})
.filter(line => line.length > 0)
.join('\n');
}
function getModernTranscriptItems() {
const modernPanel = document.querySelector(
'ytd-macro-markers-list-renderer[panel-target-id="PAmodern_transcript_view"], ytd-macro-markers-list-renderer[panel-content-visible]'
);
if (!modernPanel) {
return [];
}
return modernPanel.querySelectorAll(
'transcript-segment-view-model, .ytwTranscriptSegmentViewModelHost'
);
}
const checkTranscript = () => {
// Primary selector used by YouTube's transcript panel
let transcriptItems = document.querySelectorAll('ytd-transcript-segment-renderer');
// Fallback: segment text might be in different structure on some pages
if (transcriptItems.length === 0) {
transcriptItems = document.querySelectorAll('[class*="segment-renderer"]');
}
// New YouTube transcript layout
if (transcriptItems.length === 0) {
transcriptItems = getModernTranscriptItems();
}
if (transcriptItems.length > 0) {
const transcript = getTranscriptFromItems(transcriptItems);
if (transcript.trim().length > 0) {
sendResponseCallback({
action: "transcriptData",
transcript: transcript,
title: metadata.title,
channel: metadata.channel,
date: metadata.publishedDate,
url: metadata.url
});
return;
}
}
if (Date.now() - startTime < maxWaitTime) {
setTimeout(checkTranscript, 500);
} else {
console.error("Transcript loading timed out");
sendResponseCallback({ action: "transcriptError", error: "Transcript loading timed out" });
}
};
checkTranscript();
}
window.summariseExtensionYouTubeHandleMessage = (request, sender, sendResponse) => {
if (request.action === "extractTranscript") {
console.log("Received request to extract transcript for summary...");
extractYouTubeData(response => {
if (response.action === "transcriptData") {
chrome.runtime.sendMessage({
action: "transcriptExtracted",
text: response.transcript,
pageUrl: response.url,
pageTitle: response.title,
publishedDate: response.date
});
} else {
chrome.runtime.sendMessage(response); // Forward the error
}
});
return true;
}
if (request.action === "getTranscriptAndMetadata") {
console.log("Received request to get transcript and metadata for copying...");
extractYouTubeData(sendResponse);
return true;
}
return false;
};
if (!window.summariseExtensionYouTubeListenerInstalled) {
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
return window.summariseExtensionYouTubeHandleMessage(request, sender, sendResponse);
});
window.summariseExtensionYouTubeListenerInstalled = true;
console.log('Installed YouTube transcript message listener');
} else {
console.log('Reused existing YouTube transcript listener with updated handler');
}