-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
161 lines (147 loc) · 5.43 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pica/9.0.1/pica.min.js" integrity="sha512-FH8Ofw1HLbwK/UTvlNBxsfICDXYZBr9dPuTh3j17E5n1QZjaucKikW6UwMREFo7Z42AlIigHha3UVwWepr0Ujw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style>
canvas {
border: solid;
border-color: red;
}
</style>
<title>Make spotlight</title>
</head>
<body>
<div class="container mx-auto col-6">
<h1>Make spotlight</h1>
<form class="form-group" id="main-form">
<div class="row align-items-center mb-3">
<div class="col-auto">
<label for="pmid">PMID</label>
<input type="text" class="form-control" id="pmid" value="">
<label for="pmid">Image</label>
<input type="file" class="form-control" accept="image/*" name="image" id="file">
</div>
</div>
<input id='submit-button' type="submit" class="btn btn-primary m-2" />
</form>
<pre><code id="json-result">
</code></pre>
</div>
</div>
</body>
<footer>
<script>
async function getPubMedInfo(pmid) {
const pmid_numbers = pmid.replace('PMID:','')
const baseUrl = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=json&rettype=xml&id=';
const response = await fetch(baseUrl + pmid);
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(await response.text(), "text/xml");
let year = '';
if (xmlDoc.querySelector('PubDate Year')){
year = xmlDoc.querySelector('PubDate Year').textContent
}
else if (xmlDoc.querySelector('PubDate MedlineDate')){
year = xmlDoc.querySelector('PubDate MedlineDate').textContent.split(/\s/)[0]
}
else {year = '?????????????????????????????????'}
const journalName = xmlDoc.querySelector('Journal ISOAbbreviation').textContent
const firstAuthor = xmlDoc.querySelector('Author LastName').textContent
const title = xmlDoc.querySelector('Article ArticleTitle').textContent
let doi = ''
if (xmlDoc.querySelector('Article ELocationID[EIdType="doi"]')){
doi = 'https://doi.org/' + xmlDoc.querySelector('Article ELocationID[EIdType="doi"]').textContent
}
else {
doi = 'https://doi.org/' + xmlDoc.querySelector('ArticleId[IdType="doi"]').textContent
}
const date = (new Date()).toISOString().split('T')[0]
const authorEtAl = `${firstAuthor} et al, ${year}`
const imageName = `${date}-PMID${pmid_numbers}.png`
const imageFile = document.getElementById('file').files[0]
if (imageFile) {
const image = new Image();
image.src = URL.createObjectURL(imageFile);
await new Promise(resolve => image.onload = resolve);
resizeImage(image,imageName)
}
return `{
"title": "${authorEtAl}",
"title_link": null,
"panel_type": "spotlight",
"head_image": [
"spotlight/${imageName}"
],
"content": "${title}\\n\\nOriginally published in <a href='${doi}'>${journalName}</a>.",
"reference_id": "PMID:${pmid_numbers}",
"link_label": "Publication record in PomBase ...",
"date_added": "${date}",
"show_on_front_page": true
},`
}
async function clickFunction(e) {
e.preventDefault()
const string2print = await getPubMedInfo(document.getElementById("pmid").value)
document.getElementById("json-result").textContent = string2print
}
async function resizeImage(image, imageName)
{
const aspectRatio = image.naturalWidth / image.naturalHeight;
let newWidth = 300;
let newHeight = 185;
if (newWidth / aspectRatio > newHeight) {
newWidth = newHeight * aspectRatio;
} else {
newHeight = newWidth / aspectRatio;
}
console.log(newWidth, newHeight, newWidth / newHeight, aspectRatio)
const canvas = document.createElement('canvas');
const canvas2 = document.createElement('canvas');
canvas2.width = 300;
canvas2.height = 185;
const ctx = canvas2.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas2.width, canvas2.height);
await pica().resize(image, canvas, {
quality: 3,
alpha: true,
unsharpAmount: 80,
unsharpRadius: 0.6,
unsharpThreshold: 2,
resizeWidth: newWidth,
resizeHeight: newHeight,
reduce: true,
alpha: false,
});
const dx = (300 - newWidth) / 2;
const dy = (185 - newHeight) / 2;
ctx.drawImage(
canvas,
dx,
dy,
newWidth,
newHeight
);
const dataUrl = canvas2.toDataURL('image/png');
const link = document.createElement('a');
link.setAttribute('href', dataUrl);
link.setAttribute('download', imageName);
document.body.appendChild(link);
link.click()
document.body.appendChild(canvas);
document.body.appendChild(canvas2);
}
window.onload = () => {
document
.getElementById("submit-button")
.addEventListener("click",clickFunction );
};
</script>
</footer>
</html>