-
Notifications
You must be signed in to change notification settings - Fork 1
/
inject.js
67 lines (61 loc) · 2.19 KB
/
inject.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
(function() {
var articles = document.body.getElementsByClassName('gs_ri');
console.log(articles.length);
for (var i = 0; i < articles.length; i++) {
let button = document.createElement('a');
button.className = "credo_button";
button.innerHTML = "EndNote and SciHub";
button.addEventListener('click', onClick(articles[i]));
var whereAdd = articles[i].getElementsByClassName('gs_fl')[0]
whereAdd.appendChild(button);
}})();
function onClick(article) {
return function() {
downloadSciHub(article);
downloadEndNote(article);
};
}
function downloadEndNote(article) {
var citeLink = article.getElementsByClassName('gs_or_cit gs_nph')[0]
citeLink.click(); // open Cite window
setTimeout(function(){
var elementExists = document.getElementById("gs_citi");
if (!!elementExists) {
var endNoteLink = elementExists.children[1].href; // 1 for EndNote
console.log("endNoteLink: "+ endNoteLink);
window.open(endNoteLink,"_self"); //download
//downloadURI(endNoteLink, "endnote.enw")
console.log("endnote success download")
}
document.getElementById("gs_cit-x").click(); //close Cite window
},1500); //ms to show window, need to be encreased if not working
}
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
function downloadSciHub(article) {
var sciHubLink = 'https://sci-hub.tw/';
var articleLink = article.getElementsByTagName('a')[0].href
console.log("sciHubLink: "+sciHubLink + articleLink);
httpGetAsync(sciHubLink + articleLink)
}
function httpGetAsync(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
var parser = new DOMParser();
var doc = parser.parseFromString(xmlHttp.responseText, "text/html");
pdfLink = doc.getElementById("pdf").src;
console.log("pdfLink: "+pdfLink);
window.open(pdfLink,"_blank"); //download
}
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}