-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuryanamascara.js
70 lines (69 loc) · 2.24 KB
/
suryanamascara.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
if (loggator()) {
// Append Today button
var surya = document.createElement('button');
surya.innerHTML = 'Today';
surya.id = 'snButton';
// Insert in footer
// document.querySelector('footer').appendChild(surya);
// insert in main
// document.querySelector('main').insertBefore(surya,document.getElementById('years'));
document.getElementById('commands').appendChild(surya);
// Retrieve token
var localFnp = localStorage.getObject('fnp');
surya.addEventListener('click', practice);
// Append Yesterday button
var suryaye = document.createElement('button');
suryaye.innerHTML = 'Yesterday';
suryaye.id = 'yeButton';
// DEBUG: DONT SHOW YESTERDAY BUTTON
// document.querySelector('footer').appendChild(suryaye);
// Add listener
suryaye.addEventListener('click', function(e) { practice(e, true); }, false );
}
function practice (e, yesterday) {
yesterday = yesterday || false;
if(e) e.preventDefault();
fetch('https://api.github.com/repos/ashtanga/ashtanga.github.io/contents/_data/practice.csv',{
method: 'GET',
headers: {
Authorization: 'token ' + atob(localFnp.token),
Accept: 'application/vnd.github.v3.full+json'
}
}).then(
function (r) { return r.json(); }
).then(
function (l) {
var sha = l.sha;
var cnt = atob(l.content);
var date = new Date();
var giornoPratica = (yesterday) ? date.setDate(date.getDate() - 1) : date;
var newDate = giornoPratica.getFullYear() + '-' + ('0' + (giornoPratica.getMonth() + 1)).slice(-2) + '-' + ('0' + giornoPratica.getDate()).slice(-2);
var newfile = cnt + newDate + "\n";
var requestData = {
message: 'Update practice.csv by button',
sha: sha,
content: btoa(newfile)
};
return fetch('https://api.github.com/repos/ashtanga/ashtanga.github.io/contents/_data/practice.csv',{
method: 'PUT',
headers: {
Authorization: 'token ' + atob(localFnp.token),
Accept: 'application/vnd.github.v3.full+json'
},
body: JSON.stringify(requestData)
}).then(
function(r){
return r.json();
}
).then(
function(payload){
if(payload.commit.sha.length == 40){
var oldurl = window.location.origin;
window.location.href = oldurl + '/#' + payload.commit.sha;
window.location.reload();
}
}
).catch(console.log);
}
);
}