-
Notifications
You must be signed in to change notification settings - Fork 39
/
background.js
164 lines (146 loc) · 6.13 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
/**
* @author Vinit Shahdeo
* @email [email protected]
*/
var count = 0;
$("#banner").fadeTo(4000, 500).slideUp(500, function(){
$("#banner").slideUp(500);
});
$("#developer").fadeTo(4000, 500).slideUp(500, function(){
$("#developer").slideUp(500);
});
function setCookieCount(){
chrome.cookies.getAll({},function(cookies){
count=cookies.length;
document.getElementById("cookie-counter").innerHTML = count;
});
}
setCookieCount();
function displayCookies(){
setCookieCount();
document.getElementById("cookie").style.display="none";
var tableLog = document.getElementById("cookieslog");
tableLog.style.display="table";
tableLog.innerHTML = "";
var domain = document.getElementById("url").value;
//var tarea_regex = /(http(s?))\:\/\//gi;
if(domain=="" || domain==null){
document.getElementById("banner").style.display="block";
document.getElementById("message").style.display-"block";
document.getElementById("banner").style.className="alert alert-danger alert-dismissible";
document.getElementById("message").innerHTML="Invalid URL! <strong>Hint</strong> : Please enter <strong>complete url</strong> including <kbd>http://</kpd> or <kpd>https://</kpd> below and press <span class='label label-primary'>Display Cookies</span>"
}
if(!(domain.indexOf("http://") == 0 || domain.indexOf("https://") == 0)){
document.getElementById("banner").style.display="block";
document.getElementById("message").style.display="block";
document.getElementById("banner").style.className="alert alert-danger alert-dismissible";
document.getElementById("message").innerHTML="Invalid URL! <strong>Hint</strong> : Please enter <strong>complete url</strong> including <kbd>http://</kbd> or <kbd>https://</kbd> below and press <span class='label label-primary'>Display Cookies</span>";
}
else{
document.getElementById("banner").style.display="none";
chrome.cookies.getAll({url:domain},function(cookies){
//var row = tableLog.insertRow(-1);
for(var i in cookies){
if(i==0){
var firstRow = tableLog.insertRow(-1);
firstRow.insertCell(0).innerHTML="<strong>NAME</strong>";
firstRow.insertCell(1).innerHTML="<strong>VALUE</strong>";
}
console.log(cookies[i]);
//var row = "<tr><td>"+cookies[i].name+"</td><td>"+cookies[i].value+"</td></tr>";
var row = tableLog.insertRow(-1);
var value = cookies[i].value;
var name = cookies[i].name;
if(name.length>10){
name = name.substring(0,10);
name+="...";
}
if(value.length>15){
value = value.substring(0,15);
value+="...";
}
row.insertCell(0).innerHTML = name;
row.insertCell(1).innerHTML = value;
}
});
}
}
function setCookies(){
document.getElementById("cookieslog").style.display="none";
var domain = document.getElementById("url").value;
var name = document.getElementById("key").value;
var value = document.getElementById("value").value;
var input = document.getElementById("cookie").style.display="block";
var banner = document.getElementById("banner");
banner.style.display="block";
banner.className="alert alert-info alert-dismissible"
document.getElementById("message").innerHTML = "Please enter the <strong>url</strong>, <strong>name</strong> and <strong>value</strong> pair and click <span class='label label-success'>Set Cookies</span> button."
if(domain=="" || domain==null){
document.getElementById("message").innerHTML = "Please enter the <kbd>url</kbd>, <kbd>name</kbd> & <kbd>value</kbd> pair and click <span class='label label-success'>Set Cookies</span> button."
}
else if(name=="" || name==null || value=="" || value==null){
banner.className="alert alert-warning alert-dismissible";
document.getElementById("message").innerHTML = "Please enter the <code>name</code> and <code>value</code> pair and click <span class='label label-success'>Set Cookies</span> button."
}
else{
chrome.cookies.set({url:domain,name:name,value:value,expirationDate : 1610701693},function(cookie){
console.log("cookie is set");
document.getElementById("banner").className="alert alert-success alert-dismissible";
document.getElementById("message").innerHTML = "<strong>SUCCESS!</strong> Cookies is set for <strong>"+domain+"</strong>";
console.log(cookie);
var name = document.getElementById("key").value="";
var value = document.getElementById("value").value="";
$("#banner").fadeTo(2000, 500).slideUp(500, function(){
$("#banner").slideUp(500);
});
setCookieCount();
});
}
}
//displayCookies();
//setCookies();
function onCookieChanged(){
chrome.cookies.onChanged.addListener(function(cookies){
console.log("cookies are being changed ", cookies.cookie.domain);
console.log(cookies);
});
}
onCookieChanged();
function clearAllCookies(){
console.log("cookies cleared");
chrome.cookies.getAll({}, function(cookies) {
for (var i in cookies) {
removeCookie(cookies[i]);
}
});
document.getElementById("banner").className="alert alert-danger alert-dismissible";
document.getElementById("message").innerHTML = "All Cookies are cleared!";
setCookieCount();
}
function removeCookie(cookie) {
var url = "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain +
cookie.path;
chrome.cookies.remove({"url": url, "name": cookie.name});
}
function updateBanner(){
document.getElementById("banner").style.display="none";
}
document.addEventListener('DOMContentLoaded', function() {
var clear_Cookies = document.getElementById("clear_cookies");
var set_Cookies = document.getElementById("set_cookies");
var display_Cookies = document.getElementById("display_cookies");
var url = document.getElementById("url");
// onClick's logic below:
clear_Cookies.addEventListener('click', function() {
clearAllCookies();
});
set_Cookies.addEventListener('click',function(){
setCookies();
});
display_Cookies.addEventListener('click',function(){
displayCookies();
});
url.addEventListener('blur',function(){
updateBanner();
});
});