-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
215 lines (190 loc) · 4.84 KB
/
popup.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
var dict = {};
var globalName = "";
getSavedUsername();
$("#click").hide();
document.addEventListener('DOMContentLoaded', () => {
chrome.runtime.sendMessage({popupOpen: true}, function(response) {
fillDict(response);
makeList(dict);
});
});
function fillDict(array)
{
for(var i = 0; i < array.length; i++)
{
var instance = array[i];
var myKey = getUniqueKey(instance);
var newInstance = new Object();
var hadKey = false;
for (var key in dict) {
if (dict.hasOwnProperty(key)) {
if(myKey in dict)
{
hadKey = true;
if(dict[key].title === "mention")
{
dict[key].url = "https://www.minnowbooster.net/users/" + globalName + "/mentions";
dict[key].link = (dict[key].numOfAuthors + 1) + " locations"
}
dict[key].numOfAuthors++;
break;
}
}
}
if(!hadKey)
{
newInstance.authors = instance.author;
newInstance.link = instance.link;
newInstance.title = instance.title;
newInstance.url = instance.url;
newInstance.numOfAuthors = 1;
dict[myKey] = newInstance;
}
}
}
function getUniqueKey(instance)
{
if(instance.title === "mention")
{
return instance.title + instance.authors;
}
else if(instance.title === "follow")
{
return instance.title;
}
return instance.title + instance.link;
}
function clear(key)
{
chrome.runtime.sendMessage({delete: dict[key]}, function(response) {});
console.log("deleting: " + key);
delete dict[key];
emptyList();
makeList(dict);
}
function makeList(ourDict)
{
//Get the list element
var list = document.getElementById('list');
for (var key in dict) {
if (dict.hasOwnProperty(key)) {
var div = document.createElement('div');
var p = document.createElement('p');
var i = document.createElement('i');
i.className = "material-icons icons";
i.innerHTML = Sanitizer.escapeHTML`<i class='material-icons clear'>clear</i>`;
var instance = dict[key];
var href = instance.url;
p.onclick= (function(href) { return function () { chrome.tabs.create({url: href}) }; })(href);
i.onclick = (function(key) { return function () { clear(key); }; })(key);
p.className = "link"
var authors = getAllAuthors(instance);
var link = ""
if(instance.title !== "follow")
{
link = instance.link.substr(0, 10) + "\u2026";
}
if(instance.title === "upvote") {
p.innerHTML = Sanitizer.escapeHTML`${authors} upvoted: ${link}`;
div.className = "upvotes";
}
else if(instance.title === "comment") {
p.innerHTML = Sanitizer.escapeHTML`${authors} commented: ${link}`;
div.className = "comments"
}
else if(instance.title === "follow") {
p.innerHTML = Sanitizer.escapeHTML`${authors} now follow(s) you`;
div.className = "follows"
}
else if(instance.title === "resteem") {
p.innerHTML = Sanitizer.escapeHTML`${authors} resteemed: ${link}`;
div.className = "resteems"
}
else {
p.innerHTML = Sanitizer.escapeHTML`${instance.authors} mentioned you in: ${link}`;
div.className = "mentions"
}
// Add it to the list:
div.appendChild(p);
div.appendChild(i);
list.appendChild(div);
}
}
}
function getAllAuthors(instance)
{
var authors = instance.authors;
if(instance.numOfAuthors > 2)
{
authors += " and " + instance.numOfAuthors + " others";
}
return authors;
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.msg === "add") {
$("#list").append(chrome.runtime.getBackgroundPage(function(backgroundPage) {
emptyList();
dict = {};
fillDict(backgroundPage.list);
backgroundPage.reset();
makeList(dict);
}));
}
}
);
function emptyList()
{
var myNode = document.getElementById("list");
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
}
/**
* Gets the saved username.
*
*/
function getSavedUsername() {
chrome.storage.sync.get('username', function(name){
if(name['username'])
{
var result = name['username'].toString();
globalName = result;
$("#txt").val(result)
$("#status").html("Status: Connected as " + result);
$("#target").hide();
$("#click").show();
}
});
}
function retrieveUsername()
{
chrome.storage.sync.get('username', function(name){
if(name['username'])
{
return name['username'].toString();
}
});
}
/**
* Sets the given username for the extension..
*
* @param {string} The name to be stored.
*/
function saveUsername(name) {
chrome.storage.sync.set({'username': name});
}
$(document).on('submit','#target',function(){
var name = $("#txt").val().toLowerCase();
globalName = name;
saveUsername(name);
$("#txt").val(name);
$("#status").html("Status: Connected as " + name);
$("#target").hide();
$("#click").show();
return false;
});
$(document).on('click','#edit',function(){
$("#target").show();
return false;
});