-
Notifications
You must be signed in to change notification settings - Fork 20
/
options.js
66 lines (58 loc) · 1.87 KB
/
options.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
/* global chrome */
var currentToken
function message (text) {
document.getElementById('message').innerHTML = text
}
chrome.storage.sync.get('token', ({token}) => {
if (chrome.runtime.lastError) {
message(chrome.runtime.lastError.message)
return
}
if (document.getElementById('token').value === '' && token) {
document.getElementById('token').value = token
currentToken = token
} else {
message('please input a token, otherwise this extension may (and probably will) fail to resolve relative module paths (maybe other unpredictable issues will happen also).')
}
})
document.getElementById('token').addEventListener('input', function (e) {
message('')
})
document.getElementById('token').addEventListener('blur', function (e) {
if (!e.target.value) {
if (currentToken) {
chrome.storage.sync.set({token: null}, () => {
if (chrome.runtime.lastError) {
message(chrome.runtime.lastError.message)
return
}
message('token removed.')
})
}
return
}
window.fetch('https://api.github.com/user', {
headers: {'Authorization': 'token ' + e.target.value}
})
.then(r => r.json())
.catch(() => {
message('failed to verify token validity. do you have an internet connection?<br>nevermind, it will be saved anyway.')
currentToken = e.target.value
return new Promise((resolve, reject) => setTimeout(resolve, 4000))
})
.then(user => {
if (user.login) {
currentToken = e.target.value
chrome.storage.sync.set({token: e.target.value}, () => {
if (chrome.runtime.lastError) {
message(chrome.runtime.lastError.message)
return
}
message('hello, ' + user.login + '! your token is now saved!')
})
} else {
message('this is not a valid token, so we will not save it.')
}
})
})
chrome.storage.sync.set({seenOptions: true})