-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.js
510 lines (461 loc) · 18.1 KB
/
lib.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// async wrapping for chrome.storage.sync.set
const set_storage = async function (obj)
{
return new Promise((resolve, reject) =>
{
try
{
chrome.storage.sync.set(obj,
function ()
{
resolve();
});
}
catch (ex)
{
reject(ex);
}
});
};
// async wrapping for chrome.storage.sync.get
const get_storage = async function (key,default_value=undefined)
{
return new Promise((resolve, reject) =>
{
try
{
chrome.storage.sync.get(key,
function (value)
{
if (value[key]!==undefined)
{
resolve(value[key]);
}
else if (key===null)
{
resolve(value)
}
else
{
resolve(default_value)
}
});
}
catch (ex)
{
reject(ex);
}
});
};
// async wrapping for chrome.storage.sync.remove
const remove_storage = async function (key_or_keys)
{
return new Promise((resolve, reject) =>
{
try
{
chrome.storage.sync.remove(key_or_keys,
function ()
{
resolve();
});
}
catch (ex)
{
reject(ex);
}
});
};
function is_valid_text_node(node)
{
if (node.nodeType === 3 &&
node.parentElement.tagName.toLowerCase() !== 'script' &&
node.textContent.trim())
{
return true
}
else
return false
}
async function get_page_DOIs(url)
{
console.log("Storage content:",await get_storage(null))
console.log('Querying:',url)
return await get_storage(url,[])
}
function remove_tailing_dot(text)
{
while (text[text.length-1]==='.')
{
text = text.substr(0,text.length-1)
}
return text
}
function replace_text_node_with_dom_obj(text_node,search_text,dom_obj)
{
let text_content = text_node.nodeValue
let first_split_pos = text_content.search(search_text)
if (first_split_pos!==-1)
{
let second_node = text_node.splitText(first_split_pos+search_text.length)
text_node.parentElement.insertBefore(dom_obj,second_node)
replace_text_node_with_dom_obj(second_node,search_text,dom_obj)
}
}
function is_URL(text)
{
try
{
new URL(text)
return true
}
catch (e)
{
if (!e.message.trim().startsWith("Failed to construct 'URL': Invalid URL"))
{
console.log("Unknown error happened",e,text)
}
else
{
//pass
}
return false
}
}
// find DOI from string
function match_doi(text)
{
try
{
if (!text.trim().startsWith('#') &&
!text.trim().startsWith('.'))
text = decodeURIComponent(text)
}
catch (e)
{
//console.log("ERROR IGNORED (This is normal if the URL is weird):",e,text)
}
//匹配:
// https://doi.org/10.1002/anie.202105092
// 10.1021/acs.orglett.5b00297
// 对wiley的特殊情况:https://doi.org/10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C
// 行文中的情况:《自然•化学》(Nature Chemistry, 2021, DOI: 10.1038/s41557-021-00778-z)。近日,2021年诺贝尔化学奖颁给了德国化学家本杰明·利斯特(Benjamin List),以及美国
// 排除多余文字如"(blabla, DOI: 10.1021/acs.orglett.5b00297)"不要把括号吃进去
// 排除中文字符和中文标点
// 只有10.1002允许有括号
// 括号必须在10.开头之后左右匹配
//
// 特殊情况举例:
// https://chem.xmu.edu.cn/xwdt/kyzc.htm;
// https://chem.xmu.edu.cn/info/1274/11287.htm;
let doi_reg_all = RegExp(/(10(?:\.\d+)+(?:\/[^\s&\"\/]+)+)/g)
if (text.search('10.') !== -1 && doi_reg_all.test(text))
{
let re_ret = text.match(doi_reg_all)
if (re_ret)
{
let matched_doi = re_ret[0]
matched_doi = matched_doi.split(/[^\x00-\x7F]/g,1)[0]
// 如果不是wiley的,不允许出现括号
if ((!matched_doi.startsWith('10.1002')) && (!matched_doi.startsWith('10.1016')))
{
matched_doi = matched_doi.split(/[()]/g,1)[0]
}
// 如果是10.1002开头或10.1016开头,自左向右匹配,直到第一个无左括号的右括号
else
{
let cut = 0
let matching_status = 0
while(cut<matched_doi.length)
{
if (matched_doi[cut]=='(')
{
matching_status++
}
if (matched_doi[cut]==')')
{
matching_status--
}
if (matching_status<0)
{
break
}
cut++
}
matched_doi = matched_doi.substr(0,cut)
}
// 10.1055会出现自己在doi后面加个pdf的情况
// dx.doi.org/10.1055/s-1997-1294 中会识别到 10.1055/s-1997-1294.pdf
if (matched_doi.startsWith('10.1055'))
{
if (matched_doi.endsWith('.pdf'))
{
matched_doi = matched_doi.substring(0,matched_doi.length-4)
}
}
return remove_tailing_dot(matched_doi)
}
}
// 为nature特意写一条,因为nature文章上半页生成不了链接
// https://www.nature.com/articles/s41586-021-03878-5.pdf ==> 10.1038/s41586-021-03878-5
// https://wayf.springernature.com/?redirect_uri=https://www.nature.com/articles/nchem.110 ==> 10.1038/nchem.110
if (text.search("nature.com")!==-1)
{
let nature_doi_reg = RegExp(/articles\/([^\s&\/]+)/g)
let re_ret = nature_doi_reg.exec(text)
if (re_ret)
{
let matched_doi = "10.1038/"+re_ret[1]
if (matched_doi.endsWith('.pdf'))
matched_doi = matched_doi.substr(0,matched_doi.length-4)
console.log(matched_doi)
return remove_tailing_dot(matched_doi)
}
}
// ChemComm的特殊链接 http://xlink.rsc.org/?DOI=C6CC05568K
if (is_URL(text))
{
let url_object = new URL(text)
// Split the host into its sections
let hostParts = url_object.host.split('.');
// Check if the last two sections of the host are 'rsc.org'
if (hostParts.length >= 2
&& hostParts[hostParts.length - 2] === 'rsc'
&& hostParts[hostParts.length - 1] === 'org')
{
let doi = url_object.searchParams.get('DOI')
if (doi)
{
return "10.1039/"+doi
}
}
// https://pubs.rsc.org/en/content/articlelanding/2017/cc/c6cc05568k
const pattern = /cc\/([a-zA-Z0-9]+)/;
const matches = text.match(pattern);
// If the pattern matches, transform and return the string
if (matches && matches[1])
{
return `10.1039/${matches[1].toUpperCase()}`;
}
}
}
// Test situations for the doi matching function
function test_match_doi_function()
{
let test_cases = ['hahaha',
'10.1021/acs.orglett.5b00297',
'哈哈哈https://doi.org/10.1002/anie.202105092',
'哈哈哈https://doi.org/10.1002/anie.202105092)',
'https://doi.org/10.1002/anie.202105092',
'10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C',
'https://doi.org/10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C',
'对wiley的特殊情况:https://doi.org/10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C',
'对wiley的特殊情况:https://doi.org/10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C)',
'《自然•化学》(Nature Chemistry, 2021, DOI: 10.1038/s41557-021-00778-z)。近日,2021年诺贝尔化学奖颁给了德国化学家本杰明·利斯特(Benjamin List),以及美国',
"(blabla, DOI: 10.1021/acs.orglett.5b00297)",
"DOI: 10.1021/acs.orglett.5b00297)",
'http://xlink.rsc.org/?DOI=C6CC05568K',
"https://lls.reaxys.com/xflink?aulast=Xu&title=Bioorganic%20and%20Medicinal%20Chemistry%20Letters&volume=29&issue=19&spage=&date=2019&coden=BMCLE&doi=10.1016%2Fj.bmcl.2019.126630&issn=1464-3405",
"https://www.thieme-connect.de/products/ejournals/pdf/10.1055/s-1997-1294.pdf",
"https://pubs.rsc.org/en/content/articlelanding/2017/cc/c6cc05568k"]
let answers = [undefined,
"10.1021/acs.orglett.5b00297",
"10.1002/anie.202105092",
"10.1002/anie.202105092",
"10.1002/anie.202105092",
"10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C",
"10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C",
"10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C",
"10.1002/1521-3773(20001117)39:22<3964::AID-ANIE3964>3.0.CO;2-C",
"10.1038/s41557-021-00778-z",
"10.1021/acs.orglett.5b00297",
"10.1021/acs.orglett.5b00297",
"10.1039/C6CC05568K",
"10.1016/j.bmcl.2019.126630",
"10.1055/s-1997-1294",
"10.1039/C6CC05568K"]
console.log("-----------Test start-----------")
for (let i=0;i<test_cases.length;i++)
{
let test_case = test_cases[i]
let answer = answers[i]
if (match_doi(test_case)===answer)
console.log("PASS",test_case,match_doi(test_case),answer)
else
console.log('FAILFAILFAIL',test_case,match_doi(test_case),answer)
}
console.log("-----------Test end-----------")
}
// test_match_doi_function()
function get_dict_value(dict,key,def)
{
if (dict && key in dict)
{
return dict[key]
}
else
{
return def
}
}
// convert DOI to sci-hub or lib-gen download links using the user-set link template in popup.html
async function doi_to_link(doi,override_choice = false)
{
let settings_storage = await get_storage('settings')
let template = ""
let site_choice = undefined
if (!override_choice)
site_choice = settings_storage["pdf_link_radio_choice"]
else
site_choice = override_choice
if (site_choice === 'sci-hub')
template = settings_storage["scihub_link"]
else
template = settings_storage["libgen_link"]
let regex = new RegExp(/[<>:"\/\\|?*%]/,'g')
let download_filename = doi.replace(regex, '_') + '.pdf'
let ret = template.replace(/\[DOI\]/g,doi).replace(/\[DOI_FILENAME\]/g,download_filename)
return ret
}
// test whether current page is already sci-hub or lib-gen download page (used as icons will not be created in these pages)
const already_is_download_page = async function()
{
let settings_storage = await get_storage('settings')
let sci_hub_url = settings_storage["scihub_link"]
let lib_gen_url = settings_storage["libgen_link"]
//如果是Lib-Gen或者Sci-Hub的下载页,不再创建链接按钮
let current_page_url = window.location.href
if (current_page_url.startsWith(lib_gen_url.split(/[\[\]\?]/g)[0]))
{
return true
}
if (current_page_url.startsWith(sci_hub_url.split(/[\[\]\?]/g)[0]))
{
return true
}
}
// create the green download icon, the icon will have the link, but what's actually effective is the onclick function
// this way, the new tab will not be focused, to avoid tab switching.
const create_download_icon = async function(doi)
{
let a = document.createElement("a");
let download_button_img_src = chrome.runtime.getURL("images/Download_button.png")
let download_button_hover_img_src=chrome.runtime.getURL("images/Download_button_hover.png")
let download_button_pressed_img_src=chrome.runtime.getURL("images/Download_button_pressed.png")
let link_from_doi = await doi_to_link(doi)
a.setAttribute('href',link_from_doi)
let img = document.createElement("img");
img.setAttribute("src", download_button_img_src)
img.onmouseover = function(){
this.src=download_button_hover_img_src
}
img.onmouseout = function(){
this.src=download_button_img_src
}
img.onmousedown = function()
{
this.src=download_button_pressed_img_src
}
img.onmouseup = function()
{
this.src=download_button_hover_img_src
}
a.setAttribute('target',"_blank")
img.setAttribute('style',"height:15px;width:20.5px")
a.appendChild(img)
a.classList.add('LYH_download_icon')
a.onclick = function() {
chrome.runtime.sendMessage({"CreateTab": link_from_doi});
return false;
}
return a
}
const open_libgen_from_DOI = async function(doi)
{
location.replace(await doi_to_link(doi,'lib-gen'))
}
const open_scihub_from_DOI = async function(doi)
{
location.replace(await doi_to_link(doi,'sci-hub'))
}
const open_scihub_from_libgen_error = async function()
{
await open_scihub_from_DOI(match_doi(window.location.href),'lib-gen')
}
async function set_bkg_settings(key, value)
{
let settings_storage = await get_storage("settings")
settings_storage[key] = value
await set_storage({settings:settings_storage})
//console.log("Setting change required:", key, value)
}
async function get_bkg_settings(key)
{
let settings_storage = await get_storage("settings")
return settings_storage[key]
}
// get the active tab URL, used in popup and background js
function getCurrentTabUrl(callback)
{
chrome.tabs.query({active: true, currentWindow: true},
function (tabs)
{
let tab = tabs[0];
let url = tab.url;
callback(url);
}
);
}
// filter through doi containing each other
// e.g. remove 10.1021/acs.orglett.5b00297/suppl_file/ol5b00297_si_001.pdf if 10.1021/acs.orglett.5b00297 existed
// while 10.1021/acs.orglett.5b002 should be kept even if 10.1021/acs.orglett.5b00 existed
function redundant_doi(DOI1,DOI2)
{
let char_to_test = ["/"]
if (DOI1===DOI2)
return false
for (let char of char_to_test)
{
if (DOI2.startsWith(DOI1+char))
{
return DOI2
}
if (DOI1.startsWith(DOI2+char))
{
return DOI1
}
}
return false
}
// filter through doi containing extra pieces
// e.g. remove the #xxx part in 10.1021/acs.orglett.5b00297#title if 10.1021/acs.orglett.5b00297 existed
// while 10.1021/acs.orglett.5b00297#title should be kept if 10.1021/acs.orglett.5b00297 doesn't exist
// return a 2-tuple, everywhere the tuple[0] is recognized as DOI, it should be replaced with tuple[1]
function need_to_replace_doi(DOI1,DOI2)
{
let char_to_test = ['?',"&",'#']
if (DOI1===DOI2)
return false
for (let char of char_to_test)
{
if (DOI2.startsWith(DOI1+char))
{
return [DOI2,DOI1]
}
if (DOI1.startsWith(DOI2+char))
{
return [DOI1,DOI2]
}
}
return false
}
// listen to x-mol opening request from shortcut call, then open the x-mol page, this will save the hassle to obtain the selected text of the active tab from the background or popup page
function x_mol_request_listener(request, sender, sendResponse)
{
// console.log(request,'heard from message')
if ("x_mol_search" in request)
{
// console.log('x-mol search request received from shortcut')
chrome.runtime.sendMessage({"CreateTab": "https://www.x-mol.com/q?option=" + window.getSelection().toString()});
}
}