-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.js
157 lines (128 loc) · 3.48 KB
/
inject.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
var cache = {};
var inPopup = false;
var currentPopup;
var dict;
chrome.runtime.sendMessage({action: "dict"}, function(dictInfo) {
dict = dictInfo.defDict;
});
document.addEventListener("mouseup", function(e) {
if (inPopup) {
clearPopupAfter(currentPopup);
}
else {
clearAllPopup();
}
inPopup = false;
var selection = window.getSelection();
if (selection.rangeCount > 0) {
getDefinition(selection.getRangeAt(0));
// getDefinition(selection.getRangeAt(0).cloneRange());
}
});
function clearAllPopup() {
var popups = document.querySelectorAll(".word-def-popup");
[].forEach.call(popups, function(popup) {
popup.parentNode.removeChild(popup);
});
}
function clearPopupAfter(thisPopup) {
var popups = document.querySelectorAll(".word-def-popup");
for (var i = popups.length - 1; i >= 0; i--) {
var popup = popups[i];
if (popup === thisPopup) {
break;
}
popup.parentNode.removeChild(popup);
}
}
function isEnglishWord(s) {
// Can't be a word if empty or longer than "supercalifragilisticexpialidocious"
if (s === "" || s.length > 34) {
return false;
}
var reg = /^[A-Za-z]+$/;
return reg.test(s);
}
function getDefinition(range) {
var word = range.toString().trim();
if (isEnglishWord(word)) {
chrome.runtime.sendMessage(
{
action: "definition",
word: word,
},
function(definition) {
if (definition) {
showDefinition(definition, range);
}
}
);
}
}
function showDefinition(definition, range) {
var $definition = $(definition);
var popup = createPopup();
$(popup).append($definition);
var offset = getRangeOffset(range);
popup.style.top = offset.top + "px";
popup.style.left = offset.left + "px";
var height = $(window).height() - (offset.top - window.scrollY) - 10;
if (height < 400) {
height = 400;
}
$definition.css("max-height", height + "px");
document.body.appendChild(popup);
adjustPopupPosition(popup);
}
function createPopup() {
var popup = document.createElement("div");
popup.setAttribute("class", "word-def-popup");
var tip = document.createElement("div");
tip.setAttribute("class", "word-def-popup-tip");
popup.appendChild(tip);
// popup.onmouseenter = function() {
// this.classList.remove("word-def-fade");
// };
// popup.onmouseleave = function() {
// $(this).addClass("word-def-fade");
// };
popup.onmousedown = function() {
// remove current selection
window.getSelection().removeAllRanges();
};
popup.onmouseup = function() {
inPopup = true;
currentPopup = popup;
};
return popup;
}
function getRangeOffset(range) {
var rect = range.getBoundingClientRect();
var offset = {};
offset.top = rect.bottom + window.scrollY + 10;
offset.left = rect.left + window.scrollX + (rect.right-rect.left)/2;
return offset;
}
function adjustPopupPosition(popup) {
// var seqNum = popup.getAttribute("data-word-def-popup-seq-num");
// var pairedPopup = document.querySelector("[data-word-def-popup-seq-num='" + seqNum + "']");
// if (pairedPopup !== popup) {
// popup.style.marginTop = pairedPopup.offsetHeight + "px";
// }
var popupLeft = $(popup).offset().left;
var popupWidth = $(popup).outerWidth();
var windowWidth = $(window).width();
var pml;
var tml;
var shift = -popupWidth / 2;
var pullLeft = popupLeft + popupWidth + shift - windowWidth;
if (pullLeft > 0) {
shift -= pullLeft;
}
var pullRight = -(popupLeft + shift);
if (pullRight > 0) {
shift += pullRight;
}
popup.style.marginLeft = shift + "px";
$(popup).children(".word-def-popup-tip").css("left", -shift + "px");
}