This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wandermind.js
194 lines (149 loc) · 3.82 KB
/
wandermind.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
// *** Load jquery, cache, and underscore first! *** //
// Settings
// Strange Chromeness -- can't connect to self.
var modes = ["work", "break"]
var defaultTime = {
set : function(key, time) {
cache.setItem("defaultTime" + key, time)
},
get : function(key) {
times = {"work": 1, "break": 1}
return cache.getItem("defaultTime" + key, times[key])
},
import : function(text) {
return parseInt(text)
},
export : function(time) {
return time.toString()
},
open : function(key) {
return this.export(this.get(key))
},
save : function(key, text) {
this.set(key, this.import(text))
},
}
// Blacklist
var blacklist = {
// low-level routines
set : function (bl) {
cache.setItem("blacklist", bl)
},
get : function () {
return cache.getItem("blacklist", [])
},
import : function(text) {
return text.split("\n")
},
export : function(bl) {
return bl.join("\n")
},
// high-level routines
open : function() {
return this.export(this.get())
},
save : function(text) {
this.set(this.import(text))
},
isBlocked : function(url) {
url = url.toLowerCase()
return _.any(this.get(), function(item) {
return url.search(item.toLowerCase()) != -1
})
}
}
// Tags
var tagCounter = {
set : function (tag, value) {
cache.setItem("tag-" + tag, value)
},
get : function (tag) {
return cache.getItem("tag-" + tag, 0)
},
import : function(text) {
return parseInt(text)
},
export : function(value) {
return value.toString()
},
open : function(key) {
return this.export(this.get(key))
},
save : function(key, text) {
this.set(key, this.import(text))
},
increment : function(key) {
this.set(key, this.get(key) + 1)
}
}
// Whitelists
var whitelist = {
set : function (wl) {
cache.setItem("whitelist", wl)
},
get : function () {
return cache.getItem("whitelist", {})
},
import : function(text) {
items = text.split("\n")
var currentItem = null
var whitelist = {}
_.each(items, function(item) {
if (item[0] == "*") {
currentItem = item.substr(1)
} else {
whitelist[item].push()
}
})
return whitelist
},
export : function(wl) {
var text = ""
_.each(wl, function(value, key) {
text += "*" + key + "\n"
text += value.join("\n") + "\n"
})
return text
},
open : function(key) {
return this.export(this.get(key))
},
save : function(text) {
this.set(this.import(text))
},
getTag : function(tag) {
return this.get()[tag]
},
isAllowed : function(tag, url) {
url = url.toLowerCase()
return _.any(this.getTag(tag), function(item) {
return url.search(item.toLowerCase()) != -1
})
}
}
// Modes
var mode = {
change : function (msg) {
chrome.extension.getBackgroundPage().currentState = msg
chrome.extension.getBackgroundPage().change()
},
// High-level commands
pause : function () {
this.change({mode: "pause", tags: [], duration: 0})
},
work : function(tags, duration) {
this.change({mode: "work", tags: tags, duration: duration})
},
break : function(duration) {
this.change({mode: "break", tags: [], duration: duration})
}
}
// Misc
// Taken from Strict-Pomodoro project
function formatDuration (timeRemaining) {
if(timeRemaining >= 60) {
return Math.round(timeRemaining / 60) + "m";
} else {
return (timeRemaining % 60) + "s";
}
}