-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_extension
More file actions
193 lines (166 loc) · 6.62 KB
/
Copy pathemail_extension
File metadata and controls
193 lines (166 loc) · 6.62 KB
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
// Gmail Spam Detector Content Script
// This script runs on Gmail pages to detect spam
class GmailSpamDetector {
constructor() {
this.apiUrl = 'http://localhost:5001'; // Your API server
this.init();
}
init() {
console.log('Gmail Spam Detector initialized');
// Wait for Gmail to load
this.waitForGmail().then(() => {
this.setupEmailObserver();
this.checkVisibleEmails();
});
}
waitForGmail() {
return new Promise((resolve) => {
const checkGmail = () => {
if (document.querySelector('[data-thread-id]') ||
document.querySelector('.ii.gt')) {
resolve();
} else {
setTimeout(checkGmail, 1000);
}
};
checkGmail();
});
}
setupEmailObserver() {
// Observe changes in the email list
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length > 0) {
setTimeout(() => this.checkVisibleEmails(), 1000);
}
});
});
const emailContainer = document.querySelector('#\\:2') ||
document.querySelector('[role="main"]');
if (emailContainer) {
observer.observe(emailContainer, {
childList: true,
subtree: true
});
}
}
extractEmailContent(emailElement) {
try {
// Try to extract email content from Gmail's structure
const subjectElement = emailElement.querySelector('[data-legacy-thread-id]') ||
emailElement.querySelector('h2') ||
emailElement.querySelector('.hP');
const bodyElement = emailElement.querySelector('.ii.gt div') ||
emailElement.querySelector('[dir="ltr"]');
const senderElement = emailElement.querySelector('.go .g2') ||
emailElement.querySelector('.yW span');
const subject = subjectElement ? subjectElement.textContent.trim() : 'No Subject';
const body = bodyElement ? bodyElement.textContent.trim() : '';
const sender = senderElement ? senderElement.textContent.trim() : 'Unknown';
return {
subject: subject,
body: body,
sender: sender,
fullContent: `Subject: ${subject}\nFrom: ${sender}\n\n${body}`
};
} catch (error) {
console.error('Error extracting email content:', error);
return null;
}
}
async checkEmailSpam(emailContent) {
try {
const response = await fetch(`${this.apiUrl}/check-email`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email_content: emailContent.body,
subject: emailContent.subject,
sender: emailContent.sender
})
});
if (response.ok) {
return await response.json();
} else {
console.error('API request failed:', response.status);
return null;
}
} catch (error) {
console.error('Error calling spam detection API:', error);
return null;
}
}
addSpamIndicator(emailElement, spamResult) {
// Remove existing indicators
const existingIndicator = emailElement.querySelector('.spam-detector-indicator');
if (existingIndicator) {
existingIndicator.remove();
}
// Create spam indicator
const indicator = document.createElement('div');
indicator.className = 'spam-detector-indicator';
indicator.style.cssText = `
position: absolute;
top: 5px;
right: 5px;
padding: 4px 8px;
border-radius: 4px;
font-size: 11px;
font-weight: bold;
z-index: 1000;
${spamResult.is_spam ?
'background-color: #ff5722; color: white;' :
'background-color: #4caf50; color: white;'
}
`;
indicator.textContent = spamResult.is_spam ?
`🚨 SPAM (${(spamResult.spam_probability * 100).toFixed(1)}%)` :
`✅ SAFE (${((1 - spamResult.spam_probability) * 100).toFixed(1)}%)`;
indicator.title = `Spam confidence: ${(spamResult.spam_probability * 100).toFixed(2)}%`;
// Make email container relative positioned
emailElement.style.position = 'relative';
emailElement.appendChild(indicator);
// Add background color for spam emails
if (spamResult.is_spam) {
emailElement.style.backgroundColor = '#ffebee';
emailElement.style.borderLeft = '4px solid #ff5722';
}
}
async checkVisibleEmails() {
// Find email elements (Gmail's structure can vary)
const emailElements = document.querySelectorAll('[data-thread-id], .zA, .ii.gt');
for (const emailElement of emailElements) {
// Skip if already processed
if (emailElement.hasAttribute('data-spam-checked')) {
continue;
}
const emailContent = this.extractEmailContent(emailElement);
if (emailContent && emailContent.body.length > 10) {
emailElement.setAttribute('data-spam-checked', 'true');
// Check for spam
const spamResult = await this.checkEmailSpam(emailContent);
if (spamResult) {
this.addSpamIndicator(emailElement, spamResult);
}
// Small delay to avoid overwhelming the API
await new Promise(resolve => setTimeout(resolve, 100));
}
}
}
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
new GmailSpamDetector();
});
} else {
new GmailSpamDetector();
}
// Also initialize when navigating in Gmail (single-page app)
window.addEventListener('hashchange', () => {
setTimeout(() => {
new GmailSpamDetector();
}, 2000);
});