Skip to content

Commit

Permalink
feat:release v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhaowin committed Oct 12, 2024
1 parent c7e4046 commit 148ab70
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 58 deletions.
2 changes: 1 addition & 1 deletion release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 22 additions & 6 deletions src/info.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"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",
"icon": "",
"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": [
{
Expand All @@ -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"
}
]
}
134 changes: 83 additions & 51 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand All @@ -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));
}
}
});
Expand All @@ -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));
}
}
Expand Down

0 comments on commit 148ab70

Please sign in to comment.