-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
317 lines (276 loc) · 12.4 KB
/
script.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// DOM elements
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');
const resultsList = document.getElementById('results-list');
const paperSummary = document.getElementById('paper-summary');
const sortBy = document.getElementById('sort-by');
const dateFrom = document.getElementById('date-from');
const dateTo = document.getElementById('date-to');
const sourceCheckboxes = document.querySelectorAll('input[name="source"]');
const citationStyle = document.getElementById('citation-style');
const citationOutput = document.getElementById('citation-output');
const copyCitationButton = document.getElementById('copy-citation');
const previewButton = document.getElementById('preview-paper');
const previewSection = document.getElementById('preview-section');
const paperPreview = document.getElementById('paper-preview');
const fullPaperViewButton = document.getElementById('full-paper-view');
// Event listeners
searchButton.addEventListener('click', searchPapers);
searchInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
searchPapers();
}
});
sortBy.addEventListener('change', sortPapers);
citationStyle.addEventListener('change', () => {
if (currentPaper) {
updateCitation(currentPaper);
}
});
copyCitationButton.addEventListener('click', copyCitation);
previewButton.addEventListener('click', previewPaper);
fullPaperViewButton.addEventListener('click', openFullPaper);
sourceCheckboxes.forEach(checkbox => {
checkbox.addEventListener('change', searchPapers);
});
let currentPapers = [];
let currentPaper = null;
async function searchPapers() {
const keyword = searchInput.value;
const fromDate = dateFrom.value ? new Date(dateFrom.value) : null;
const toDate = dateTo.value ? new Date(dateTo.value) : null;
const selectedSources = Array.from(sourceCheckboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
currentPapers = [];
try {
for (const source of selectedSources) {
switch (source) {
case 'arxiv':
const arxivPapers = await searchArxiv(keyword, fromDate, toDate);
currentPapers = [...currentPapers, ...arxivPapers];
break;
case 'hal':
const halPapers = await searchHAL(keyword, fromDate, toDate);
currentPapers = [...currentPapers, ...halPapers];
break;
case 'paperswithcode':
const paperswithcodePapers = await searchPapersWithCode(keyword, fromDate, toDate);
currentPapers = [...currentPapers, ...paperswithcodePapers];
break;
}
}
sortPapers();
} catch (error) {
console.error('Error fetching papers:', error);
resultsList.innerHTML = '<li>Error fetching papers. Please try again.</li>';
}
}
async function searchArxiv(keyword, fromDate, toDate) {
const response = await fetch(`https://export.arxiv.org/api/query?search_query=all:${keyword}&start=0&max_results=100`);
const xmlText = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, "text/xml");
const entries = xmlDoc.getElementsByTagName('entry');
return Array.from(entries).map(entry => ({
source: 'arXiv',
title: entry.getElementsByTagName('title')[0].textContent,
authors: Array.from(entry.getElementsByTagName('author')).map(author => author.getElementsByTagName('name')[0].textContent),
published: new Date(entry.getElementsByTagName('published')[0].textContent),
summary: entry.getElementsByTagName('summary')[0].textContent,
pdfUrl: entry.querySelector('link[title="pdf"]').getAttribute('href'),
citations: Math.floor(Math.random() * 1000) // Simulated citation count
})).filter(paper => {
if (fromDate && paper.published < fromDate) return false;
if (toDate && paper.published > toDate) return false;
return true;
});
}
async function searchHAL(keyword, fromDate, toDate) {
const response = await fetch(`https://api.archives-ouvertes.fr/search/?q=${keyword}&wt=json&fl=title_s,authFullName_s,publicationDate_s,abstract_s,fileMain_s,citationFull_s&fq=submittedDate_s:[${fromDate ? fromDate.toISOString() : '*'} TO ${toDate ? toDate.toISOString() : '*'}]`);
const data = await response.json();
return data.response.docs.map(doc => ({
source: 'HAL',
title: Array.isArray(doc.title_s) ? doc.title_s.join('') : doc.title_s,
authors: doc.authFullName_s,
published: new Date(doc.publicationDate_s),
summary: doc.abstract_s,
pdfUrl: doc.fileMain_s,
citations: 0 // Simulated citation count
}));
}
async function searchPapersWithCode(keyword, fromDate, toDate) {
const corsProxy = 'https://paperspot.rcg4ueducation.workers.dev/?target=';
const apiUrl = `https://paperswithcode.com/api/v1/search/?q=${keyword}&items_per_page=10`;
const response = await fetch(corsProxy + apiUrl, {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
});
const data = await response.json();
return data.results.map(result => ({
source: 'Papers with Code',
title: result.paper.title,
authors: result.paper.authors,
published: new Date(result.paper.published),
summary: result.paper.abstract,
pdfUrl: result.paper.url_pdf,
citations: result.repository ? result.repository.stars : 0, // Use repository stars as citations
repoUrl: result.repository ? result.repository.url : null,
repoName: result.repository ? result.repository.name : null,
repoDescription: result.repository ? result.repository.description : null,
stars: result.repository ? result.repository.stars : 0
})).filter(paper => {
if (fromDate && paper.published < fromDate) return false;
if (toDate && paper.published > toDate) return false;
return true;
});
}
function sortPapers() {
switch (sortBy.value) {
case 'date':
currentPapers.sort((a, b) => b.published - a.published);
break;
case 'relevance':
currentPapers.sort((a, b) => b.citations - a.citations);
break;
case 'alphabetical':
currentPapers.sort((a, b) => a.title.localeCompare(b.title));
break;
default:
currentPapers.sort((a, b) => b.citations - a.citations);
break;
}
displayPapers();
}
function displayPapers() {
resultsList.innerHTML = '';
currentPapers.forEach((paper, index) => {
const li = document.createElement('li');
li.textContent = `[${paper.source}] ${paper.title}`;
li.addEventListener('click', () => showPaperDetails(index));
resultsList.appendChild(li);
});
}
function showPaperDetails(index) {
currentPaper = currentPapers[index];
let detailsHTML = `
<h3>${currentPaper.title}</h3>
<p><strong>Source:</strong> ${currentPaper.source}</p>
<p><strong>Authors:</strong> ${currentPaper.authors.join(', ')}</p>
<p><strong>Published:</strong> ${currentPaper.published.toLocaleDateString()}</p>
<p><strong>Citations/Stars:</strong> ${currentPaper.citations}</p>
<h4>Abstract</h4>
<p>${currentPaper.summary}</p>
`;
paperSummary.innerHTML = detailsHTML;
const repoInfo = document.getElementById('repository-info');
if (currentPaper.source === 'Papers with Code' && currentPaper.repoUrl) {
document.getElementById('repo-url').innerHTML = `<strong>Repository:</strong> <a href="${currentPaper.repoUrl}" target="_blank">${currentPaper.repoName}</a>`;
document.getElementById('repo-stars').innerHTML = `<strong>⭐</strong> ${currentPaper.stars}`;
document.getElementById('repo-description').textContent = currentPaper.repoDescription;
repoInfo.style.display = 'block';
} else {
repoInfo.style.display = 'none';
}
updateCitation(currentPaper);
previewSection.style.display = 'none';
}
function updateCitation(paper) {
let citation = '';
const year = paper.published.getFullYear();
const url = paper.pdfUrl;
switch (citationStyle.value) {
case 'apa':
citation = `${paper.authors.join(', ')}. (${year}). ${paper.title}. ${paper.source}. ${url}`;
break;
case 'mla':
citation = `${paper.authors.join(', ')}. "${paper.title}." ${paper.source}, ${paper.published.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}, ${url}`;
break;
case 'chicago':
citation = `${paper.authors.join(', ')}. "${paper.title}." ${paper.source} (${year}). ${url}`;
break;
case 'harvard':
citation = `${paper.authors.join(', ')}, ${year}. ${paper.title}. ${paper.source}. Available at: ${url} [Accessed ${new Date().toLocaleDateString()}]`;
break;
case 'ieee':
citation = `${paper.authors.join(', ')}, "${paper.title}," ${paper.source}, ${year}. [Online]. Available: ${url}`;
break;
case 'vancouver':
citation = `${paper.authors.join(', ')}. ${paper.title}. ${paper.source} [Internet]. ${year} [cited ${new Date().toLocaleDateString()}]. Available from: ${url}`;
break;
case 'acm':
citation = `${paper.authors.join(', ')}. ${year}. ${paper.title}. ${paper.source}. DOI: ${url}`;
break;
case 'asa':
citation = `${paper.authors.join(', ')}. ${year}. "${paper.title}." ${paper.source}. Retrieved ${new Date().toLocaleDateString()} (${url})`;
break;
case 'cse':
citation = `${paper.authors.join(', ')}. ${year}. ${paper.title}. ${paper.source} [Internet]. Available from: ${url}`;
break;
case 'turabian':
citation = `${paper.authors.join(', ')}. "${paper.title}." ${paper.source}, ${year}. ${url}`;
break;
default:
citation = 'Unknown citation style';
}
citationOutput.textContent = citation;
}
function copyCitation() {
const citationText = citationOutput.textContent;
navigator.clipboard.writeText(citationText).then(() => {
alert('Citation copied to clipboard!');
}).catch(err => {
console.error('Failed to copy citation: ', err);
});
}
const modal = document.getElementById("modal");
const closeButton = document.getElementsByClassName("close-button")[0];
async function previewPaper() {
if (currentPaper) {
modal.style.display = 'block';
const paperPreview = document.getElementById('paperPreview');
paperPreview.innerHTML = 'Loading preview...';
try {
const corsProxy = 'https://paperspot.rcg4ueducation.workers.dev/?target=';
const pdfUrl = currentPaper.pdfUrl;
const loadingTask = pdfjsLib.getDocument(corsProxy + pdfUrl);
const pdf = await loadingTask.promise;
const scale = 2;
const numPagesToRender = 5;
paperPreview.innerHTML = '';
for (let pageNum = 1; pageNum <= numPagesToRender; pageNum++) {
const page = await pdf.getPage(pageNum);
const viewport = page.getViewport({ scale });
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: context,
viewport: viewport
};
await page.render(renderContext).promise;
paperPreview.appendChild(canvas);
}
} catch (error) {
console.error('Error loading PDF:', error);
paperPreview.innerHTML = 'Error loading PDF preview. Please try opening the full paper.';
}
}
}
// Evento para cerrar el modal
closeButton.onclick = function() {
modal.style.display = "none";
};
// Cerrar el modal si se hace clic fuera del contenido
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
function openFullPaper() {
if (currentPaper) {
window.open(currentPaper.pdfUrl, '_blank');
}
}