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

Commit a47d3d1

Browse files
authored
Merge pull request #5 from x86chi/ES6+
update to ES6+ syntax
2 parents be8c673 + d0cca01 commit a47d3d1

3 files changed

Lines changed: 80 additions & 89 deletions

File tree

inject_stylesheet.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
var url = window.location.href;
2-
if (
3-
url.includes("acmicpc.net")
4-
) inject("override-commons.css");
1+
const inject = localCss => {
2+
const href = chrome.extension.getURL(`css/${localCss}`)
3+
console.log(href)
4+
const injection = document.createElement('link')
5+
Object.assign(injection, {
6+
rel: 'stylesheet',
7+
type: 'text/css',
8+
href,
9+
})
10+
document.getElementsByTagName('head')[0].appendChild(injection)
11+
}
512

6-
function inject(localCss) {
7-
console.log(chrome.extension.getURL("css/" + localCss));
8-
var injection = document.createElement("link");
9-
injection.setAttribute("rel", "stylesheet");
10-
injection.setAttribute("type", "text/css");
11-
injection.setAttribute("href", chrome.extension.getURL("css/" + localCss));
12-
document.getElementsByTagName("head")[0].appendChild(injection);
13-
}
13+
const url = window.location.href
14+
if (url.includes('acmicpc.net')) inject('override-commons.css')

options_logged_info.js

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
1-
var loggedInUser = document.getElementById('user_id');
2-
var logoutButton = document.getElementById('submit');
1+
const loggedInUser = document.getElementById('user_id')
2+
const logoutButton = document.getElementById('submit')
33

4-
function logout() {
5-
chrome.storage.local.remove("token", function() {
6-
chrome.tabs.getSelected(null, function(tab) {
7-
var code = 'window.location.reload();';
8-
chrome.tabs.executeScript(tab.id, {code: code});
9-
});
10-
window.location.href = '/options_login.html';
11-
});
4+
const logout = () => {
5+
chrome.storage.local.remove('token', () => {
6+
chrome.tabs.getSelected(null, ({ id }) => {
7+
const code = 'window.location.reload();'
8+
chrome.tabs.executeScript(id, { code })
9+
})
10+
window.location.href = '/options_login.html'
11+
})
1212
}
1313

14-
function validateToken(token) {
15-
var params = {
16-
"token": token
17-
};
18-
var xhr = new XMLHttpRequest();
19-
xhr.open('POST', 'https://api.solved.ac/validate_token.php', true);
20-
xhr.setRequestHeader('Content-type', 'application/json');
21-
xhr.onload = function () {
22-
console.log(this.responseText);
23-
if (this.status == 200) {
24-
var response = JSON.parse(this.responseText);
25-
loggedInUser.innerText = response.user.user_id;
26-
} else {
27-
alert(JSON.parse(this.responseText).error);
28-
logout();
29-
}
30-
};
31-
xhr.send(JSON.stringify(params));
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))
3230
}
3331

34-
chrome.storage.local.get(["token"], function(items){
35-
debugger;
36-
if (items.token) {
37-
validateToken(items.token);
38-
}
39-
});
32+
chrome.storage.local.get(['token'], ({ token }) => {
33+
debugger
34+
if (token) {
35+
validateToken(token)
36+
}
37+
})
4038

41-
logoutButton.addEventListener("click", logout);
39+
logoutButton.addEventListener('click', logout)

options_login.js

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
1-
chrome.storage.local.get(["token"], function(items){
2-
debugger;
3-
if (items.token) {
4-
window.location.href = '/options_logged_info.html';
5-
}
6-
});
1+
chrome.storage.local.get(['token'], ({ token }) => {
2+
debugger
3+
if (token) window.location.href = '/options_logged_info.html'
4+
})
75

8-
var submitButton = document.getElementById('submit');
6+
const submitButton = document.getElementById('submit')
7+
const userIdInput = document.getElementById('user_id')
8+
const passwordInput = document.getElementById('password')
99

10-
var userIdInput = document.getElementById('user_id');
11-
var passwordInput = document.getElementById('password');
10+
const validate = () => {
11+
const { value: user_id } = userIdInput
12+
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+
}
1223

13-
function onKeyPress() {
14-
if (event.keyCode == 13) {
15-
validate();
16-
}
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 })
28+
})
29+
window.location.href = '/options_logged_info.html'
30+
})
31+
return
32+
}
33+
xhr.send(JSON.stringify(params))
1734
}
1835

19-
function validate() {
20-
var userId = userIdInput.value;
21-
var password = passwordInput.value;
22-
var params = {
23-
"user_id": userId,
24-
"password": password
25-
};
26-
var xhr = new XMLHttpRequest();
27-
xhr.open('POST', 'https://api.solved.ac/request_token.php', true);
28-
xhr.setRequestHeader('Content-type', 'application/json');
29-
xhr.onload = function () {
30-
console.log(this.responseText);
31-
if (this.status == 200) {
32-
chrome.storage.local.set({ "token" : JSON.parse(this.responseText).token }, function() {
33-
chrome.tabs.getSelected(null, function(tab) {
34-
var code = 'window.location.reload();';
35-
chrome.tabs.executeScript(tab.id, {code: code});
36-
});
37-
window.location.href = '/options_logged_info.html';
38-
});
39-
} else {
40-
alert(JSON.parse(this.responseText).error);
41-
}
42-
};
43-
xhr.send(JSON.stringify(params));
44-
}
36+
const onKeyPress = ({ keyCode }) => (keyCode === 13 ? validate() : null)
4537

46-
submitButton.addEventListener("click", validate);
47-
userIdInput.addEventListener("keyup", onKeyPress);
48-
passwordInput.addEventListener("keyup", onKeyPress);
38+
submitButton.addEventListener('click', validate)
39+
userIdInput.addEventListener('keyup', onKeyPress)
40+
passwordInput.addEventListener('keyup', onKeyPress)

0 commit comments

Comments
 (0)