generated from MuiseDestiny/zotero-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 106
/
utils.ts
162 lines (156 loc) · 6.58 KB
/
utils.ts
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
162
import Requests from "zotero-reference/src/modules/requests";
import { config } from "../../package.json";
import LocalStorage from "./localStorage";
const requests = new Requests()
export function getPublicationTitle(item: Zotero.Item) {
return [item.getField("publicationTitle"), item.getField("proceedingsTitle")].find(i => i.trim().length > 0) || ""
}
/**
* @param localStorage
* @param item
* @param tip
* @returns -1 失败 0 数据库无信息 1 成功
*/
export async function updatePublicationTags(localStorage: LocalStorage, item: Zotero.Item, tip: boolean = false) {
const publicationTitle = getPublicationTitle(item)
let secretKey = Zotero.Prefs.get(`${config.addonRef}.easyscholar.secretKey`) as string
if (!secretKey) { return -1}
let response
try {
response = await requests.get(
`https://easyscholar.cc/open/getPublicationRank?secretKey=${secretKey}&publicationName=${encodeURIComponent(publicationTitle)}`,
)
} catch { console.log(response) }
if (response && response.data) {
// 自定义数据集 + 官方数据集合并
let officialAllData={...response.data.officialRank.all}
if (Object.keys(officialAllData).length === 0) {
if (tip) {
new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true })
.createLine({ text: "Not Found", type: "default" }).show()
}
await localStorage.set(item, "publication", "")
}
let customRankInfo = response.data.customRank.rankInfo
response.data.customRank.rank.forEach((rankString: string) => {
try {
// 1613160542602600448&&&2
let uuid: string, rank: string;
[uuid, rank] = rankString.split("&&&")
rank = {
"1": "oneRankText",
"2": "twoRankText",
"3": "threeRankText",
"4": "fourRankText",
"5": "fiveRankText"
}[rank] as string
let info = customRankInfo.find((i: any) => i.uuid == uuid)
officialAllData[info.abbName] = info[rank]
} catch { }
})
if (Object.keys(officialAllData).length === 0){
return 0;
}
else{
await localStorage.set(item, "publication", officialAllData)
// 显示它支持的所有字段
if (tip) {
let popupWin = new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true }).show()
popupWin.createLine({ text: publicationTitle, type: "default" })
Object.keys(officialAllData).forEach(k => {
popupWin.createLine({ text: `${k}: ${officialAllData[k]}`, type: "success" })
})
ztoolkit.ItemTree.refresh()
}
return 1
}
}
}
// let lock: undefined | _ZoteroPromiseObject = undefined
// const utils = {
// requests: new Requests(),
// localStorage: new localStorage(config.addonRef),
// wait(item: Zotero.Item, key: string, local: boolean=true) {
// switch (key) {
// case "publication":
// const publicationTitle = this.getPublicationTitle(item)
// if (publicationTitle == "") { return }
// let data
// if (local) {
// try {
// data = this.localStorage.get(item, key)
// } catch {}
// }
// if (data !== undefined) { return data }
// // 开启一个异步更新影响因子
// let secretKey = Zotero.Prefs.get(`${config.addonRef}.easyscholar.secretKey`) as string
// window.setTimeout(async () => {
// this.requests.cache = {}
// if (secretKey) {
// await lock;
// lock = Zotero.Promise.defer()
// let response
// try {
// response = await this.requests.get(
// `https://easyscholar.cc/open/getPublicationRank?secretKey=${secretKey}&publicationName=${encodeURIComponent(publicationTitle)}`,
// )
// } catch { console.log(response) }
// // 延迟
// await Zotero.Promise.delay(1000)
// lock!.resolve()
// if (response && response.data) {
// // 自定义数据集+官方数据集合并
// let officialAllData = response.data.officialRank.all
// if (!officialAllData) {
// if (!local) {
// new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true })
// .createLine({ text: "Not Found", type: "default" }).show()
// }
// return await this.localStorage.set(item, key, "")
// }
// let customRankInfo = response.data.customRank.rankInfo
// response.data.customRank.rank.forEach((rankString: string) => {
// try {
// // 1613160542602600448&&&2
// let uuid: string, rank: string;
// [uuid, rank] = rankString.split("&&&")
// rank = {
// "1": "oneRankText",
// "2": "twoRankText",
// "3": "threeRankText",
// "4": "fourRankText",
// "5": "fiveRankText"
// }[rank] as string
// let info = customRankInfo.find((i:any) => i.uuid == uuid)
// officialAllData[info.abbName] = info[rank]
// } catch {}
// })
// if (officialAllData) {
// await this.localStorage.set(item, key, officialAllData)
// console.log(item.key, officialAllData)
// // 显示它支持的所有字段
// let popupWin = new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true }).show()
// popupWin.createLine({text: publicationTitle, type: "default"})
// Object.keys(officialAllData).forEach(k => {
// popupWin.createLine({ text: `${k}: ${officialAllData[k]}`, type: "success" })
// })
// // ztoolkit.ItemTree.refresh()
// }
// }
// } else {
// if (!local) {
// new ztoolkit.ProgressWindow("Publication Tags", { closeTime: 3000, closeOtherProgressWindows: true })
// .createLine({ text: "No easyScholar secret key configured", type: "fail" }).show()
// }
// }
// })
// return
// default:
// break
// }
// },
// getPublicationTitle(item: Zotero.Item) {
// return [item.getField("publicationTitle"), item.getField("proceedingsTitle")].find(i=>i.trim().length > 0)
// }
// }
// export default utils