-
Notifications
You must be signed in to change notification settings - Fork 3
/
showRef.js
126 lines (107 loc) · 4.86 KB
/
showRef.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
// Take a website link and reference number, show title, authors, abstract in the modal
async function showRef(windowHref, refNum) {
// from current paper, get the refernce paper details
// ref is an object with following keys: Title,PubMedLink, GoogleScholarLink,CASLink, ArticleLink
let ref = null;
if (localStorage.getItem('eprRefs') != null) {
const allRefs = JSON.parse(localStorage.getItem('eprRefs'));
ref = allRefs[refNum-1];
} else {
// nature
if (windowHref.includes("nature.com")) {
const allRefs = await NaturePreScrapeRef(windowHref);
ref = allRefs[refNum-1];
} else if (windowHref.includes("science.org")) {
const allRefs = await SciencePreScrapeRef(windowHref);
ref = allRefs[refNum-1];
}
}
let refPaperContent = {'title': '', 'authors': '', 'abstract': ''};
// Plan: try extracting all 3 detials from pubmed. If it fails, extract from crossref
if (ref != null) {
if (ref.PubMedLink != '' || ref.ArticleLink != '') { // either pubmed or cross ref
// try pubmed first
let pubmedContent = null;
if (ref.PubMedLink != '') {
// get it from cache or scrape
const localAbstracts = localStorage.getItem('eprPrescrapedAbstracts');
if (refNum <= 50 && localAbstracts != null) {
console.log("Got abstact from LocalStorage")
pubmedContent = JSON.parse(localAbstracts)[refNum-1];
} else {
pubmedContent = await scrapePubmed(ref.PubMedLink);
}
} else {
pubmedContent = {};
}
if (!isObjEmpty(pubmedContent)) {
refPaperContent.title = pubmedContent.title;
refPaperContent.authors = pubmedContent.authors.join(', ');
refPaperContent.abstract = pubmedContent.abstract;
} else { // if no content from pubmed, try crossref
if (ref.ArticleLink != '') {
const articleDoi = ref.ArticleLink.replace("https://doi.org/", "");
crossRefContent = await getCrossRef(articleDoi);
const { title, author, abstract } = crossRefContent.message;
// Convert the authors array into a string, with each author separated by a comma
const authorsNames = author.map(a => a.given + ' ' + a.family);
const authorsStr = authorsNames.join(', ');
refPaperContent.title = title;
refPaperContent.authors = authorsStr;
refPaperContent.abstract = abstract || '';
}
}
} else { // no pubmed, no cross ref, just show title
refPaperContent.title = ref.Title;
}
} // ref != null
if (refPaperContent.title != ''){
// Remove any existing drawers
const existingDrawer = document.getElementById('sideDrawer');
if (existingDrawer) {
existingDrawer.remove();
}
// Create a new div with a side drawer and cross button
const drawer = document.createElement('div');
drawer.id = 'sideDrawer'; // Assign an id to the drawer
drawer.style.position = 'fixed';
drawer.style.zIndex = 9999;
drawer.style.right = '0';
drawer.style.top = '0';
drawer.style.height = '100vh';
drawer.style.width = '40%';
drawer.style.backgroundColor = '#fff';
drawer.style.overflow = 'auto';
drawer.style.padding = '20px';
drawer.style.boxShadow = '-2px 0 8px rgba(0, 0, 0, 0.2)';
// Begin constructing the HTML to be inserted into the drawer
let drawerHTML = `
<button id="closeDrawer" style="position: fixed; right: 20px; top: 20px; background: none; border: none; font-size: 20px;">×</button>
`;
// Generate the HTML string
// drawerHTML += `
// <h3>${title}</h3>
// <h5>${authorsStr}</h5>
// <p>${abstract}</p>
// `;
drawerHTML += `
<h3>${refNum}. ${refPaperContent.title}</h3><br />
<h5>${refPaperContent.authors}</h5><br />
<p>${refPaperContent.abstract}</p>
`;
// Set the drawer's HTML
drawer.innerHTML = drawerHTML;
// Add the drawer to the page
document.body.appendChild(drawer);
// Add an event listener for the cross button
document.getElementById('closeDrawer').addEventListener('click', function() {
// Remove the selection (highlight) from the page, else closed drawer will reopen
window.getSelection().removeAllRanges();
// close the drawer
drawer.remove();
});
}
}
function isObjEmpty(obj) {
return Object.keys(obj).length === 0;
}