Skip to content

Commit 3a2889a

Browse files
committed
Merge branch 'release/v0.2.6'
2 parents f60b468 + 125a4ef commit 3a2889a

15 files changed

Lines changed: 224 additions & 232 deletions

LICENSE

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2-
Version 2, December 2004
1+
MIT License
32

4-
Copyright (C) 2020-2022 Trim21 <trim21.me@gmail.com>
3+
Copyright (c) 2025 maanimis
54

6-
Everyone is permitted to copy and distribute verbatim or modified
7-
copies of this license document, and changing it is allowed as long
8-
as the name is changed.
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
911

10-
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11-
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
1214

13-
0. You just DO WHAT THE FUCK YOU WANT TO.
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

config/metadata.cjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ module.exports = {
2626
"GM_xmlhttpRequest",
2727
],
2828
require: [
29-
'https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js',
30-
'https://update.greasyfork.org/scripts/530526/1558038/ProgressUI-Module.js',
31-
'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js'
29+
// 'https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js',
30+
// 'https://update.greasyfork.org/scripts/530526/1558038/ProgressUI-Module.js',
31+
// 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js'
3232
],
3333
connect: [],
3434
"run-at": "document-end",
35+
"inject-into": "content",
3536
};

dist/index.prod.user.js

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
// ==UserScript==
2-
// @name MetaTranslator
3-
// @name:en MetaTranslator
4-
// @namespace Violentmonkey Scripts
5-
// @version 0.1
6-
// @author maanimis <maanimis.dev@gmail.com>
7-
// @source https://github.com/maanimis/MetaTranslator
8-
// @license MIT
9-
// @match *://*/*
10-
// @grant GM_setValue
11-
// @grant GM_getValue
12-
// @grant GM_deleteValue
13-
// @grant GM_addValueChangeListener
14-
// @grant GM_registerMenuCommand
15-
// @grant GM_unregisterMenuCommand
16-
// @grant GM_xmlhttpRequest
17-
// @require https://update.greasyfork.org/scripts/530648/1558616/FileDownloader-Module.js
18-
// @require https://update.greasyfork.org/scripts/530526/1558038/ProgressUI-Module.js
19-
// @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js
20-
// @run-at document-end
2+
// @name MetaTranslator
3+
// @name:en MetaTranslator
4+
// @namespace Violentmonkey Scripts
5+
// @version 0.2.6
6+
// @author maanimis <maanimis.dev@gmail.com>
7+
// @source https://github.com/maanimis/MetaTranslator
8+
// @license MIT
9+
// @match *://*/*
10+
// @grant GM_setValue
11+
// @grant GM_getValue
12+
// @grant GM_deleteValue
13+
// @grant GM_addValueChangeListener
14+
// @grant GM_registerMenuCommand
15+
// @grant GM_unregisterMenuCommand
16+
// @grant GM_xmlhttpRequest
17+
// @run-at document-end
18+
// @inject-into content
2119
// ==/UserScript==
2220

