Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update shanbay private api #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"storage",
"activeTab",
"tabs",
"notifications",
"http://dict.youdao.com/",
"https://api.shanbay.com/"
]
Expand Down
28 changes: 28 additions & 0 deletions build.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCinep+LvPP9QVz
cFTrWmSGIvEG9YId8DyYJJudIKtpZXtt7vAum7etFtJaVoEyC/ucVLvcvtDPBI+9
ixgL1iMuITfBY8uk6UpjghkT0kl5HR+oa6U8aNMr1MFdcMXIwd753s9ci6a/2cjh
Apvp82Wpkrhs+N/d/GbA5dyoawYZIXXgqBKKIP323PFjezmW1NGcwNoFY7GQeAqs
M1VGAsiHzVoauWwPgcl+USvLfFJXd0m+zw00BRPbbmFEY+bivVvxI0Lapi8iO453
L+WONScwQCrJvZlILD+aoxlEX9lS6ZJtmek30o0v/lxICBBqVG2BkzpOMuL3n6G+
yDGPs9aDAgMBAAECggEAMSHXTKKxBZ75M6S385gPETwTaM/arWL9W9aLsdDCGYkQ
3uk3Zk2kFEb17Vd/l3hpYBV14T9B5ZpyN4mEDWd4n6JnryWQzNWzpRcUrpl1m4NC
ImfLt9HQI8kXVz0QdRSmYwZ/zm1fR03s1JcuNmRUzgRHdp1HPlHZVeTdY4FaiVLI
cq4nMAayY14oXL8CmEjSQIP+cNUvhYrS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 50 additions & 19 deletions src/components/add_word.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
import icons from './icons'
import { addNotebookWord } from '../message'

const SHANBAY_URL = 'https://www.shanbay.com/bdc/learnings/library/'
const SHANBAY_URL = 'https://web.shanbay.com/wordsweb/#/collection'

const styles = {
container: {
Expand All @@ -24,18 +24,33 @@ const styles = {
width: 8,
height: 8,
},
check: {
position: 'absolute',
left: 8,
bottom: 0,
width: 10,
height: 10,
},
invalid: {
position: 'absolute',
left: 8,
bottom: 0,
width: 10,
height: 10,
},
}

class AddWord extends Component {
constructor(props) {
super(props)

const { defaultAdded } = props
const { defaultAdded, defaultInvalid } = props

this.addWord = this.addWord.bind(this)

this.state = {
added: defaultAdded,
invalid: defaultInvalid
}
}

Expand All @@ -45,17 +60,17 @@ class AddWord extends Component {

addWord() {
const { word } = this.props

addNotebookWord(word).then((response) => {
const { success, msg } = response

if (!success) {
throw new Error(msg)

if (!!success) {
this.setState({ added: true, })
} else if(msg == 'Invalid Token!') {
this.setState({ invalid: true, added: false })
} else {
throw new Error(msg);
}

this.setState({
added: true,
})
}).catch((err) => {
this.props.flash(err.message)
})
Expand All @@ -64,32 +79,46 @@ class AddWord extends Component {
render() {
const { addWord } = this
const { word, showWordsPage } = this.props
const { added } = this.state
const { added, invalid } = this.state

if (!word || !showWordsPage) {
return null
}

const content = (
<div style={{ display: 'inline-block' }}>
<img
src={icons.book}
style={styles.book}
alt="book"
/>
{!added ? (
<div style={{ display: 'inline-block' } }>

{ !!invalid ? (
<img
src={icons.invalid}
style={styles.invalid}
alt="invalid"
/>
) : !added? (
<img
src={icons.plus}
style={styles.plus}
alt="plus"
/>
) : null}
) : (
<img
src={icons.check}
style={styles.check}
alt="check"
/>
) }

<img
src={icons.book}
style={styles.book}
alt="book"
/>
</div>
)

return added ? (
<a
title="点击加入扇贝词库"
title="单词本中查看"
style={styles.container}
href={SHANBAY_URL}
target="_blank"
Expand All @@ -114,12 +143,14 @@ AddWord.propTypes = {
showWordsPage: bool.isRequired,
word: string,
defaultAdded: bool,
defaultInvalid: bool,
flash: func.isRequired,
}

AddWord.defaultProps = {
word: '',
defaultAdded: false,
defaultInvalid: false,
}

export default AddWord
2 changes: 1 addition & 1 deletion src/components/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import icons from './icons'
const { string } = PropTypes

function getAudioURL(word, type = 1) {
return `http://dict.youdao.com/dictvoice?audio=${word}&type=${type}`
return `https://dict.youdao.com/dictvoice?audio=${word}&type=${type}`
}

class Audio extends Component {
Expand Down
2 changes: 2 additions & 0 deletions src/components/icons.js

Large diffs are not rendered by default.

104 changes: 17 additions & 87 deletions src/event_page.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { EVENTS, onMessage } from './message'
import { parse } from './parse'
import { getWordURL, have } from './words'
import ShanbayOauth from './lib/shanbay_oauth2'
import { getWordURL, have } from './words'
import { lookUp, addWord, notify } from './shanbay_api'

const { CLEAR_SHANBAY_TOKEN, SEARCH_WORD, OPEN_NEW_TAB, ADD_WORD_SHANBAY } = EVENTS
let oauth = null

async function getWordExplain(body) {
const explain = parse(body)
Expand All @@ -26,107 +25,38 @@ async function getWords(word, sendRes) {
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)
// get shanbay word id
response = await lookUp(data)
const { id } = response

// add word
addWord(id)

sendRes({ success: true })
} catch (err) {
// 如果token失效,清除token并重新添加
if (err.message === 'Invalid token') {
clearShanbayToken()
await addWordToShanbay(data, sendRes)
if (err.msg == "单词没找到") {
sendRes({ success: false, msg: 'Shabay: Word Not Found!' })
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,
sendRes({ success: false, msg: 'Invalid Token!' })
notify({
title: '扇贝认证失败!',
message: '登录后才能使用生词本功能。\n点击此消息前往扇贝登录',
url: 'https://web.shanbay.com/web/account/login/'
})

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)
})
Expand Down
Loading