-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpascript.js
63 lines (56 loc) · 2.14 KB
/
dpascript.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
/*
HTML injection goes here
*/
// Before displaying another time the banner, we look if tabId isn't already registered or the website doesn't already have a timer set
function inject(){
//TODO: Avant de réinjecter on regarde si le tableau keys ne contient pas déjà une clé pour cet onglet avec cette url (on envoie un message)
var inject = document.createElement("div");
inject.innerHTML = "<div style=\"position: fixed; top:0;left:0;right:0;background:#F33; z-index:99999999999;color:white;padding:10px 5px;font-size:16px;\" id=\"dpalertbanner\"> For how long do you intend to stay on this website? <input type=\"number\" id=\"timeAmount\" min=\"1\" max=\"60\" placeholder=\"1-60min\" style=\"width:64px\"> <button id=\"validate\"> ADD TIMER </button> <button id=\"ignorebtn\" style=\"float:right;\">X</button></div>";
document.body.insertBefore(inject, document.body.firstChild);
}
/*
* Listener
*/
function evHandler(){
inject();
document.getElementById("validate").addEventListener("click", function(){
var timeAmount = document.getElementById("timeAmount").value;
if(timeAmount > 0 && timeAmount < 61 && Number.isInteger(parseInt(timeAmount))){
chrome.runtime.sendMessage({
content: "addTimer",
amount: timeAmount
},function(r){
document.getElementById("dpalertbanner").style.display = "none";
});
}else{
alert("Value is not an integer or is not between 1 and 60 minutes.");
}
});
document.getElementById("ignorebtn").addEventListener("click", function(){
chrome.runtime.sendMessage({
content: "addIgnore"
},function(r){
document.getElementById("dpalertbanner").style.display = "none";
});
});
}
// Communication with background.js => is website blacklisted?
chrome.runtime.sendMessage({
content: "getStatus"
}, function(res){
if(res.result === true){
//We check if key doesn't already exist
chrome.runtime.sendMessage({
content: "doesKeyExist"
}, function(r){
if(r.result === false){
//We inject the banner if key doesn't exist
evHandler();
}
});
}
});
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
if(message.content === "reinject")
evHandler();
});