-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherepublikmanagedailytask.user.js
278 lines (242 loc) · 6.58 KB
/
erepublikmanagedailytask.user.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ==UserScript==
// @name ERepublik - manage daily tasks
// @namespace arvid.jakobsson@gmail.com
// @description Allows citizens to add their own daily tasks
// @include http://www.erepublik.com/daily_alerts.html*
// ==/UserScript==
(function() {
GM_addStyle("#doneTaskListHolder { opacity: 0.5; }");
addNewTaskLink();
showTasks();
})();
function getTasks() {
return deserialize('tasks', '({})');
}
function saveTask(task) {
var newTaskObj = getTasks();
if (newTaskObj[task.name] != null) {
return false;
}
else {
newTaskObj[task.name] = task;
task.id = getUniqueTaskId();
saveTasks(newTaskObj);
return true;
}
}
function getUniqueTaskId() {
var taskObj = getTasks();
var uniqueId = -1;
for (var key in taskObj) {
uniqueId = Math.max(uniqueId, taskObj[key].id);
}
return uniqueId+1;
}
function updateTask(task) {
var newTaskList = getTasks();
newTaskList[task.name] = task;
saveTasks(newTaskList);
}
function saveTasks(tasks) {
serialize('tasks', tasks);
}
function showTask(task) {
var taskEl = {
n: 'div', a: {'@class' : 'alert', '@id': 'taskNode' + task.id},
c: [
{n: 'div', a: {textContent: task.name, '@class' : 'alert-title'}},
{
n: 'div', a: {'@class' : 'alert-content'},
c: [
{n: 'p', a: {innerHTML: task.desc, '@style': 'float: left'}},
{n: 'div', c: [
{
n: 'input',
a: {'@type': 'checkbox'},
el: {e: 'click', l: function(e) { taskDone(task, e); } }
},
{n: 'label', a: {textContent: 'Done?'}}, //TODO: fixa "for"-attributet
{n: 'div', c: [
{n: 'a', a: {'@href': 'javascript:;', textContent: 'remove'}, el: {e: 'click', l: function(e) { removeTask(task, e); }}}
]}
]}
]
}
]
};
var holder;
if (isDone(task)) {//UGLEY!
taskEl.c[1].c[1].a['@checked'] = 'checked';
holder = $xs("id('doneTaskListHolder')");
}
else
holder = $xs("id('taskListHolder')");
createEl(taskEl, holder);
}
function removeTask(task) {
$('taskNode' + task.id).parentNode.removeChild($('taskNode' + task.id));
var taskObj = getTasks();
//taskObj[task.name] = undefined;
delete taskObj[task.name];
saveTasks(taskObj);
}
function taskDone(task, event) {
var holder, last_done, taskNode = $('taskNode' + task.id);
if (event.target.checked) {
last_done = getERepTime();
holder = $('doneTaskListHolder');
}
else {
task.last_done = undefined;
holder = $('taskListHolder');
}
if (holder.firstChild)
holder.insertBefore(taskNode, holder.firstChild);
else
holder.appendChild(taskNode);
task.last_done = last_done;
updateTask(task);
}
function isDone(task) {
return (task.last_done ? task.last_done.date == getERepTime().date : false);
}
function getERepTime(time) { //VOLATILE
var time_str = $xs("id('headinfo5')/div[@class='time']").textContent;
var hr = parseInt(time_str.match(/(\d+):\d+/)[1], 10);
var min = parseInt(time_str.match(/\d+:(\d+)/)[1], 10);
var time = hr*60 + min;
var date = parseInt($xs("id('headinfo5')/div[@class='date']").textContent.match(/Day\s+(\d+)/)[1], 10);
return {date: date, time: time};
}
function showTasks() {
var tasks = getTasks();
for (var key in tasks)
showTask(tasks[key]);
}
function addNewTaskLink() {
var holder = $xs("id('primary')/div");
/* creates task list holder */
var taskListHolder = createEl({
n: 'div',
a: {'@id': 'taskListHolder'}
});
holder.appendChild(taskListHolder);
//holder.appendChild(createEl({n: 'p', a: {textContent: 'Done tasks:'}}));
var doneTaskListHolder = createEl({
n: 'div',
a: {'@id': 'doneTaskListHolder'}
});
holder.appendChild(doneTaskListHolder);
/* creates new task link */
var newTaskLink = createEl({
n: 'a',
a: {'@href': '#', textContent: 'Add new daily task', '@id': 'newTaskLink'}
});
newTaskLink.addEventListener('click', showNewTaskForm, false);
holder.appendChild(newTaskLink);
/* creates new task form */
var newTaskForm = createEl ({
n: 'form',
a: {'@id': 'newTaskForm', '@style' : 'display: none'},
c: [
{n: 'p', a: {textContent: 'Give your task an unique name:'}},
{n: 'input', a: {'name': 'taskName'}},
{n: 'p', a: {textContent: 'Describe your task (HTML allowed):'}},
{n: 'textarea', a: {'name': 'taskDesc'}},
/*{n: 'p', a: {textContent: 'How often should this task be done?'}},
{n: 'select'},*/
{n: 'input', a: {'type': 'submit', 'value': 'Save'}},
{n: 'span', a: {textContent: ' or '}, c: [
{n: 'a', a: {'@href': 'javascript:;', textContent: 'cancel'}, el: {e: 'click', l: cancelNewTask}}
]}
],
el: {e: 'submit', l: saveTaskFromForm, b: false}
});
//newTaskForm.addEventListener('submit', saveTaskFromForm, false);
holder.appendChild(newTaskForm);
}
function cancelNewTask(e) {
var form = $('newTaskForm')
toggle(form);
form.reset();
e.stopPropagation();
e.preventDefault();
}
function showNewTaskForm(e) {
toggle($('newTaskForm'));
e.stopPropagation();
e.preventDefault();
}
function saveTaskFromForm(e) {
e.stopPropagation();
e.preventDefault();
var form = $('newTaskForm');
var task = {
name: form.elements.namedItem('taskName').value,
desc: form.elements.namedItem('taskDesc').value,
interval: 123,
last_done: undefined
};
if (task.name == null || task.name == "") {
alert('Task name required');
}
else {
if (!saveTask(task))
alert('a task with that name already exist');
else {
showTask(task);
form.reset();
toggle(form);
}
}
}
/* *** */
function $x(xpath, root) { // From Johan Sundström
var doc = root ? root.evaluate ? root : root.ownerDocument : document, next;
var got = doc.evaluate(xpath, root||doc, null, null, null), result = [];
while(next = got.iterateNext())
result.push(next);
return result;
}
function $xs(xpath, root) {
return $x(xpath, root)[0];
}
function $(id) { return document.getElementById(id); }
function deserialize(name, def) {
return eval(GM_getValue(name, def) );
}
function serialize(name, val) {
GM_setValue(name, uneval(val));
}
function createEl(elObj, parent) {
var el;
el = document.createElement(elObj.n);
if (elObj.a) {
attributes = elObj.a;
for (var key in attributes) {
if (typeof(attributes[key]) == 'string') {
if (key.charAt(0) == '@')
el.setAttribute(key.substring(1), attributes[key]);
else
el[key] = attributes[key];
}
}
}
if (elObj.c) {
elObj.c.forEach(function (v) { createEl(v, el); });
}
if (elObj.el) {
el.addEventListener(elObj.el.e, elObj.el.l, elObj.el.b ? elObj.el.b : false);
}
if (parent) {
parent.appendChild(el);
}
return el;
}
function toggle(el) {
var vis = el.style.display;
if (el.style.display == 'block')
el.style.display = 'none';
else
el.style.display = 'block';
}