-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtranslation_task.js
More file actions
54 lines (50 loc) · 1.6 KB
/
translation_task.js
File metadata and controls
54 lines (50 loc) · 1.6 KB
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
function TranslationTask(words, translationBox) {
this.cancelled = false;
this.words = words;
this.finished = 0;
this.translationBox = translationBox;
this.results = [];
this.seen = {};
if (TranslationTask.activeTask != null) {
TranslationTask.activeTask.cancel();
TranslationTask.activeTask = null;
}
TranslationTask.activeTask = this;
}
TranslationTask.prototype.cancel = function() {
this.cancelled = true;
};
TranslationTask.prototype.run = function() {
for (var i = 0; i < this.words.length; i++) {
this.translate(i);
}
};
TranslationTask.prototype.translate = function(i) {
var self = this;
var word = self.words[i];
self.seen[word] = true;
chrome.runtime.sendMessage(
{'action' : 'translateWord', 'word' : word},
function (results) {
if (self.cancelled) return;
for (var j = 0; j < results.length; ++j) {
var match = results[j].value.match(/<→(.*)>/);
if (match && // Found a link to the canonical spelling.
!self.seen[match[1]]) { // Check if we don't do infinite loop.
var l = self.words.length;
self.words.push(match[1]);
self.translate(l);
// return;
}
}
Array.prototype.push.apply(self.results, results);
++self.finished;
if (self.finished == self.words.length) {
TranslationTask.activeTask = null;
if (!self.translationBox.Enabled()) return;
self.translationBox.SetContent(self.results);
self.translationBox.AdjustLocation();
self.translationBox.Fadein();
}
});
};