-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
235 lines (183 loc) · 6.34 KB
/
background.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
// Set up the .content div
function setupDom() {
// Create DOM element
var dom = $('<div/>').addClass('content');
// Append h1 with gjengnavn
dom.append(getGjeng());
// Append the search form
dom.append(getSearchForm());
// Append "groups" div
dom.append(createDivGroups());
// Append "alle" div
dom.append(createDivAlle());
return dom;
}
// GET STUFF FROM ORIGINAL
// -----------------------------------------------------------
// Find all lists on the original site
function findAllEmailLists() {
// Get list table anchor elements
var trs = $('div.content table tr');
// Iterate through all anchors and store href/listname
var lists = [];
$.each(trs, function(ind, val) {
var a = $('a', val);
var inputCsrf = $('form input[name="csrftoken"]', val);
lists.push({ 'href': a.attr('href'), 'name': a.text(), 'csrftoken': inputCsrf.val() });
});
return lists;
}
// Get gjengnavn h1
function getGjeng() {
return $('h1');
}
// Get search form
function getSearchForm() {
return $('form[action="search.cgi"]');
}
// -----------------------------------------------------------
// CREATING SHIT ON MY OWN
// -----------------------------------------------------------
// Create the "groups" div
function createDivGroups() {
var div = $('<div/>');
div.append($('<h2/>').text('Groups'));
div.append($('<input/>').attr({ 'type': 'text', 'id': 'addGroupName' }));
div.append($('<button/>').text('Legg til gruppe').click(
function() {
var name = $('#addGroupName').val();
if (name.length > 0)
Storage.addGroup(name, function() { location.reload(); });
else
alert("Groupn må ha et navn.");
})
);
Storage.getGroups(function(res) {
var groups = res.groups;
$.each(groups, function(ind, val) {
div.append($('<h3/>').text(val.name));
});
});
return div;
}
// Create the "alle" div
function createDivAlle() {
var div = $('<div/>');
div.append($('<h2/>').text('Alle'));
var lists = findAllEmailLists();
var tableRows = [];
$.each(lists, function(ind, val) {
var row = [];
var tdLink = $('<a/>').attr('href', val.href).text(val.name);
var tdForm = createTdForm(val.name, val.csrftoken);
var tdGroupDropdown = $('<select/>');
createGroupDropdown(tdGroupDropdown);
tableRows.push([tdLink, tdForm, tdGroupDropdown]);
});
div.append(createTable(tableRows));
return div;
}
// Create the form going in each table row
function createTdForm(listname, csrftoken) {
var form = $('<form/>');
form.attr({'action': 'delete_list.cgi', 'method': 'post'});
form.append($('<input/>').attr({ 'type': 'hidden', 'name': 'listname', 'value': listname }));
form.append($('<input/>').attr({ 'type': 'hidden', 'name': 'csrftoken', 'value': csrftoken }));
form.append($('<input/>').attr({ 'type': 'submit', 'value': 'Slett liste' }));
return form;
}
// Create a table from a list of rows
function createTable(rows) {
var table = $('<table/>');
$.each(rows, function(ind, val) {
var tr = $('<tr/>');
$.each(val, function(ind2, val2) {
var td = $('<td/>');
td.append(val2);
tr.append(td);
});
table.append(tr);
});
return table;
}
// Create the gruppe dropwdown to be added to each element in "alle"
function createGroupDropdown(select) {
var createSelects = function(res) {
//console.log(groups);
if (res.groups.length === 0) {
select.append($('<option/>').val('null').text('Ingen grupper eksisterer'));
} else {
select.append($('<option/>').val('null').text('Ingen gruppe'));
$.each(res.groups, function(ind, val) {
select.append($('<option/>').val(val.name).text(val.name));
});
}
}
var groups = Storage.getGroups(createSelects);
}
// -----------------------------------------------------------
// STORAGE
// -----------------------------------------------------------
var Storage = {
init: function() {
var initGroups = function(res) {
if (res.groups === undefined)
chrome.storage.sync.set({ 'groups': [] }, function() {
console.log('Storage: "groups" created');
});
};
this.getGroups(initGroups)
},
getGroups: function(callback) {
return chrome.storage.sync.get('groups', callback);
},
addGroup: function(name, callback) {
this.groupExists(name,
// If it exists
function() {
alert('Gruppen eksisterer allerede.');
},
// If it doesn't
function() {
chrome.storage.sync.get('groups', function(res) {
var groups = res.groups;
groups.push({ 'name': name, 'members': [], 'subgroups': [] });
chrome.storage.sync.set({ 'groups': groups }, function() {
console.log('Storage: added group "' + name + '"')
if (callback !== undefined)
callback();
})
});
}
);
},
groupExists: function(name, callbackSuccess, callbackFail) {
this.getGroups(function(res) {
var found = res.groups.find(function(elmt, ind, arr) {
return elmt.name === name;
});
if (found !== undefined)
callbackSuccess(found);
else
callbackFail();
});
},
addToGroup: function(group, element, callback) {
this.groupExists(group,
// If it exists
function(found) {
this.getGroups(function(res) {
var groups = res.groups;
});
},
// If it doesn't
function() {
alert('Gruppen eksisterer ikke. Vet ikke hva som gikk galt der.');
}
}
};
// -----------------------------------------------------------
//chrome.storage.sync.remove('groups', function() { console.log('Removed "groups"') });
Storage.init();
var content = $('div.content');
content.replaceWith(setupDom);