-
Notifications
You must be signed in to change notification settings - Fork 2
/
inline_translator.js
218 lines (185 loc) · 7 KB
/
inline_translator.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
// this file is for bookmarklet loading.
(function(){
var SCRIPT_URL = "http://miya.pe.kr/inline_translator.js";
var TEXT_LENGTH_LIMIT = 500;
var TARGET_LANGUAGE = "ko";
var InlineTranslator = {
init: function(targetWindow) {
var this_ = this;
this.window_ = targetWindow;
this.activated_ = false;
var outputElement = this.window_.document.createElement("div");
outputElement.style.position = "fixed";
outputElement.style.zIndex = "9999999";
outputElement.style.display = "block";
outputElement.style.top = "0";
outputElement.style.left = "0";
outputElement.style.padding = "5px";
outputElement.style.border = "1px solid #666";
outputElement.style.backgroundColor = "#fff";
outputElement.style.color = "#006";
outputElement.innerHTML = "inine translator 로딩중입니다. :)";
this.window_.document.body.appendChild(outputElement);
this.outputElement_ = outputElement;
this.loadMSLanguageApi();
},
loadMSLanguageApi: function() {
// do nothing
this.initAfterLoadApi();
},
initAfterLoadApi: function() {
var this_ = this;
var outputElement = this.getOutputElement();
this.addEventListener(this.window_.document.body, "mouseup", function(evt) {
if (!this_.activated_) return;
var targetElement = evt.srcElement ? evt.srcElement : evt.target;
if (targetElement == this_.getOutputElement())
return;
var text = this_.getSelectedTextFromRange(this_.getRange());
if (!text || text.length > TEXT_LENGTH_LIMIT) return;
this_.translateAndShowResult(evt, text);
});
this.addEventListener(this.window_.document.body, "mousedown", function(evt) {
var targetElement = evt.srcElement ? evt.srcElement : evt.target;
if (targetElement != this_.getOutputElement())
this_.hideOutputElement();
});
outputElement.innerHTML = "inline translator가 로딩되었습니다. :)";
var loaderElement = this.window_.document.createElement("div");
loaderElement.style.position = "fixed";
loaderElement.style.zIndex = "9999998";
loaderElement.style.display = "block";
loaderElement.style.right = "0";
loaderElement.style.bottom = "0";
loaderElement.innerHTML = '<img title="inline translator를 제거하려면 클릭하세요." alt="inline translator가 실행중입니다." src="http://www.miya.pe.kr/it.png" style="cursor: pointer;" />';
// image
this.window_.document.body.appendChild(loaderElement);
loaderElement.onclick = function() {
this_.removeInlineTranslator();
this.parentNode.removeChild(this);
};
setTimeout(function() {
outputElement.style.display = "none";
outputElement.style.position = "absolute";
}, 1500);
this.activated_ = true;
},
removeInlineTranslator: function() {
if (this.isScriptIncluded(SCRIPT_URL))
this.removeScript(SCRIPT_URL);
InlineTranslator = null;
this.activated_ = false;
},
addEventListener: function(targetObject, eventType, callback) {
if (targetObject.addEventListener) {
targetObject.addEventListener(eventType, callback, false);
} else if (targetObject.attachEvent) {
targetObject.attachEvent("on"+ eventType, callback);
};
},
getOutputElement: function() {
return this.outputElement_;
},
translateAndShowResult: function(evt, text) {
var this_ = this;
var mousePosition = this.getMousePosition(evt);
this.showOutputElementWithLoadingMessage(mousePosition.x, mousePosition.y);
window.InlineTranslatorCallback = function(response) {
this_.showTranslatedResult(response);
};
var s = document.createElement("script");
s.src = "https://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId="+
"4CEC1ACDC05DD131B324891DEE15AEA26337C236"+
"&oncomplete=InlineTranslatorCallback&to=" + TARGET_LANGUAGE + "&text=" + text;
document.getElementsByTagName("head")[0].appendChild(s);
},
showTranslatedResult: function(translatedText) {
if (translatedText) {
this.showOutputElementWithText(translatedText);
} else {
this.showOutputElementWithEmptyResult();
};
},
hideOutputElement: function() {
this.getOutputElement().style.display = "none";
},
showOutputElementWithLoadingMessage: function(posX, posY) {
var outputElement = this.getOutputElement();
outputElement.innerHTML = '<span style="color: #bbb;">Seaching…</span>';
outputElement.style.display = "block";
outputElement.style.left = posX + "px";
outputElement.style.top = posY + "px";
},
showOutputElementWithText: function(text) {
var outputElement = this.getOutputElement();
outputElement.innerHTML = text;
},
showOutputElementWithEmptyResult: function() {
var outputElement = this.getOutputElement();
outputElement.innerHTML = '<span style="color: #f99;">결과없음.</span>';
},
getMousePosition: function(evt) {
var targetWindow = this.getWindow();
var evt = evt || targetWindow.event;
if (!evt) return;
var doc = targetWindow.document;
var mouseX = evt.pageX || (evt.clientX + (doc.documentElement.scrollLeft || doc.body.scrollLeft)) || 0;
var mouseY = evt.pageY || (evt.clientY + (doc.documentElement.scrollTop || doc.body.scrollTop)) || 0;
return {x: mouseX, y: mouseY};
},
getSelectedTextFromRange: function(range) {
var selectedText = "";
if (range && range.text) {
selectedText = range.text;
} else if (range && range.toString) {
selectedText = range.toString();
};
return selectedText;
},
getWindow: function() {
return this.window_;
},
getSelection: function() {
var targetWindow = this.getWindow(), selection = null;
if (targetWindow.getSelection) {
selection = targetWindow.getSelection();
} else if (targetWindow.document.selection) {
selection = targetWindow.document.selection;
};
return selection;
},
getRange: function() {
var selection = this.getSelection(), range = null;
if (selection && selection.getRangeAt && selection.rangeCount > 0) {
range = selection.getRangeAt(0);
} else if (selection && selection.createRange) {
range = selection.createRange();
}
return range;
},
isScriptIncluded: function(scriptUrl) {
var scripts = this.getWindow().document.getElementsByTagName("script");
var result = false;
for (var i=0, l=scripts.length; i<l; i++)
if (scripts[i] && scripts[i].src == scriptUrl)
result = true;
return result;
},
includeScript: function(scriptUrl) {
if (this.isScriptIncluded(GOOGLE_API_URL)) return;
var doc = this.getWindow().document;
var script = doc.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("charset", "utf-8");
script.setAttribute("src", scriptUrl);
doc.body.appendChild(script);
},
removeScript: function(scriptUrl) {
var scripts = this.getWindow().document.getElementsByTagName("script");
for (var i=0, l=scripts.length; i<l; i++)
if (scripts[i] && scripts[i].src == scriptUrl)
scripts[i].parentNode.removeChild(scripts[i]);
}
};
InlineTranslator.init(window);
})();