-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPDFFromScihub
56 lines (53 loc) · 1.59 KB
/
PDFFromScihub
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
// PDF From Scihub
// @author Polygon
// @link https://github.com/windingwind/zotero-actions-tags/discussions/124
//
// [Download PDF from Scihub]
// Name : Download PDF From Scihub
// Event : None
// Operation : Script
// Data : JS code below
// Shortcut: None
// Menu Label: Download PDF From Scihub
const ZoteroPane = require("ZoteroPane")
const Zotero = require("Zotero")
let DOI = ""
if (!item.isRegularItem()) {
return "This type is not supported"
} else if ((DOI = item.getField("DOI")).length == 0) {
return "No DOI does not support query"
}
// scihub start
let parser = new DOMParser()
let scihubUrl = "https://sci-hub.se"
let url = `${scihubUrl}/${DOI}`
let resp = await Zotero.HTTP.request("GET", url)
let htmlDoc = parser.parseFromString(resp.responseText, "text/html")
let pdfTag = htmlDoc.querySelector("#pdf")
if (pdfTag) {
let pdfUrl = pdfTag.getAttribute("src")
if (pdfUrl.startsWith("//")) {
pdfUrl = "https:" + pdfUrl
} else if (pdfUrl.startsWith("/")) {
pdfUrl = scihubUrl + pdfUrl
}
let importOptions = {
libraryID: item.libraryID,
url: pdfUrl,
parentItemID: item.id,
title: "PDF_From_Scihub",
fileBaseName: "Full_Text_by_Action",
contentType: 'application/pdf',
referrer: url,
}
// result is an attachment item
let result = await Zotero.Attachments.importFromURL(importOptions)
if (result.key) {
return "SciHub Download Successful"
}
}
// open-access start if pdf can not found in scihub
Zotero.launchURL(
`https://www.open-access.xyz/?doi=${DOI}`
)
return "Querying..."