Skip to content
This repository was archived by the owner on Feb 14, 2022. It is now read-only.

Commit 5a5a1b7

Browse files
authored
Merge pull request #7 from x86chi/async-without-callback
use axios for sync without callback
2 parents 06fd5e1 + b04164b commit 5a5a1b7

6 files changed

Lines changed: 37 additions & 47 deletions

File tree

3rdparty/axios.0.19.0.min.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"all_frames": true,
2828
"js": [
2929
"3rdparty/jquery.3.3.1.min.js",
30-
"3rdparty/bootstrap.3.2.0.min.js",
30+
"3rdparty/bootstrap.3.2.0.min.js",
3131
"content.js",
3232
"inject_stylesheet.js"
3333
]
@@ -36,4 +36,4 @@
3636
"browser_action": {
3737
"default_popup": "options_login.html"
3838
}
39-
}
39+
}

options_logged_info.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<h1 id="user_id">...</h1>
1313

1414
<div id="submit" class="button">로그아웃</div>
15-
</div>
15+
</div>
16+
<script src="3rdparty/axios.0.19.0.min.js"></script>
1617
<script src="options_logged_info.js"></script>
1718
</body>
18-
</html>
19+
</html>

options_logged_info.js

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const loggedInUser = document.getElementById('user_id')
22
const logoutButton = document.getElementById('submit')
3+
const URL = 'https://api.solved.ac/validate_token.php'
34

45
const logout = () => {
56
chrome.storage.local.remove('token', () => {
@@ -11,29 +12,13 @@ const logout = () => {
1112
})
1213
}
1314

14-
const validateToken = token => {
15-
const params = { token }
16-
const xhr = new XMLHttpRequest()
17-
xhr.open('POST', 'https://api.solved.ac/validate_token.php', true)
18-
xhr.setRequestHeader('Content-type', 'application/json')
19-
xhr.onload = ({ responseText }) => {
20-
console.log(responseText)
21-
if (!this.status === 200) {
22-
alert(JSON.parse(responseText).error)
23-
logout()
24-
return
25-
}
26-
const { user } = JSON.parse(responseText)
27-
loggedInUser.innerText = user.user_id
28-
}
29-
xhr.send(JSON.stringify(params))
15+
const validateToken = ({ token }) => {
16+
debugger
17+
axios.post(URL, { token }).then(({ data, status }) => {
18+
loggedInUser.innerText = data.user.user_id
19+
})
3020
}
3121

32-
chrome.storage.local.get(['token'], ({ token }) => {
33-
debugger
34-
if (token) {
35-
validateToken(token)
36-
}
37-
})
22+
chrome.storage.local.get(['token'], validateToken)
3823

3924
logoutButton.addEventListener('click', logout)

options_login.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ <h3 class="input_caption">비밀번호</h3>
1919

2020
<br>
2121
<div id="submit" class="button">로그인</div>
22-
</div>
22+
</div>
23+
<script src="3rdparty/axios.0.19.0.min.js"></script>
2324
<script src="options_login.js"></script>
2425
</body>
25-
</html>
26+
</html>

options_login.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,25 @@ chrome.storage.local.get(['token'], ({ token }) => {
66
const submitButton = document.getElementById('submit')
77
const userIdInput = document.getElementById('user_id')
88
const passwordInput = document.getElementById('password')
9+
const URL = 'https://api.solved.ac/request_token.php'
910

1011
const validate = () => {
1112
const { value: user_id } = userIdInput
1213
const { value: password } = passwordInput
13-
const params = { user_id, password }
14-
const xhr = new XMLHttpRequest()
15-
xhr.open('POST', 'https://api.solved.ac/request_token.php', true)
16-
xhr.setRequestHeader('Content-type', 'application/json')
17-
xhr.onload = ({ responseText, status }) => {
18-
console.log(responseText)
19-
if (!status === 200) {
20-
alert(JSON.parse(this.responseText).error)
21-
return
22-
}
23-
24-
chrome.storage.local.set({ token: JSON.parse(responseText).token }, () => {
25-
chrome.tabs.getSelected(null, tab => {
26-
const code = 'window.location.reload();'
27-
chrome.tabs.executeScript(tab.id, { code })
14+
axios
15+
.post(URL, { user_id, password })
16+
.then(({ data, status }) => {
17+
const { token } = data
18+
console.log(token)
19+
chrome.storage.local.set({ token }, () => {
20+
chrome.tabs.getSelected(null, ({ id }) => {
21+
const code = 'window.location.reload();'
22+
chrome.tabs.executeScript(id, { code })
23+
})
24+
window.location.href = '/options_logged_info.html'
2825
})
29-
window.location.href = '/options_logged_info.html'
3026
})
31-
return
32-
}
33-
xhr.send(JSON.stringify(params))
27+
.catch(({ message }) => alert(message))
3428
}
3529

3630
const onKeyPress = ({ keyCode }) => (keyCode === 13 ? validate() : null)

0 commit comments

Comments
 (0)