-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloggator.js
140 lines (126 loc) · 4.51 KB
/
loggator.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
function b64e(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
function b64d(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + c.charCodeAt(0).toString(16);
}).join(''));
}
function create (tagname, attributes, content) {
var element = document.createElement(tagname),
property;
// element attributes
if(typeof attributes == "object") {
for(property in attributes) {
if(attributes.hasOwnProperty(property)) {
element.setAttribute(property, attributes[property]);
}
}
}
if (typeof attributes === "string") { content = attributes; }
if(content) { element.innerHTML = content; }
return element;
}
Element.prototype.acc = function (input) {
if (input.constructor === Array) {
for (var i = 0; i < input.length; i++) {
if(input[i]) this.appendChild(input[i]);
}
} else if (input.constructor) {
this.appendChild(input);
}
return this;
};
Storage.prototype.setObject = function(key, value) {
this.setItem(key, b64e(JSON.stringify(value)));
};
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(b64d(value));
};
function checkLogin (response) {
if (response.status !== 200) {
// Unauthorized or bad credential
localStorage.removeItem('fnp');
return false;
} else {
// logged, set logout button
var loginButton = document.getElementById('login_button'),
loginForm = document.getElementById("login_form");
loginButton.removeEventListener("click", createLoginForm, false);
// reset login form
if (loginForm) loginForm.parentNode.appendChild(
create('div', {class:'flash'}, 'You are logged in: <a href="' + window.location + '">Reload</a>')
); else {
loginButton.innerHTML = 'Logout';
loginButton.addEventListener("click", logout, false);
}
return true;
}
}
function logout (event) {
event.preventDefault();
localStorage.removeItem('fnp');
var loginButton = document.getElementById('login_button');
loginButton.removeEventListener("click", logout, false);
document.getElementById('login_form_div').appendChild(
create('div', {class:'flash'}, 'You are logged out: <a href="' + window.location + '">Reload</a>')
);
}
// Error callback
function errore (e) {
return console.log('error: ' + e);
}
function getAuth (token) {
var localFnp = localStorage.getObject('fnp') || {};
localFnp.token = btoa(token);
localStorage.setObject('fnp', localFnp);
return fetch('https://api.github.com/user', {
headers: { Authorization: 'token ' + token}
}).then(checkLogin).catch(errore);
}
function serveLoginForm (event) {
event.preventDefault();
var tokenField = document.getElementById('token_field');
if (tokenField.value.length === 40) {
getAuth(tokenField.value);
} else if (tokenField.value.length > 0) {
tokenField.parentNode.appendChild(create('div', {class:'flash'}, 'Invalid token'));
}
tokenField.value = '';
}
function toggleLoginForm () {
var loginFormDiv = document.getElementById("login_form_div");
loginFormDiv.style.display = (window.getComputedStyle(loginFormDiv).getPropertyValue('display') === 'block') ? 'none' : 'block';
}
function createLoginForm (event) {
event.preventDefault();
if (!document.getElementById('login_form')) {
// create form
var formElement = create('form',{id:'login_form'});
// populate and add event
formElement.acc([
create('label', 'Paste your personal token: '),
create('input', {type: 'password', id: 'token_field'}),
create('input', {type: 'submit', id: 'submit_button', value: 'Login'})
]).addEventListener("submit", serveLoginForm);
// append formElement
if (document.getElementById('login_form_div').appendChild(formElement)) {
document.getElementById('login_button').addEventListener("click", toggleLoginForm, false);
}
}
}
var loggator = function () {
// create form
var loginButtonDiv = document.getElementById('login_button_div') ||
document.getElementById('commands').appendChild(create('div', {id:'login_button_div'})),
loginFormDiv = document.getElementById('login_form_div') || document.getElementById('commands').appendChild(create('div', {id:'login_form_div'})),
loginButton = document.getElementById('login_button') || loginButtonDiv.appendChild(create('a', {id:'login_button',href:'#'}, 'Login')),
localFnp = localStorage.getObject('fnp');
// create login event
loginButton.addEventListener("click", createLoginForm, false);
// check token and return
return (localFnp && localFnp.token) ? getAuth(atob(localFnp.token)) : 0;
};