-
Notifications
You must be signed in to change notification settings - Fork 0
/
FavaGitSync.js
63 lines (57 loc) · 1.62 KB
/
FavaGitSync.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
export default {
guessURL() {
const re = /^(.*)\/(?:income_statement|balance_sheet|trial_balance|journal|query|holdings|commodities|documents|events|statstics|editor|import|options|help)\/?$/;
const url = window.location.href
const found = url.match(re)
if (!found) {
return "/beancount"
}
return found[1]
},
guessedURL: "",
init() {
this.guessedURL = this.guessURL()
const spacer = document.querySelector("header > span.spacer");
const header = document.querySelector("header");
const syncButton = document.createElement("Button");
syncButton.id = "git-sync-button"
syncButton.textContent = 'sync'
syncButton.style.fontWeight = "bold"
syncButton.style.cursor = "pointer"
var timeout
syncButton.onclick = async () => {
clearTimeout(timeout)
fetch(this.guessedURL + "/extension/FavaGitSync/sync").
then(response => {
if (response.ok) {
syncButton.textContent = "sync ✅"
syncButton.style.backgroundColor = ""
timeout = setTimeout(() => {syncButton.textContent = "sync"}, 2000)
syncButton.blur()
} else {
syncButton.textContent = "sync fail"
syncButton.style.backgroundColor = "red"
syncButton.blur()
}
})
}
header.insertBefore(syncButton, spacer)
},
onPageLoad() {
const syncButton = document.getElementById("git-sync-button");
if (syncButton == null) {
return
}
fetch(this.guessedURL + "/extension/FavaGitSync/status").
then(response => {
if (response.status == 250) {
syncButton.textContent = "sync 🟠"
} else if (response.status == 200) {
} else {
syncButton.textContent = "sync ?"
}
})
},
onExtensionPageLoad() {
},
};