Skip to content

Commit 5df777b

Browse files
committed
More notification
1 parent 18c3b32 commit 5df777b

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

sources/anki.popclipext/anki.sh

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
# Created by cdpath on 2018/4/19.
44
# Copyright © 2018 cdpath. All rights reserved.
55

6-
set -xeuo pipefail
6+
#set -xeuo pipefail
77

88

99
## PopClip Env
10-
entry=$POPCLIP_TEXT
11-
safe_entry=$POPCLIP_URLENCODED_TEXT
12-
dict_svc=$POPCLIP_OPTION_DICT_SVC
13-
target_deck=$POPCLIP_OPTION_TARGET_DECK
14-
note_type=$POPCLIP_OPTION_NOTE_TYPE
15-
front_field=$POPCLIP_OPTION_FRONT_FIELD
16-
back_field=$POPCLIP_OPTION_BACK_FIELD
17-
tag=$POPCLIP_OPTION_TAG
10+
entry=${POPCLIP_TEXT:-debug}
11+
safe_entry=${POPCLIP_URLENCODED_TEXT:-debug}
12+
dict_service=${POPCLIP_OPTION_DICT_SVC:-shanbay}
13+
target_deck=${POPCLIP_OPTION_TARGET_DECK:-Default}
14+
note_type=${POPCLIP_OPTION_NOTE_TYPE:-Basic}
15+
front_field=${POPCLIP_OPTION_FRONT_FIELD:-Front}
16+
back_field=${POPCLIP_OPTION_BACK_FIELD:-Back}
17+
tag=${POPCLIP_OPTION_TAG:-debug}
1818
app_tag=${POPCLIP_APP_NAME// /_} # replace spaces with underscore
1919

2020

@@ -31,6 +31,7 @@ dialog() {
3131
## Dictionary Services
3232
_shanbay()
3333
{
34+
local safe_entry=$1
3435
url="https://api.shanbay.com/bdc/search/?word=$safe_entry"
3536
local definition=$(curl -sSL $url | perl -pe 's/^.*?(?<="definition":)(.*?[^\\]")(?=\,).*?$/$1/' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/^"//' -e 's/"$//')
3637
if [[ $definition = *'"status_code": 1'* ]]; then
@@ -42,6 +43,7 @@ _shanbay()
4243

4344
_youdao()
4445
{
46+
local safe_entry=$1
4547
url="http://dict.youdao.com/m/search?q=$safe_entry"
4648
curl -sSL $url \
4749
| sed -ne '/网络释义/,/更多释义/p' \
@@ -52,20 +54,21 @@ _youdao()
5254

5355
look_up()
5456
{
55-
local definition=''
56-
if [ "$dict_svc" = "shanbay" ]
57+
local safe_entry=$1
58+
if [ "$dict_service" = "shanbay" ]
5759
then
58-
definition=$(_shanbay)
59-
elif [ "$dict_svc" = "youdao" ]
60+
definition=$(_shanbay $safe_entry)
61+
elif [ "$dict_service" = "youdao" ]
6062
then
61-
definition=$(_youdao)
63+
definition=$(_youdao $safe_entry)
6264
else
65+
definition=''
6366
echo "Not Implemented"
6467
exit 1
6568
fi
6669

6770
if [[ -z "$definition" ]]; then
68-
dialog "$dict_svc" "未找到单词" 3
71+
dialog "$dict_service" "未找到单词" 3
6972
exit 1
7073
else
7174
echo $definition
@@ -76,6 +79,7 @@ look_up()
7679
## AnkiConnect
7780
gen_post_data()
7881
{
82+
local definition=$1
7983
cat <<EOF
8084
{
8185
"action": "addNote",
@@ -84,7 +88,7 @@ gen_post_data()
8488
"note": {
8589
"fields": {
8690
"$front_field": "$entry",
87-
"$back_field": "$(look_up)"
91+
"$back_field": "$definition"
8892
},
8993
"modelName": "$note_type",
9094
"deckName": "$target_deck",
@@ -100,25 +104,31 @@ EOF
100104

101105
check_result()
102106
{
103-
if [[ $1 != *'"error": null'* ]]; then
104-
if [[ $1 = "null" ]]; then
107+
local resp=$1
108+
local definition=$2
109+
if [[ $resp != *'"error": null'* ]]; then
110+
if [[ $resp = "null" ]]; then
105111
msg="Invalid post data for AnkiConnect"
106112
else
107-
msg=$(echo "$1" | perl -pe 's/^.*?(?<="error": ")(.*?[^\\])(?=[\."]).*?$/$1/' | sed -e 's/^"//' -e 's/"$//')
113+
msg=$(echo "$resp" | perl -pe 's/^.*?(?<="error": ")(.*?[^\\])(?=[\."]).*?$/$1/' | sed -e 's/^"//' -e 's/"$//')
108114
fi
109-
if [[ -z "$1" ]]; then
115+
if [[ -z "$resp" ]]; then
110116
msg="Did you open anki?"
111117
fi
112118
dialog "AnkiConnect" "$msg" 5
119+
else
120+
dialog "$entry" "Saved to $target_deck" 5
113121
fi
114122
}
115123

116124

117125
## main
118126
main()
119127
{
120-
local res=$(curl -X POST -d "$(gen_post_data)" "localhost:8765")
121-
check_result "$res"
128+
local definition=$(look_up $safe_entry)
129+
payload=$(gen_post_data "$definition")
130+
res=$(curl -sX POST -d "$payload" "localhost:8765")
131+
check_result "$res" "$definition"
122132
}
123133

124134

0 commit comments

Comments
 (0)