diff --git a/package.json b/package.json index 2b2f9de..d180804 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bobplugin-google-translate", - "version": "1.0.2", + "version": "1.1.0", "description": "Google 翻译插件,无需申请 API 秘钥", "homepage": "https://github.com/tingv/bobplugin-google-translate", "repository": "https://github.com/tingv/bobplugin-google-translate.git", diff --git a/src/info.json b/src/info.json index ef38e42..1cf6f16 100644 --- a/src/info.json +++ b/src/info.json @@ -1,7 +1,7 @@ { "identifier": "com.tingv.bobplugin.googletranslate", "category": "translate", - "version": "1.0.2", + "version": "1.1.0", "name": "Google 翻译", "summary": "Google 翻译插件,无需申请 API 秘钥", "author": "TingV", @@ -41,6 +41,22 @@ "value": "cn" } ] + }, + { + "identifier": "iBooksCleanup", + "type": "menu", + "title": "iBooks 清理", + "defaultValue": "disable", + "menuValues": [ + { + "title": "开启", + "value": "enable" + }, + { + "title": "关闭", + "value": "disable" + } + ] } ] } diff --git a/src/main.ts b/src/main.ts index a38c187..8a7d70c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,7 +15,7 @@ export function supportLanguages(): Bob.supportLanguages { export function translate(query: Bob.TranslateQuery, completion: Bob.Completion) { const { text = '', detectFrom, detectTo } = query; const str = formatString(text); - const params = { from: detectFrom, to: detectTo, cache: Bob.api.getOption('cache'), tld: Bob.api.getOption('tld'), }; + const params = { from: detectFrom, to: detectTo, cache: Bob.api.getOption('cache'), tld: Bob.api.getOption('tld'), iBooksCleanup: Bob.api.getOption('iBooksCleanup'), }; let res = _translate(str, params); res diff --git a/src/translate.ts b/src/translate.ts index c956d8e..eeb3f63 100644 --- a/src/translate.ts +++ b/src/translate.ts @@ -11,6 +11,7 @@ interface QueryOption { cache?: string; tld?: string; timeout?: number; + iBooksCleanup?: string; } var resultCache = new Bob.CacheResult('translate-result'); @@ -22,11 +23,19 @@ var resultCache = new Bob.CacheResult('translate-result'); * @return {object} 一个符合 bob 识别的翻译结果对象 */ async function _translate(text: string, options: QueryOption = {}): Promise { - const { from = 'auto', to = 'auto', cache = 'disable', tld = 'com', timeout = 10000 } = options; + const { from = 'auto', to = 'auto', cache = 'disable', tld = 'com', timeout = 10000, iBooksCleanup = 'disable' } = options; const sourceLanguage = standardToNoStandard(from); const targetLanguage = standardToNoStandard(to); + // 清理从 iBooks 中提取的内容 + if (iBooksCleanup === 'enable') { + const matchText = text.match(/^“([\s\S]*)”\n+摘录来自:/); + if (matchText && typeof matchText[1] !== "undefined") { + text = matchText[1]; + } + } + const cacheKey = CryptoJS.MD5(`${text}${from}${to}`); if (cache === 'enable') { const _cacheData = resultCache.get(cacheKey);