-
Notifications
You must be signed in to change notification settings - Fork 0
/
onlineTranslator.js
107 lines (101 loc) · 3.51 KB
/
onlineTranslator.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
// ==UserScript==
// @name onlineTranlator
// @namespace http://0.0.0.0/
// @version 0.8.5
// @description 在线阅读文献时,注释生物学相关用词。
// @author Icedragon
// @match https://www.ncbi.nlm.nih.gov/pubmed/*
// @match file:///home/icedragon/Documents/Zotero/*
// @exclude https://www.ncbi.nlm.nih.gov/pubmed/?term*
// @match https://www.nature.com/articles/*
// @match https://www.ncbi.nlm.nih.gov/pmc/articles/*
// @match https://genomebiology.biomedcentral.com/articles/*
// @match http://www.plantcell.org/content/*
// @match https://www.sciencedirect.com/science/article/*
// @match https://microbiomejournal.biomedcentral.com/articles/*
// @match https://onlinelibrary.wiley.com/doi/full/*
// @match https://www.cell.com/cell/fulltext/*
// @resource bioDict.json https://github.com/flyingicedragon/paperDict/raw/master/bioDict.min.json
// @grant GM_getResourceText
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function () {
function trans(content) {
/**
* 自动寻找释义并增加释义
* @param content {ChildNode} 节点
*/
let contentStr = content.textContent
const contentArr = contentStr.split(' ')
const dictStr = GM_getValue('paperdict_dict', null)
if (dictStr == null) {
console.log('无法加载词库')
}
const dictionary = JSON.parse(dictStr)
for (let i = contentArr.length - 1; i >= 0; i--) {
let hasTrans = 0
if (contentArr[i].match(/[a-zA-Z-]+/)) {
if (i > 1 && contentArr[i - 1].match(/[a-zA-Z-]+/)) {
if (i > 2 && contentArr[i - 2].match(/[a-zA-Z-]+/) && dictionary[contentArr.slice(i - 2, i + 1).join(' ').toLowerCase()]) {
contentArr[i] += ' (' + dictionary[contentArr.slice(i - 2, i + 1).join(' ').toLowerCase()] + ')'
i -= 2
hasTrans = 1
} else if (dictionary[contentArr.slice(i - 1, i + 1).join(' ').toLowerCase()]) {
contentArr[i] += ' (' + dictionary[contentArr.slice(i - 1, i + 1).join(' ').toLowerCase()] + ')'
i -= 1
hasTrans = 1
}
}
if (!hasTrans && dictionary[contentArr[i].match(/[a-zA-Z-]+/)[0].toLowerCase()]) {
contentArr[i] += ' (' + dictionary[contentArr[i].match(/[a-zA-Z-]+/)[0].toLowerCase()] + ')'
}
}
}
contentStr = contentArr.join(' ')
content.textContent = contentStr
}
let start = function () {
/**
* 开始翻译
*/
console.log('翻译开始')
let pEles = document.querySelectorAll('p, h1, h2, h3, h4')
for (const pEle of pEles) {
for (const content of pEle.childNodes) {
trans(content)
}
}
console.log('翻译结束');
}
let update_dict = function () {
/**
* 更新词库
*/
console.log('更新词库')
const dictStr = GM_getResourceText('bioDict.json')
GM_setValue('paperdict_dict', dictStr)
console.log('更新完成')
}
let buttonEle
buttonEle = document.createElement('input')
buttonEle.type = 'button'
buttonEle.value = '翻译'
buttonEle.onclick = start
buttonEle.style.position = 'fixed'
buttonEle.style.top = '300px'
buttonEle.style.right = '100px'
buttonEle.style.height = '30px'
buttonEle.style.width = '60px'
document.body.appendChild(buttonEle)
buttonEle=document.createElement('input')
buttonEle.type = 'button'
buttonEle.value = '更新'
buttonEle.onclick = update_dict
buttonEle.style.position = 'fixed'
buttonEle.style.top = '350px'
buttonEle.style.right = '100px'
buttonEle.style.height = '30px'
buttonEle.style.width = '60px'
document.body.appendChild(buttonEle)
}())