2321
/******/ (() => { // webpackBootstrap
@@ -235,8 +233,32 @@ class GoogleTranslator {
235233
}
236234
}
237235

236+
;// ./src/services/storage/cache.storage.ts
237+
class SessionStorageService {
238+
get(key, defaultValue) {
239+
const item = sessionStorage.getItem(key);
240+
return item !== null ? JSON.parse(item) : defaultValue;
241+
}
242+
set(key, value) {
243+
sessionStorage.setItem(key, JSON.stringify(value));
244+
}
245+
remove(key) {
246+
sessionStorage.removeItem(key);
247+
}
248+
onChange(key, callback) {
249+
const storageHandler = (event) => {
250+
if (event.storageArea === sessionStorage && event.key === key) {
251+
const newValue = event.newValue ? JSON.parse(event.newValue) : null;
252+
const oldValue = event.oldValue ? JSON.parse(event.oldValue) : null;
253+
callback(newValue, oldValue);
254+
}
255+
};
256+
window.addEventListener("storage", storageHandler);
257+
}
258+
}
259+
238260
;// ./src/services/storage/storage.service.ts
239-
class GM_StorageService {
261+
class GMStorageService {
240262
get(key, defaultValue) {
241263
return GM_getValue(key, defaultValue);
242264
}
@@ -250,7 +272,40 @@ class GM_StorageService {
250272
GM_addValueChangeListener(key, callback);
251273
}
252274
}
253-
const GMStorageService = new GM_StorageService();
275+
276+
;// ./src/services/storage/handler.storage.ts
277+
278+
279+
class StorageHandler {
280+
sessionStorageService;
281+
gmStorageService;
282+
constructor(sessionStorageService, gmStorageService) {
283+
this.sessionStorageService = sessionStorageService;
284+
this.gmStorageService = gmStorageService;
285+
}
286+
get(key, defaultValue) {
287+
const sessionValue = this.sessionStorageService.get(key, defaultValue);
288+
if (sessionValue !== undefined && sessionValue !== null) {
289+
return sessionValue;
290+
}
291+
return this.gmStorageService.get(key, defaultValue);
292+
}
293+
set(key, value) {
294+
this.sessionStorageService.set(key, value);
295+
this.gmStorageService.set(key, value);
296+
}
297+
remove(key) {
298+
this.sessionStorageService.remove(key);
299+
this.gmStorageService.remove(key);
300+
}
301+
onChange(key, callback) {
302+
this.sessionStorageService.onChange(key, callback);
303+
this.gmStorageService.onChange(key, callback);
304+
}
305+
}
306+
const sessionStorageService = new SessionStorageService();
307+
const gmStorageService = new GMStorageService();
308+
const storageHandler = new StorageHandler(sessionStorageService, gmStorageService);
254309

255310
;// ./src/services/storage/index.ts
256311

@@ -261,10 +316,10 @@ const GMStorageService = new GM_StorageService();
261316

262317
class LocalStorageLanguageService {
263318
getTargetLanguage() {
264-
return GMStorageService.get("targetLang", "fa");
319+
return storageHandler.get("targetLang", "fa");
265320
}
266321
setTargetLanguage(lang) {
267-
GM_setValue("targetLang", lang);
322+
storageHandler.set("targetLang", lang);
268323
}
269324
}
270325

@@ -295,6 +350,7 @@ class BrowserSelectionService {
295350

296351

297352

353+
298354
class TranslationHandler {
299355
tooltip;
300356
translator;
@@ -325,11 +381,15 @@ class TranslationHandler {
325381
const position = this.selectionService.getSelectionPosition();
326382
if (!position)
327383
return;
384+
const cacheResult = sessionStorageService.get(selectedText, null);
385+
if (cacheResult) {
386+
this.tooltip.show(cacheResult, position.x, position.y);
387+
return;
388+
}
328389
try {
329390
const translationResult = await this.translator.translate(selectedText);
330-
if (selectedText === translationResult.translation)
331-
return;
332391
const formattedText = this.formatter.format(translationResult);
392+
sessionStorageService.set(selectedText, formattedText);
333393
this.tooltip.show(formattedText, position.x, position.y);
334394
}
335395
catch (error) {

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

readme.cn.md

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

readme.md

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,8 @@
1-
# This is a project help you build userscript with webpack
1+
# MetaTranslator
2+
This is a project help you build userscript with webpack
23

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

5-
[中文说明](./readme.cn.md)
6-
7-
## dev
8-
9-
1. Allow Tampermonkey's access to local file URIs [tampermonkey/faq](https://tampermonkey.net/faq.php?ext=dhdg#Q204)
10-
2. install deps with `npm i` or `npm ci`.
11-
3. `npm run dev` to start your development.
12-
13-
Now you will see 2 files in `./dist/`
14-
15-
- `dist/index.dev.user.js`: **You should install this userscript in your browser.** It's a simple loader that load `dist/index.debug.js` on matched web page.
16-
- `dist/index.debug.js`: This is the development build with `eval-source-map`. It will be automatically loaded by `dist/index.dev.user.js` via `@require file://.../dist/index.debug.js` metadata, **Don't add it to your userscript manager.**
17-
18-
4. edit [src/index.ts](./src/index.ts), you can even import css or less files. You can use scss if you like.
19-
5. go wo <https://www.example.com/> and open console, you'll see it's working.
20-
21-
livereload is default enabled, use [this Chrome extension](https://chrome.google.com/webstore/detail/jnihajbhpnppcggbcgedagnkighmdlei)
22-
23-
### NOTICE
24-
25-
Everytime you change your metadata config,
26-
you'll have to restart webpack server and install newly generated `dist/index.dev.user.js` UserScript in your browser again.
27-
28-
## used package
29-
30-
If you prefer some other bundler like rollup, you can use some of these packages directly.
31-
32-
[userscript-metadata-generator](https://github.com/trim21/userscript-metadata-generator)
33-
34-
[gm-fetch](https://github.com/trim21/gm-fetch)
35-
36-
[userscript-metadata-webpack-plugin](https://github.com/trim21/userscript-metadata-webpack-plugin)
37-
38-
## Cross Site Request
39-
40-
you can call `GM.xmlHttpRequest` directly or use a fetch API based on `GM.xmlHttpRequest` <https://github.com/Trim21/gm-fetch>
41-
42-
## TypeScript
43-
44-
use typescript as normal, see [example](src/index.ts)
45-
466
## dependencies
477

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

src/app.ts

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

0 commit comments

Comments
 (0)