From d85831cc9aeb5fbf38293b5e931a457592e20a7d Mon Sep 17 00:00:00 2001 From: Gabe Yuan Date: Wed, 3 Jan 2024 10:10:54 +0800 Subject: [PATCH] fix: keep the translated image size unchanged --- src/libs/translator.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libs/translator.js b/src/libs/translator.js index 6725f01..6e2903a 100644 --- a/src/libs/translator.js +++ b/src/libs/translator.js @@ -360,7 +360,7 @@ export class Translator { } }, 500); - _matchLength = (q) => + _invalidLength = (q) => !q || q.length < (this._setting.minLength ?? TRANS_MIN_LENGTH) || q.length > (this._setting.maxLength ?? TRANS_MAX_LENGTH); @@ -388,7 +388,7 @@ export class Translator { this._tranNodes.set(el, q); // 太长或太短 - if (this._matchLength(q)) { + if (this._invalidLength(q)) { return; } @@ -400,6 +400,10 @@ export class Translator { let text = ""; el.childNodes.forEach((child) => { if (child.nodeType === 1 && child.matches(keepSelector)) { + if (child.nodeName === "IMG") { + child.style.cssText += `width: ${child.width}px;`; + child.style.cssText += `height: ${child.height}px;`; + } text += `#${keeps.length}#`; keeps.push(child.outerHTML); } else { @@ -408,7 +412,7 @@ export class Translator { }); // 太长或太短 - if (this._matchLength(text.replace(/#(\d+)#/g, "").trim())) { + if (this._invalidLength(text.replace(/#(\d+)#/g, "").trim())) { return; }