-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagecheck.js
217 lines (163 loc) · 6.63 KB
/
agecheck.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
// The base url for the domain info API
const API_BASE = "https://rdap.verisign.com/com/v1/domain/"
// The base URL for domains with "-" in them
const API_BASE_DASH = "https://rdap.publicinterestregistry.org/rdap/domain/"
// I'm not sure if this is consistent or the best way to select all of the emails
const EMAIL_SPAN_CLASS = 'gD'
// What parent level the domain info box needs to be at when the email is collapsed
const COLLAPSE_PAR_NUM = 4
// What parent level the domain info box needs to be at when the email is expanded
const EXP_PAR_NUM = 10
const LABEL_COLORS = {
"Good": "green",
"Warning": "orange",
"Suspicious": "red"
}
const ACCORD_FOLDED_CLASS = "kQ bg adv"
const ACCORD_UNFOLDED_CLASS = "kv bg"
// Get the domain info from verisign
// Modified from https://stackoverflow.com/a/22790025
function getDomainInfo(domain2Check) {
var Httpreq = new XMLHttpRequest(); // a new request
let reqURL = API_BASE + domain2Check
if(domain2Check.includes("-"))
{
reqURL = API_BASE_DASH + domain2Check
}
Httpreq.open("GET", reqURL, false);
Httpreq.send(null);
if (Httpreq.responseText.length == 0) {
return null
}
return JSON.parse(Httpreq.responseText);
}
// Get the registration time info from the API
function getRegInfo(data) {
let info = {};
data["events"].forEach(element => {
tmp = new Date(Date.parse(element["eventDate"]))
let ea = element["eventAction"]
info[ea] = {}
info[ea]["date"] = tmp.toLocaleTimeString(undefined, { year: 'numeric', month: 'long', day: 'numeric' })
info[ea]["diff"] = Date.now() - tmp.getTime()
});
return info;
}
// Get the spans that contain the email headers
function getEmailBlocks(onlyVis) {
spans = Array.from(document.getElementsByClassName(EMAIL_SPAN_CLASS));
if (onlyVis) {
return spans.filter((ele) => ele.offsetParent !== null)
}
return spans
}
// Add a label to an email
// Will add attribute to element to only 1 label is added
function addLabel(ele) {
// Test if element already has label
if (ele.getAttribute("data-haslabel") !== "true") {
addr = ele.getAttribute("email");
domain = addr.split("@")[1];
// Remove extra bits of the domain (issue #7)
tmp = domain.split(".")
domain = tmp[tmp.length-2]+"."+tmp[tmp.length-1]
d = document.createElement("div");
d.style.border = "black"
d.style.backgroundColor = LABEL_COLORS["Good"]
d.style["border-radius"] = "10px";
d.style["text-align"] = "center";
d.style.color = "black"
tmp = getDomainInfo(domain);
if (tmp != null) {
info = getRegInfo(tmp);
d.innerText = "This domain was created on " + info["registration"]["date"];
diffDays = info["registration"]["diff"] / (1000 * 3600 * 24);
// Different text/color depending on the time difference
if (diffDays < 1) {
d.style.backgroundColor = LABEL_COLORS["Suspicious"]
d.innerText += " (Less than a day ago!!!)"
}
else if (diffDays < 7) {
d.style.backgroundColor = LABEL_COLORS["Suspicious"]
d.innerText += " (Less than a week ago!!!)"
}
else if (diffDays < 365.25) {
d.style.backgroundColor = LABEL_COLORS["Warning"]
d.innerText += " (Less than a year ago)"
}
else {
d.innerText += " (More than a year ago)"
}
}
else {
d.innerText = "Weird, this domain does not exist"
}
parNum = EXP_PAR_NUM;
if (ele.classList.contains("bco")) {
parNum = COLLAPSE_PAR_NUM;
}
par = ele;
for (i = 0; i < parNum; i++) {
par = par.parentElement
}
// Insert label in correct spot
par.insertAdjacentElement("afterend", d)
ele.setAttribute("data-haslabel", "true")
// Add event listener for if the email is expanded or collapsed
// The other form of the email doesn't exist in the DOM until it needs to be seen
par.addEventListener("click", event => {
window.setTimeout(() => {
spans = getEmailBlocks(false)
spans.forEach(addLabel)
}
, 0)
})
}
}
// Select the <div> element with id "loading"
const loadingDiv = document.getElementById('loading');
// The div with folded emails
// Will be filled in the following mutation observer
let accordEle = null;
// The mutation observer for the accordion
// Will be filled in the following mutation observer
let obs = null;
// Create a MutationObserver to watch for when the loading div becomes hidden
// Will create another mutation observer if there is an accordion in the email chain
const observer = new MutationObserver((mutationsList, observer) => {
mutationsList.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
// Check if the "display" style property has changed to "none" (hidden)
if (loadingDiv.style.display === 'none') {
// The <div> with id "loading" is now hidden
spans = getEmailBlocks(false)
spans.forEach(addLabel)
// If there is a fold in this email chain
let tmp = document.getElementsByClassName(ACCORD_FOLDED_CLASS)
if (tmp.length > 0) {
accordEle = tmp[0]
obs = new MutationObserver((mutationsList, observer) => {
mutationsList.forEach((mutation) => {
// If the class changed and the class name is correct
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
if (accordEle.className == ACCORD_UNFOLDED_CLASS) {
spans = getEmailBlocks(false)
spans.forEach(addLabel)
obs.disconnect()
}
}
});
});
obs.observe(accordEle, { attributes: true, attributeFilter: ['class'] });
}
}
}
});
});
// Start observing the <div> element for changes in the 'style' attribute
const config = { attributes: true };
observer.observe(loadingDiv, config);
window.addEventListener('popstate', function (event) {
spans = getEmailBlocks(false)
spans.forEach(addLabel)
});