Skip to content

Commit 3000aa3

Browse files
committed
Merge branch 'release/v0.2.7'
2 parents 3a2889a + 9e092cc commit 3000aa3

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

config/metadata.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
dependencies,
44
repository,
55
version,
6+
description
67
} = require("../package.json");
78

89
module.exports = {
@@ -16,6 +17,7 @@ module.exports = {
1617
source: repository.url,
1718
'license': 'MIT',
1819
match: ["*://*/*"],
20+
description,
1921
grant: [
2022
"GM_setValue",
2123
"GM_getValue",

dist/index.prod.user.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// @name MetaTranslator
33
// @name:en MetaTranslator
44
// @namespace Violentmonkey Scripts
5-
// @version 0.2.6
5+
// @version 0.2.7
66
// @author maanimis <maanimis.dev@gmail.com>
77
// @source https://github.com/maanimis/MetaTranslator
88
// @license MIT
99
// @match *://*/*
10+
// @description Show translated tooltip on text selection
1011
// @grant GM_setValue
1112
// @grant GM_getValue
1213
// @grant GM_deleteValue
@@ -374,20 +375,21 @@ class TranslationHandler {
374375
}
375376
async onTextSelect() {
376377
const selectedText = this.selectionService.getSelectedText();
377-
if (!selectedText) {
378-
this.tooltip.hide();
379-
return;
380-
}
378+
if (!selectedText)
379+
return this.tooltip.hide();
381380
const position = this.selectionService.getSelectionPosition();
382381
if (!position)
383382
return;
384-
const cacheResult = sessionStorageService.get(selectedText, null);
385-
if (cacheResult) {
386-
this.tooltip.show(cacheResult, position.x, position.y);
387-
return;
388-
}
383+
const cachedResult = sessionStorageService.get(selectedText, null);
384+
if (cachedResult)
385+
return this.tooltip.show(cachedResult, position.x, position.y);
386+
await this.fetchAndShowTranslation(selectedText, position);
387+
}
388+
async fetchAndShowTranslation(selectedText, position) {
389389
try {
390390
const translationResult = await this.translator.translate(selectedText);
391+
if (translationResult.translation === selectedText)
392+
return;
391393
const formattedText = this.formatter.format(translationResult);
392394
sessionStorageService.set(selectedText, formattedText);
393395
this.tooltip.show(formattedText, position.x, position.y);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "metatranslator",
33
"description": "Show translated tooltip on text selection",
4-
"version": "0.2.6",
4+
"version": "0.2.7",
55
"author": {
66
"name": "maanimis",
77
"email": "maanimis.dev@gmail.com"

src/services/translators/apibots/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,25 @@ class TranslationHandler {
4141

4242
private async onTextSelect(): Promise<void> {
4343
const selectedText = this.selectionService.getSelectedText();
44-
if (!selectedText) {
45-
this.tooltip.hide();
46-
return;
47-
}
44+
if (!selectedText) return this.tooltip.hide();
4845

4946
const position = this.selectionService.getSelectionPosition();
5047
if (!position) return;
5148

52-
const cacheResult = sessionStorageService.get(selectedText, null);
53-
if (cacheResult) {
54-
this.tooltip.show(cacheResult, position.x, position.y);
55-
return;
56-
}
49+
const cachedResult = sessionStorageService.get(selectedText, null);
50+
if (cachedResult)
51+
return this.tooltip.show(cachedResult, position.x, position.y);
52+
53+
await this.fetchAndShowTranslation(selectedText, position);
54+
}
5755

56+
private async fetchAndShowTranslation(
57+
selectedText: string,
58+
position: { x: number; y: number },
59+
): Promise<void> {
5860
try {
5961
const translationResult = await this.translator.translate(selectedText);
62+
if (translationResult.translation === selectedText) return;
6063

6164
const formattedText = this.formatter.format(translationResult);
6265
sessionStorageService.set(selectedText, formattedText);

0 commit comments

Comments
 (0)