-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.js
65 lines (53 loc) · 1.98 KB
/
notify.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
var notificationsAreaId = "simplNotificationsArea";
var notificationsArea = null;
var timers = {};
function initNotificationsArea() {
notificationsArea = document.getElementById(notificationsAreaId);
if (notificationsArea == null) {
notificationsArea = document.createElement("div");
notificationsArea.setAttribute("id", notificationsAreaId);
notificationsArea.setAttribute("class", "simpl-notifications-area");
document.body.appendChild(notificationsArea);
}
}
function addNotification(imageUrl, fileName, notificationText) {
initNotificationsArea();
var wrapper = document.getElementById("simpl-" + fileName);
if (wrapper == null) {
wrapper = document.createElement("div");
wrapper.setAttribute("id", "simpl-" + fileName);
var img = document.createElement("img");
img.setAttribute("src", imageUrl);
var notificationP = document.createElement("p");
notificationP.innerText = notificationText;
wrapper.appendChild(img);
wrapper.appendChild(notificationP);
notificationsArea.insertAdjacentElement("afterBegin", wrapper);
} else {
// already exists, update text and reset timeout
var notificationP = wrapper.getElementsByTagName("p")[0];
notificationP.innerText = notificationText;
}
clearTimeout(timers[fileName]);
clearTimeout(timers[fileName + "-remove"]);
setForRemoval(wrapper, fileName);
}
function setForRemoval(element, fileName) {
timers[fileName] = window.setTimeout(function() {
element.className = "fade";
timers[fileName + "-remove"] = window.setTimeout(function() {
element.remove();
}, 2000);
}, 10000);
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.state == "PROGRESS") {
addNotification(message.imageUrl, message.fileName, ("Uploading... (" + message.progress + "%)"));
sendResponse({});
} else {
if (message.imageUrl) {
addNotification(message.imageUrl, message.fileName, "Image saved");
sendResponse({});
}
}
});