-
Notifications
You must be signed in to change notification settings - Fork 3
/
showMethods.js
71 lines (57 loc) · 2.44 KB
/
showMethods.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
// handle method
async function showMethods(windowHref, precedingText) {
let allMethods = null;
let mostSimilarMethods = null;
if (localStorage.getItem('eprMethods') != null) {
const allMethods = JSON.parse(localStorage.getItem('eprMethods'));
mostSimilarMethods = findSimilarMethods(precedingText, allMethods);
} else if (windowHref.includes("nature.com")) {
allMethods = await NaturePreScrapeMethods(windowHref);
}
if (allMethods != null) {
mostSimilarMethods = findSimilarMethods(precedingText, allMethods);
}
if (mostSimilarMethods != null) {
// display in a drawer
// 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 = 1000;
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>
`;
// Loop through each method and add its title and content to the HTML string
for(let method of mostSimilarMethods) {
drawerHTML += `
<h3>${method.title}</h3>
<div>${method.content}</div>
`;
}
// 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();
});
}
}