-
Notifications
You must be signed in to change notification settings - Fork 0
/
paperTranslator.js
76 lines (73 loc) · 2.62 KB
/
paperTranslator.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
/*
<script type="text/javascript" src="file:///D:/飞舞的冰龙/Documents/文献/dicScript/dictionary.js"></script>
<script type="text/javascript" src="file:///D:/飞舞的冰龙/Documents/文献/dicScript/paperTranslator.js"></script>
<link rel="stylesheet" href="file:///D:/飞舞的冰龙/Documents/文献/dicScript/style.css">
*/
/**
* 自动寻找释义并增加释义
* @param content {ChildNode} 节点
*/
function addMeans(content) {
let contentStr = content.textContent,
means = []
const contentArr = contentStr.split(' ')
const spaceSize = content.style.wordSpacing
for (let i = contentArr.length - 1; i >= 0; i--) {
let mean = [],
temMean = '',
spanIndex = 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()]) {
temMean = dictionary[contentArr.slice(i - 2, i + 1).join(' ').toLowerCase()]
spanIndex = contentStr.search(contentArr[i-2].match(/[a-zA-Z-]+/)[0])
mean = [temMean, calLeft(spanIndex, i-2, spaceSize)]
i -= 2
} else if (dictionary[contentArr.slice(i - 1, i + 1).join(' ').toLowerCase()]) {
temMean = dictionary[contentArr.slice(i - 1, i + 1).join(' ').toLowerCase()]
spanIndex = contentStr.search(contentArr[i-1].match(/[a-zA-Z-]+/)[0])
mean = [temMean, calLeft(spanIndex, i-1, spaceSize)]
i -= 1
}
}
if(mean.length==0 && dictionary[contentArr[i].match(/[a-zA-Z-]+/)[0].toLowerCase()]){
temMean = dictionary[contentArr[i].match(/[a-zA-Z-]+/)[0].toLowerCase()]
spanIndex = contentStr.search(contentArr[i].match(/[a-zA-Z-]+/)[0])
mean = [temMean, calLeft(spanIndex, i, spaceSize)]
}
}
if(mean.length>0){
means.push(mean)
}
}
means.forEach(mean => {createMeanDiv(content, mean)})
}
/**
* 添加释义节点
* @param {Element} ele 当前行节点
* @param {String} mean 释义
*/
function createMeanDiv(ele, mean){
let meanDiv = document.createElement('div')
meanDiv.className = 'blue'
meanDiv.innerHTML = mean[0]
meanDiv.style.left = mean[1] + 'em'
ele.appendChild(meanDiv)
}
/**
* 计算 left 距离
* @param {Number} spanIndex 位置
* @param {Number} i 空格数
* @param {String} spaceSize 空格宽度
*/
function calLeft(spanIndex, i, spaceSize){
return (spanIndex-i)*0.9+i*parseFloat(spaceSize)
}
window.onload = function () {
console.log('解释开始')
let spanEle = document.querySelectorAll('span')
for (const content of spanEle) {
addMeans(content)
}
console.log('解释结束');
}