-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathevent_page.js
147 lines (114 loc) · 2.83 KB
/
event_page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { EVENTS, onMessage } from './message'
import { parse } from './parse'
import { getWordURL, have } from './words'
import ShanbayOauth from './lib/shanbay_oauth2'
const { CLEAR_SHANBAY_TOKEN, SEARCH_WORD, OPEN_NEW_TAB, ADD_WORD_SHANBAY } = EVENTS
let oauth = null
async function getWordExplain(body) {
const explain = parse(body)
if (explain && explain.wordInfo && explain.wordInfo.word) {
const word = explain.wordInfo.word
const added = await have(word)
explain.added = added
}
return explain
}
async function getWords(word, sendRes) {
const url = getWordURL(word)
const body = await fetch(url).then(res => res.text())
const explain = await getWordExplain(body)
sendRes(explain)
}
function authorize() {
return new Promise((resolve) => {
oauth.authorize(() => {
resolve()
})
})
}
const ADD_WORD_URL = 'https://api.shanbay.com/bdc/learning/'
const SEARCH_WORD_URL = 'https://api.shanbay.com/bdc/search/'
function getWord(url) {
return fetch(url).then(res => res.json()).then((res) => {
const { data, msg } = res
if (msg !== 'SUCCESS') {
throw new Error(msg)
}
return data
})
}
function clearShanbayToken() {
oauth.clearToken()
}
async function addWordToShanbay(data, sendRes) {
// redirect
if (!oauth.token_valid()) {
await authorize()
}
const token = oauth.access_token()
const searchURL = `${SEARCH_WORD_URL}?word=${data}&access_token=${token}`
let response = null
try {
response = await getWord(searchURL)
} catch (err) {
// 如果token失效,清除token并重新添加
if (err.message === 'Invalid token') {
clearShanbayToken()
await addWordToShanbay(data, sendRes)
return
}
sendRes({
success: false,
msg: err.message,
})
return
}
const { id } = response
const addWordURL = `${ADD_WORD_URL}?access_token=${token}`
try {
response = await fetch(addWordURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id,
access_token: token,
}),
}).then(res => res.json()).then((res) => {
const { data: d, msg } = res
if (msg !== 'SUCCESS') {
throw new Error(msg)
}
return d
})
} catch (err) {
sendRes({
success: false,
msg: err.message,
})
return
}
sendRes({
success: true,
})
}
function openNewTab(word) {
chrome.tabs.create({ url: getWordURL(word) })
}
function init() {
oauth = ShanbayOauth.initPage()
onMessage(SEARCH_WORD, (data, sendRes) => {
getWords(data, sendRes)
})
onMessage(OPEN_NEW_TAB, (data) => {
openNewTab(data)
})
onMessage(ADD_WORD_SHANBAY, (data, sendRes) => {
addWordToShanbay(data, sendRes)
})
onMessage(CLEAR_SHANBAY_TOKEN, () => {
clearShanbayToken()
})
}
init()