-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
171 lines (139 loc) · 7.31 KB
/
index.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Obsitica Web</title>
<!-- Favicon -->
<link rel="icon" href="favicon.ico?" type="image/x-icon">
<!-- Tailwind CSS -->
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<!-- Open Graph tags for social media -->
<meta property="og:title" content="Obsitica Web - Habitica Integration">
<meta property="og:description"
content="Integrate your Habitica with Obsitica for better productivity and task management.">
<meta property="og:image" content="obsitica.png">
<meta property="og:type" content="website">
<meta property="og:url" content="https://dotmavriq.github.io/obsitica-web/">
<!-- Twitter Cards for social media -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Obsitica Web - Habitica Achievements to Obsidian Markdown Notes">
<meta name="twitter:description"
content="Integrate your Habitica with Obsitica for better productivity and task management.">
<meta name="twitter:image" content="obsitica.png">
<meta name="twitter:url" content="https://dotmavriq.github.io/obsitica-web/">
</head>
<body class="bg-gray-100 flex flex-col items-center justify-center h-screen">
<div class="p-6 rounded-lg shadow-lg bg-white">
<img src="obsitica.png" alt="Obsitica Logo" class="max-w-xs md:max-w-sm lg:max-w-md">
<input type="text" id="habitica-user-id" placeholder="Habitica User ID"
class="mt-2 px-3 py-2 bg-gray-200 rounded-md w-full text-sm" value="">
<input type="text" id="api-token" placeholder="API Token"
class="mt-2 px-3 py-2 bg-gray-200 rounded-md w-full text-sm" value="">
<!-- Button to generate general tasks -->
<button class="mt-4 px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 w-full"
onclick="storeCredentialsAndFetchTasks()">
Generate Habits and Dailies
</button>
<!-- Button to generate TODOs -->
<button class="mt-4 px-4 py-2 bg-purple-500 text-white rounded hover:bg-purple-600 w-full"
onclick="generateTodo()">
Generate TODO
</button>
<div id="output" class="mt-4 p-4 bg-gray-100 rounded-md w-full max-h-96 overflow-auto"></div>
</div>
<script>
async function fetchHabiticaTasks() {
const userId = document.getElementById('habitica-user-id').value;
const apiToken = document.getElementById('api-token').value;
const apiUrl = "https://habitica.com/api/v3/tasks/user";
if (!userId || !apiToken) {
alert("Please enter both your Habitica User ID and API Token.");
return;
}
try {
const response = await fetch(apiUrl, {
headers: {
'x-api-user': userId,
'x-api-key': apiToken
}
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
if (data.success !== true) {
throw new Error('API response is invalid. Please check your credentials.');
}
renderTasks(data.data);
} catch (error) {
console.error('There has been a problem with your fetch operation:', error);
document.getElementById('output').textContent = 'Error fetching data. Please check the console for more information.';
}
}
function renderTasks(tasks) {
const filteredHabits = tasks.filter(task => task.type === 'habit' && (task.counterUp > 0 || task.counterDown < 0));
const habitsOutput = filteredHabits.map(task => {
return `* Habit clicked: ${task.text} - Positive: ${task.counterUp}, Negative: ${task.counterDown}`;
}).join('<br>');
const filteredDailies = tasks.filter(task => task.type === 'daily' && task.completed);
const dailiesOutput = filteredDailies.map(task => {
return `* ${task.text}`;
}).join('<br>');
const today = new Date();
const outputDiv = document.getElementById('output');
outputDiv.innerHTML = `## Achievements on ${today.toISOString().split('T')[0]}<br>${habitsOutput}<br><br>## Completed Dailies<br>${dailiesOutput}`;
}
async function generateTodo() {
const userId = document.getElementById('habitica-user-id').value;
const apiToken = document.getElementById('api-token').value;
const apiUrl = "https://habitica.com/api/v3/tasks/user?type=todos";
if (!userId || !apiToken) {
alert("Please enter both your Habitica User ID and API Token.");
return;
}
try {
const response = await fetch(apiUrl, {
headers: {
'x-api-user': userId,
'x-api-key': apiToken
}
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
if (data.success !== true) {
throw new Error('API response is invalid. Please check your credentials.');
}
renderTodos(data.data);
} catch (error) {
console.error('There has been a problem with your fetch operation:', error);
document.getElementById('output').textContent = 'Error fetching data. Please check the console for more information.';
}
}
function renderTodos(todos) {
const todoOutput = todos.filter(todo => !todo.completed).map(todo => {
// Notes should be on a new line directly after the task if present.
const notesOutput = todo.notes ? `<br> _${todo.notes}_` : '';
// Subtasks should be indented and start on a new line if present.
const subtasksOutput = todo.checklist.length > 0
? `<br> ` + todo.checklist.map(check =>
`- [${check.completed ? 'x' : ' '}] ${check.text}`
).join('<br> ') // Join subtasks with a line break.
: '';
// Combine the task text, notes, and subtasks, with subtasks directly after notes.
return `- [ ] ${todo.text}${notesOutput}${subtasksOutput}`;
}).join('<br>'); // Each task separated by a single line break.
// Prepend a title and wrap with <pre> to preserve formatting.
const outputDiv = document.getElementById('output');
outputDiv.innerHTML = `<pre>### TODO:<br>${todoOutput}</pre>`;
}
function storeCredentialsAndFetchTasks() {
localStorage.setItem('habiticaUserId', document.getElementById('habitica-user-id').value);
localStorage.setItem('apiToken', document.getElementById('api-token').value);
fetchHabiticaTasks();
}
</script>
</body>
</html>