Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
- pre text translation
- fixed batch translation
  • Loading branch information
ilih-ilih committed Jun 14, 2022
1 parent dc4427c commit bea9b84
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 23 deletions.
63 changes: 46 additions & 17 deletions js/classes/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ class Translator
*
* @type {number}
*/
requests = 10;
concurrentRequests = 10;

/**
*
* @type {number}
* @private
*/
_currentRequests = 0;

/**
*
Expand Down Expand Up @@ -71,35 +78,55 @@ class Translator

/**
*
* @param {[TranslatorLine]} lines
* @param {TranslatorLine[]} lines
* @returns {Promise<void>}
*/
async run(lines)
{
let i = 0;
let promises;

[i, promises] = this._batchTranslate(i, lines);

const next = () => {
let next_promises;
[i, next_promises] = this._batchTranslate(i, lines);
const p = Promise.race(next_promises);

return i < lines.length ? p.then(next) : p;
};

return await Promise.race(promises).then(next);
}

/**
*
* @param {number} i
* @param {TranslatorLine[]} lines
* @returns [{number}, {Promise<void>[]]
* @private
*/
_batchTranslate(i, lines)
{
/**
*
* @type {Promise<void>[]}
*/
const promises = [];
let i = 0;
for (i = 0; i < lines.length; i++)
for (; i < lines.length; i++)
{
if (i === this.requests)
if (this._currentRequests === this.concurrentRequests)
{
break;
}

promises.push(this._translate(lines[i]));
if (lines[i].needTranslate)
{
promises.push(this._translate(lines[i]));
}
}

const next = () => {
const p = this._translate(lines[i]);
i += 1;

return i < lines.length ? p.then(next) : p;
};

return await Promise.race(promises).then(next);
return [i, promises];
}

/**
Expand All @@ -110,9 +137,11 @@ class Translator
*/
async _translate(line)
{
line.translation = line.needTranslate
? await this._request(line.original)
: line.original;
this._currentRequests += 1;

line.translation = await this._request(line.original);

this._currentRequests -= 1;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/classes/TranslatorLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TranslatorLine
{
this.element = element;
this.original = element.innerHTML.trim() === '<br>' ? '.' : element.innerText.trim();
this._translation = '';
this._translation = this.needTranslate ? '' : this.original;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/classes/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class UI
{
this._config = config;
this.translator.port = config.port;
this.translator.requests = config.requests;
this.translator.concurrentRequests = config.requests;
this.updateView();
}

Expand Down
9 changes: 6 additions & 3 deletions js/classes/proxy/ProxySyosetu.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ProxySyosetu extends Proxy
{
super();

this.content_selectors = ['#novel_honbun', ];
this.content_selectors = ['#novel_p', '#novel_honbun', '#novel_a', ];
}

/**
Expand All @@ -28,10 +28,13 @@ class ProxySyosetu extends Proxy
this.addLine(document.querySelector('#novel_contents p.novel_title'));
this.addLine(document.querySelector('#novel_contents p.novel_subtitle'));

const before = document.querySelectorAll('#novel_p > p');
before.forEach(e => this.addLine(e));

const content = document.querySelectorAll('#novel_honbun > p');
content.forEach(e => this.addLine(e));

const notes = document.querySelectorAll('#novel_a > p');
notes.forEach(e => this.addLine(e));
const after = document.querySelectorAll('#novel_a > p');
after.forEach(e => this.addLine(e));
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Sugoi Web Translation",
"version": "1.0.0",
"version": "1.0.1",

"manifest_version": 3,
"incognito": "split",
Expand Down

0 comments on commit bea9b84

Please sign in to comment.