From 148ab7052c4858fa79643cc385b311953bf11b1c Mon Sep 17 00:00:00 2001 From: yuhaowin Date: Sat, 12 Oct 2024 18:09:56 +0800 Subject: [PATCH] feat:release v0.2.0 --- release.sh | 2 +- src/info.json | 28 ++++++++--- src/main.js | 134 +++++++++++++++++++++++++++++++------------------- 3 files changed, 106 insertions(+), 58 deletions(-) diff --git a/release.sh b/release.sh index c98c251..7af7cb4 100644 --- a/release.sh +++ b/release.sh @@ -9,7 +9,7 @@ echo $sha256_wordbook download_link="https://github.com/yuhaowin/wordbook-bob-plugin/releases/download/v$version/wordbook-bob-plugin-v$version.bobplugin" -new_version="{\"version\": \"$version\", \"desc\": \"https://github.com/yuhaowin/wordbook-bob-plugin/releases/tag/v$version\", \"sha256\": \"$sha256_wordbook\", \"url\": \"$download_link\", \"minBobVersion\": \"0.5.4\"}" +new_version="{\"version\": \"$version\", \"desc\": \"https://github.com/yuhaowin/wordbook-bob-plugin/releases/tag/v$version\", \"sha256\": \"$sha256_wordbook\", \"url\": \"$download_link\", \"minBobVersion\": \"1.12.0\"}" json_file='appcast.json' json_data=$(cat $json_file) diff --git a/src/info.json b/src/info.json index a41b9df..1117c51 100644 --- a/src/info.json +++ b/src/info.json @@ -1,6 +1,6 @@ { "identifier": "wordbook-bob-plugin", - "version": "0.1.9", + "version": "0.2.0", "category": "translate", "name": "单词本插件", "summary": "word book plugin, add word to youdao/eudic/shanday word book", @@ -8,12 +8,12 @@ "author": "Eric_余浩", "homepage": "https://blog.yuhaowin.com/2021-01-03-wordbook-bob-plugin", "appcast": "https://raw.githubusercontent.com/yuhaowin/wordbook-bob-plugin/main/appcast.json", - "minBobVersion": "0.5.4", + "minBobVersion": "1.12.0", "options": [ { - "identifier": "selectDict", + "identifier": "dict_type", "type": "menu", - "title": "选择词典", + "title": "词典类型", "defaultValue": "1", "menuValues": [ { @@ -36,9 +36,25 @@ "title": "认证信息" }, { - "identifier": "wordbookId", + "identifier": "word_only", + "type": "menu", + "title": "仅保存单词", + "defaultValue": "1", + "menuValues": [ + { + "title": "是", + "value": "1" + }, + { + "title": "否", + "value": "0" + } + ] + }, + { + "identifier": "wordbook_id", "type": "text", - "title": "欧路单词本ID" + "title": "欧路单词本 id" } ] } diff --git a/src/main.js b/src/main.js index 635fdbb..7b15827 100644 --- a/src/main.js +++ b/src/main.js @@ -36,22 +36,79 @@ function buildError(res) { // override function supportLanguages() { - return ['auto', 'zh-Hans', 'en']; + return ['zh-Hans', 'en']; +} + +// override +function pluginValidate(completion) { + var authorization = $option.authorization; + var dict_type = $option.dict_type; + var wordbook_id = $option.wordbook_id + if (!authorization) { + completion({ + result: false, + error: { + type: "secretKey", + message: "未设置认证信息。", + troubleshootingLink: "https://github.com/yuhaowin/wordbook-bob-plugin" + } + }); + return; + } + if (dict_type == 2 && !wordbook_id) { + queryEudicWordbookIds(authorization, completion) + return + } + completion({result: true}); +} + +function queryEudicWordbookIds(token, completion) { + $http.get({ + url: EUDIC_BOOK_LIST_URL, + header: { + 'Authorization': token, + 'Content-Type': 'application/json', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' + }, + handler: function (res) { + var statusCode = res.response.statusCode; + if (statusCode === 200) { + var data = res.data.data; + completion({ + result: false, + error: { + type: "param", + message: "请选择欧路词典单词本 id : \r\n" + JSON.stringify(data, null, 4) + } + }); + } else { + completion({ + result: false, + error: { + type: "param", + message: "欧路词典 token 错误或过期,请重新填写。", + troubleshootingLink: "https://github.com/yuhaowin/wordbook-bob-plugin" + } + }); + $log.info('接口返回值 data : ' + JSON.stringify(data)); + } + } + }); } // override function translate(query, completion) { var text = query.text; var fromLanguage = query.detectFrom; - var selectDict = $option.selectDict; + var selectDict = $option.dict_type; + var word_only = $option.word_only; var authorization = $option.authorization; - EUDIC_WORD_BOOK_ID = $option.wordbookId; - - if (fromLanguage != 'en' || text.search(' ') > 0) { + EUDIC_WORD_BOOK_ID = $option.wordbook_id; + var need_save = (word_only == 0 || text.search(' ') < 1); + if (fromLanguage != 'en' || !need_save) { completion({'result': buildResult("中文、非英语单词无需添加单词本")}); return; } - if (authorization) { addWord(selectDict, authorization, text, completion); } else { @@ -64,46 +121,13 @@ function addWord(selectDict, authorization, word, completion) { addWordYoudao(authorization, word, completion); } if (selectDict == 2) { // 保存欧路单词本 - if (EUDIC_WORD_BOOK_ID) { - addWordEudic(authorization, word, completion); - } else { - queryEudicWordbookIds(authorization, completion) - } + addWordEudic(authorization, word, completion); } if (selectDict == 3) { // 保存扇贝单词本 addWordShanbay(authorization, word, completion); } } -function addWordEudic(token, word, completion) { - $http.post({ - url: EUDIC_ADD_WORD_URL, - header: { - 'Authorization': token, - 'Content-Type': 'application/json', - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' - }, - body: { - "id": EUDIC_WORD_BOOK_ID, // 单词本 id - "language": "en", - "words": [ - word - ] - }, - handler: function (res) { - var data = res.data; - var response = res.response; - var statusCode = response.statusCode; - if (statusCode === 201) { - completion({'result': buildResult("添加单词成功")}); - } else { - completion({'error': buildError('token 已经过期,请重新获取。')}); - $log.info('addWord 接口返回值 data : ' + JSON.stringify(data)); - } - } - }); -} - function addWordYoudao(cookie, word, completion) { $http.get({ url: YOUDAO_ADD_WORD_URL + encodeURIComponent(word), @@ -122,29 +146,37 @@ function addWordYoudao(cookie, word, completion) { if (data.code === 0) { completion({'result': buildResult("添加单词成功")}); } else { - completion({'error': buildError('cookie 已经过期,请重新获取。')}); + completion({'error': buildError('有道词典 cookie 错误或过期,请重新填写。')}); $log.info('addWord 接口返回值 data : ' + JSON.stringify(data)); } } }); } -function queryEudicWordbookIds(token, completion) { - $http.get({ - url: EUDIC_BOOK_LIST_URL, +function addWordEudic(token, word, completion) { + $http.post({ + url: EUDIC_ADD_WORD_URL, header: { 'Authorization': token, 'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' }, + body: { + "id": EUDIC_WORD_BOOK_ID, // 单词本 id + "language": "en", + "words": [ + word + ] + }, handler: function (res) { - var statusCode = res.response.statusCode; - if (statusCode === 200) { - var data = res.data.data; - completion({'result': buildResult("单词本列表:\r\n" + JSON.stringify(data, null, 4))}); + var data = res.data; + var response = res.response; + var statusCode = response.statusCode; + if (statusCode === 201) { + completion({'result': buildResult("添加单词成功")}); } else { - completion({'error': buildError('token 已经过期,请重新获取。')}); - $log.info('接口返回值 data : ' + JSON.stringify(data)); + completion({'error': buildError('欧路词典 token 错误或过期,请重新填写。')}); + $log.info('addWord 接口返回值 data : ' + JSON.stringify(data)); } } }); @@ -168,7 +200,7 @@ function addWordShanbay(token, word, completion) { if (res.response.statusCode === 200) { completion({'result': buildResult("添加单词本成功")}); } else { - completion({'error': buildError('添加单词失败,请检查 auth_token 是否已经过期。')}); + completion({'error': buildError('扇贝词典 auth_token 错误或过期,请重新填写。')}); $log.info('接口返回值 data : ' + JSON.stringify(data)); } }