-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
247 lines (186 loc) · 7.58 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
let requestQueue = [];
chrome.runtime.onInstalled.addListener(() => {
console.log('YouTube Citation Extension installed!');
});
async function fetchTranscript(videoId) {
const url = `http://localhost:3000/transcript/${videoId}`;
try {
const response = await fetch(url);
const data = await response.json();
// Convert the data into an array of text
const transcriptText = data.map(item => item.text);
// Store the transcript text in chrome.storage.local
chrome.storage.local.set({transcript: transcriptText}, function() {
console.log(`Transcript set to ${transcriptText}`);
});
} catch (err) {
console.error('Error fetching transcript:', err);
}
}
async function analyzeTranscript(segment) {
const url = `http://localhost:3000/analyze-transcript`;
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ transcript: segment }),
});
const data = await response.json();
console.log(data); // The analyzed transcript
// Fetch articles from PubMed using the analyzed transcript
for (let sentence of data) {
fetchPubMed(sentence);
}
} catch (err) {
console.error('Error analyzing transcript:', err);
}
}
// Create a queue to store the promises
let requestCount = 0;
let firstRequestTimestamp = null;
async function processQueue() {
if (requestQueue.length === 0) {
return;
}
const { resolve, reject, func } = requestQueue.shift();
if (requestCount === 0) {
firstRequestTimestamp = Date.now();
}
try {
const result = await func();
requestCount++;
resolve(result);
} catch (err) {
reject(err);
}
if (Date.now() - firstRequestTimestamp >= 1000) { // Change this to 1000 for 1 second
requestCount = 0;
firstRequestTimestamp = Date.now(); // Reset the timestamp for the next second
}
// Check if there are more requests to process and if we haven't already made 10 requests in the last second
if (requestQueue.length > 0 && requestCount < 10) {
setTimeout(processQueue, 1000 - (Date.now() - firstRequestTimestamp)); // Process the next request after an appropriate delay
} else if (requestCount >= 10) {
// If we have already made 10 requests in the last second, wait until a second has passed since the first request before processing the next request
setTimeout(processQueue, firstRequestTimestamp + 1000 - Date.now());
}
}
async function fetchPubMed(keywords) {
console.log(`Keywords: ${keywords}`);
const keywordsToUse = keywords.length > 5 ? keywords.slice(0, 5) : keywords;
console.log(`Fetching PubMed articles for query: ${keywordsToUse.join(' AND ')} AND ${keywordsToUse.join(' OR ')}`);
const andQuery = keywordsToUse.join(' AND ');
const orQuery = keywordsToUse.join(' OR ');
// Fetch 'AND' articles from server
let andData = await fetch(`http://localhost:3000/search-pubmed?query=${encodeURIComponent(andQuery)}`)
.then(res => res.json());
console.log(andData);
// Fetch 'OR' articles from server
let orData = await fetch(`http://localhost:3000/search-pubmed?query=${encodeURIComponent(orQuery)}`)
.then(res => res.json());
console.log(orData);
if (!Array.isArray(andData) || !Array.isArray(orData)) {
console.error('andData or orData is not an array');
return;
}
const combinedData = [...andData, ...orData];
const formattedCitations = combinedData.map((article) => {
const authors = article.authors.map((author) => `${author.name}, `).join(', ');
const year = new Date(article.pubdate).getFullYear();
const title = article.title;
const journal = article.source;
const volume = article.volume;
const issue = article.issue;
const pages = article.pages;
return `${authors} (${year}). ${title}. ${journal}, ${volume}(${issue}), ${pages}.`;
});
chrome.runtime.sendMessage({type: 'pubmedResults', data: formattedCitations});
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log(request); // Log the request
if (request.type === 'videoLoaded' && request.videoId !== null) {
// Store the video ID in chrome.storage.local
chrome.storage.local.set({videoId: request.videoId}, function() {
console.log(`Video ID set to ${request.videoId}`);
});
fetchTranscript(request.videoId);
} else if (request.action === 'setVideoId' && request.videoId !== null) {
// Store the video ID in chrome.storage.local
chrome.storage.local.set({videoId: request.videoId}, function() {
console.log(`Video ID set to ${request.videoId}`);
});
} else if (request.action === 'getCitations') {
// Get the video ID from chrome.storage.local
chrome.storage.local.get(['videoId'], function(result) {
console.log(`Getting citations for video ${result.videoId}`);
fetchTranscript(result.videoId);
});
} else if (request.action === 'getUserQuery') {
// Store the user query and analyze it
chrome.storage.local.set({userQuery: request.query}, function() {
console.log(`User query set to ${request.query}`);
analyzeQuery(request.query);
});
}
});
async function analyzeQuery(query) {
const url = `http://localhost:3000/analyze-query`;
chrome.storage.local.get(['transcript'], async function(result) {
console.log(result); // Log the result object
if (result.transcript) {
const transcript = result.transcript.join(' '); // Convert the transcript array into a string
console.log(query); // Log the query
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ transcript: transcript, user_query: query }),
});
const data = await response.json();
console.log(data); // The analyzed query
console.log('Data to extract keywords:', data);
const keywordsResponse = await fetch(`http://localhost:3000/extract-keywords`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ user_query: data }), // Send "user_query" instead of "text"
});
const keywordsData = await keywordsResponse.json();
console.log('Keywords Data:', keywordsData); // Log the keywordsData
// Fetch articles from PubMed using the extracted keywords
fetchPubMed(keywordsData);
} catch (err) {
console.error('Error analyzing query:', err);
}
} else {
console.log('Transcript is undefined');
}
});
}
async function findRelevantTranscriptPart(userQuery) {
// Get the transcript from chrome.storage.local
chrome.storage.local.get(['transcript'], function(result) {
console.log(`Transcript set to ${result.transcript}`);
// Extract keywords from the user query
const queryKeywords = nlp(userQuery).nouns().out('array');
// Find the index of the sentence in the transcript that includes any of the query keywords
const index = result.transcript.findIndex(sentence => {
const sentenceKeywords = nlp(sentence).nouns().out('array');
return sentenceKeywords.some(keyword => queryKeywords.includes(keyword));
});
// Ensure the user query was found in the transcript
if (index === -1) {
console.error('User query not found in transcript');
return;
}
// Get the relevant part of the transcript
const relevantPart = result.transcript[index];
// Analyze the relevant part of the transcript
analyzeTranscript(relevantPart);
});
}