-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
58 lines (49 loc) · 2.21 KB
/
background.js
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
const HUGGING_FACE_API_URL = 'https://api-inference.huggingface.co/models/facebook/bart-large-cnn';
const HUGGING_FACE_API_KEY = ''; // Replace with your HuggingFace API key
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'summarize') {
// Always treat the summarizer as enabled for now
summarizeVideo(request.videoInfo, 'medium')
.then(summary => sendResponse({ summary }))
.catch(error => sendResponse({ error: error.message }));
return true; // Indicates async response
}
});
async function summarizeVideo(videoInfo, length) {
const maxLength = length === 'short' ? 50 : length === 'long' ? 200 : 100;
const prompt = `Summarize the following YouTube video in about ${maxLength} characters:Title: ${videoInfo.title} Description: ${videoInfo.description} Captions: ${videoInfo.captions ? videoInfo.captions.substring(0, 1000) : 'Not available'}`;
const response = await fetch(HUGGING_FACE_API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${HUGGING_FACE_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ inputs: prompt })
});
if (!response.ok) {
throw new Error('Failed to generate summary');
}
const result = await response.json();
let summary = result[0].summary_text;
// Remove the prompt and title from the summary
const promptIndex = summary.indexOf('Summarize the following YouTube video');
if (promptIndex !== -1) {
summary = summary.substring(0, promptIndex);
}
// Remove the title if it's at the beginning of the summary
const titleIndex = summary.indexOf(videoInfo.title);
if (titleIndex === 0) {
summary = summary.substring(videoInfo.title.length).trim();
}
return summary.trim();
}
// Listen for messages from content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'summarize') {
// Call AI summarization service (placeholder)
summarizeVideo(request.videoInfo)
.then(summary => sendResponse({ summary }))
.catch(error => sendResponse({ error: error.message }));
return true; // Indicates async response
}
});