Skip to content

Commit 379745f

Browse files
committed
Merge branch 'release/v0.3'
2 parents 3000aa3 + f236e1f commit 379745f

File tree

12 files changed

+142
-146
lines changed

12 files changed

+142
-146
lines changed

.github/renovate.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

.github/workflows/ci.yaml

Lines changed: 0 additions & 22 deletions
This file was deleted.

.github/workflows/deploy-preview.yaml

Lines changed: 0 additions & 35 deletions
This file was deleted.

.github/workflows/deploy.yaml

Lines changed: 0 additions & 32 deletions
This file was deleted.

config/metadata.cjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
module.exports = {
1010
name: {
1111
$: "MetaTranslator",
12-
en: "MetaTranslator",
12+
fa: "مترجم متا",
1313
},
1414
namespace: "Violentmonkey Scripts",
1515
version: version,
@@ -27,6 +27,7 @@ module.exports = {
2727
"GM_unregisterMenuCommand",
2828
"GM_xmlhttpRequest",
2929
],
30+
icon:'https://www.google.com/s2/favicons?sz=64&domain=translate.google.com',
3031
require: [
3132
// 'https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js',
3233
// 'https://update.greasyfork.org/scripts/530526/1558038/ProgressUI-Module.js',
@@ -35,4 +36,6 @@ module.exports = {
3536
connect: [],
3637
"run-at": "document-end",
3738
"inject-into": "content",
39+
"downloadURL":"https://github.com/maanimis/MetaTranslator/releases/latest/download/index.prod.user.js",
40+
"updateURL":"https://github.com/maanimis/MetaTranslator/releases/latest/download/index.prod.user.js",
3841
};

dist/index.prod.user.js

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ==UserScript==
22
// @name MetaTranslator
3-
// @name:en MetaTranslator
3+
// @name:fa مترجم متا
44
// @namespace Violentmonkey Scripts
5-
// @version 0.2.7
5+
// @version 0.3
66
// @author maanimis <maanimis.dev@gmail.com>
77
// @source https://github.com/maanimis/MetaTranslator
88
// @license MIT
@@ -15,13 +15,79 @@
1515
// @grant GM_registerMenuCommand
1616
// @grant GM_unregisterMenuCommand
1717
// @grant GM_xmlhttpRequest
18+
// @icon https://www.google.com/s2/favicons?sz=64&domain=translate.google.com
1819
// @run-at document-end
1920
// @inject-into content
21+
// @downloadURL https://github.com/maanimis/MetaTranslator/releases/latest/download/index.prod.user.js
22+
// @updateURL https://github.com/maanimis/MetaTranslator/releases/latest/download/index.prod.user.js
2023
// ==/UserScript==
2124

2225
/******/ (() => { // webpackBootstrap
2326
/******/ "use strict";
2427

28+
;// ./src/services/menu/menu.service.ts
29+
class MenuCommandRepository {
30+
commands = new Map();
31+
add(command) {
32+
this.commands.set(command.name, command);
33+
}
34+
remove(name) {
35+
const command = this.commands.get(name);
36+
if (command) {
37+
this.commands.delete(name);
38+
}
39+
return command;
40+
}
41+
get(name) {
42+
return this.commands.get(name);
43+
}
44+
getAll() {
45+
return Array.from(this.commands.values());
46+
}
47+
clear() {
48+
this.commands.clear();
49+
}
50+
has(name) {
51+
return Boolean(this.commands.get(name));
52+
}
53+
}
54+
class MenuCommandService {
55+
_repository;
56+
constructor(_repository) {
57+
this._repository = _repository;
58+
}
59+
register(name, callback) {
60+
this.unregister(name);
61+
const id = GM_registerMenuCommand(name, callback);
62+
const command = { id, name, callback };
63+
this._repository.add(command);
64+
return command;
65+
}
66+
unregister(name) {
67+
const command = this._repository.remove(name);
68+
if (command) {
69+
GM_unregisterMenuCommand(command.id);
70+
}
71+
}
72+
unregisterAll() {
73+
this._repository.getAll().forEach((command) => {
74+
GM_unregisterMenuCommand(command.id);
75+
});
76+
this._repository.clear();
77+
}
78+
}
79+
const createMenuCommandManager = () => {
80+
const repository = new MenuCommandRepository();
81+
return new MenuCommandService(repository);
82+
};
83+
const menuCommandManager = createMenuCommandManager();
84+
const registerMenuCommand = (name, callback) => menuCommandManager.register(name, callback).id;
85+
const unregisterMenuCommand = (name) => menuCommandManager.unregister(name);
86+
87+
;// ./src/services/menu/index.ts
88+
89+
90+
2591
;// ./src/components/tooltip.component.ts
2692
class DOMTooltip {
2793
element;
@@ -352,6 +418,7 @@ class BrowserSelectionService {
352418

353419

354420

421+
355422
class TranslationHandler {
356423
tooltip;
357424
translator;
@@ -399,11 +466,15 @@ class TranslationHandler {
399466
}
400467
}
401468
registerLanguageMenu() {
402-
GM_registerMenuCommand("Set Target Language", () => {
469+
registerMenuCommand("Set Target Language", () => {
403470
const currentLang = this.languageStorage.getTargetLanguage();
404471
const input = prompt("Enter target language (fa,en,fr,de,...):", currentLang);
405472
if (input) {
406473
this.languageStorage.setTargetLanguage(input);
474+
ProgressUI.showQuick("[+]Refresh the page", {
475+
percent: 100,
476+
duration: 3000,
477+
});
407478
}
408479
});
409480
}
@@ -421,8 +492,10 @@ function initApiTranslation() {
421492

422493
;// ./src/index.ts
423494

495+
424496
async function main() {
425-
initApiTranslation();
497+
const translationHandler = initApiTranslation();
498+
registerMenuCommand("Translate Selected Text", () => translationHandler.onTextSelect());
426499
}
427500
main().catch((e) => {
428501
console.log(e);

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.7",
4+
"version": "0.3",
55
"author": {
66
"name": "maanimis",
77
"email": "maanimis.dev@gmail.com"

readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ This is a project help you build userscript with webpack
33

44
Just [use this git repo as a template](https://github.com/Trim21/webpack-userscript-template/generate).
55

6+
7+
8+
## Install
9+
10+
1. Install one of the **Violentmonkey** or **Tampermonkey** extensions (available for all browsers).
11+
2. Go to the [Releases](https://github.com/maanimis/MetaTranslator/releases) section, where you'll find a `.js` file.
12+
3. Click the file to install it, and you're all set!
13+
14+
### How to Use:
15+
- Simply select any text with your mouse, and it will be automatically translated.
16+
17+
618
## dependencies
719

820
There are two ways to using a package on npm.

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { registerMenuCommand } from "./services/menu";
12
import { initApiTranslation } from "./services/translators/apibots";
23

34
async function main() {
4-
initApiTranslation();
5+
const translationHandler = initApiTranslation();
6+
7+
registerMenuCommand("Translate Selected Text", () =>
8+
translationHandler.onTextSelect(),
9+
);
510
}
611

712
main().catch((e) => {

src/services/translators/apibots/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ISelectionService } from "./interfaces.apibots";
1212
import { LocalStorageLanguageService } from "../language-storage.service";
1313
import { BrowserSelectionService } from "../selection.service";
1414
import { sessionStorageService } from "../../storage";
15+
import { registerMenuCommand } from "../../menu";
1516

1617
class TranslationHandler {
1718
private readonly DEBOUNCE_DELAY = 300;
@@ -39,7 +40,7 @@ class TranslationHandler {
3940
this.registerLanguageMenu();
4041
}
4142

42-
private async onTextSelect(): Promise<void> {
43+
public async onTextSelect(): Promise<void> {
4344
const selectedText = this.selectionService.getSelectedText();
4445
if (!selectedText) return this.tooltip.hide();
4546

@@ -70,7 +71,7 @@ class TranslationHandler {
7071
}
7172

7273
private registerLanguageMenu(): void {
73-
GM_registerMenuCommand("Set Target Language", () => {
74+
registerMenuCommand("Set Target Language", () => {
7475
const currentLang = this.languageStorage.getTargetLanguage();
7576
const input = prompt(
7677
"Enter target language (fa,en,fr,de,...):",
@@ -79,7 +80,10 @@ class TranslationHandler {
7980

8081
if (input) {
8182
this.languageStorage.setTargetLanguage(input);
82-
// alert(`Target language set to "${input}"`);
83+
ProgressUI.showQuick("[+]Refresh the page", {
84+
percent: 100,
85+
duration: 3000,
86+
});
8387
}
8488
});
8589
}

0 commit comments

Comments
 (0)