-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescalation-handler.js
More file actions
348 lines (300 loc) · 11.9 KB
/
escalation-handler.js
File metadata and controls
348 lines (300 loc) · 11.9 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Enhanced Escalation Handler for Content Moderation Assistant
class EscalationHandler {
constructor() {
this.config = {
// Configuration options for where escalated content goes
destinations: {
local: true, // Store locally in Chrome storage
clipboard: true, // Copy to clipboard
file: false, // Save to local file
api: false, // Send to external API
email: false, // Send via email
slack: false, // Send to Slack webhook
discord: false // Send to Discord webhook
},
// API configuration (if using external API)
api: {
url: 'https://your-moderation-api.com/escalate',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
}
},
// Email configuration (if using email)
email: {
to: 'moderation@yourcompany.com',
subject: 'Content Escalation Alert'
},
// Slack webhook (if using Slack)
slack: {
webhookUrl: 'https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK'
}
};
}
async handleEscalation(data, sender) {
const escalationData = await this.prepareEscalationData(data, sender);
// Handle each configured destination
const results = {};
if (this.config.destinations.local) {
results.local = await this.storeLocally(escalationData);
}
if (this.config.destinations.clipboard) {
results.clipboard = await this.copyToClipboard(escalationData);
}
if (this.config.destinations.file) {
results.file = await this.saveToFile(escalationData);
}
if (this.config.destinations.api) {
results.api = await this.sendToAPI(escalationData);
}
if (this.config.destinations.email) {
results.email = await this.sendEmail(escalationData);
}
if (this.config.destinations.slack) {
results.slack = await this.sendToSlack(escalationData);
}
return results;
}
async prepareEscalationData(data, sender) {
// Capture screenshot
const screenshot = await this.captureScreenshot(sender.tab.id);
// Get page content
const pageContent = await this.getPageContent(sender.tab.id);
// Get user info if available
const userInfo = await this.extractUserInfo(sender.tab.id);
return {
id: `ESC_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
timestamp: new Date().toISOString(),
action: 'escalate',
severity: data.rule?.severity || 'high',
rule: data.rule,
content: data.content,
url: data.url,
pageTitle: pageContent?.title || document.title,
screenshot: screenshot,
userInfo: userInfo,
moderator: {
browser: navigator.userAgent,
extensionVersion: chrome.runtime.getManifest().version
},
metadata: {
tabId: sender.tab?.id,
windowId: sender.tab?.windowId,
domain: new URL(data.url).hostname
}
};
}
async storeLocally(escalationData) {
try {
const logs = await this.getStoredData('escalationLogs') || [];
logs.push(escalationData);
// Keep only last 1000 escalations
if (logs.length > 1000) {
logs.splice(0, logs.length - 1000);
}
await this.setStoredData('escalationLogs', logs);
return { success: true, message: 'Stored locally' };
} catch (error) {
return { success: false, error: error.message };
}
}
async copyToClipboard(escalationData) {
try {
const report = this.formatEscalationReport(escalationData);
await navigator.clipboard.writeText(report);
return { success: true, message: 'Copied to clipboard' };
} catch (error) {
return { success: false, error: error.message };
}
}
async saveToFile(escalationData) {
try {
const report = this.formatEscalationReport(escalationData);
const blob = new Blob([report], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `escalation_${escalationData.id}.txt`;
a.click();
URL.revokeObjectURL(url);
return { success: true, message: 'Saved to file' };
} catch (error) {
return { success: false, error: error.message };
}
}
async sendToAPI(escalationData) {
try {
const response = await fetch(this.config.api.url, {
method: 'POST',
headers: this.config.api.headers,
body: JSON.stringify(escalationData)
});
if (response.ok) {
const result = await response.json();
return { success: true, message: 'Sent to API', data: result };
} else {
throw new Error(`API request failed: ${response.status}`);
}
} catch (error) {
return { success: false, error: error.message };
}
}
async sendEmail(escalationData) {
try {
const subject = `${this.config.email.subject} - ${escalationData.rule?.name || 'Manual Escalation'}`;
const body = this.formatEscalationReport(escalationData);
const mailtoLink = `mailto:${this.config.email.to}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
// Open email client
window.open(mailtoLink);
return { success: true, message: 'Opened email client' };
} catch (error) {
return { success: false, error: error.message };
}
}
async sendToSlack(escalationData) {
try {
const slackMessage = {
text: `🚨 Content Escalation Alert`,
attachments: [{
color: 'danger',
fields: [
{ title: 'Rule', value: escalationData.rule?.name || 'Manual', short: true },
{ title: 'Severity', value: escalationData.severity, short: true },
{ title: 'URL', value: escalationData.url, short: false },
{ title: 'Content', value: escalationData.content.substring(0, 1000), short: false }
],
footer: 'Content Moderation Assistant',
ts: Math.floor(Date.now() / 1000)
}]
};
const response = await fetch(this.config.slack.webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(slackMessage)
});
if (response.ok) {
return { success: true, message: 'Sent to Slack' };
} else {
throw new Error(`Slack request failed: ${response.status}`);
}
} catch (error) {
return { success: false, error: error.message };
}
}
formatEscalationReport(escalationData) {
return `🚨 CONTENT ESCALATION REPORT
ID: ${escalationData.id}
Timestamp: ${escalationData.timestamp}
Severity: ${escalationData.severity.toUpperCase()}
RULE VIOLATION:
Name: ${escalationData.rule?.name || 'Manual Escalation'}
Severity: ${escalationData.rule?.severity || 'Unknown'}
CONTENT:
${escalationData.content}
PAGE INFORMATION:
URL: ${escalationData.url}
Title: ${escalationData.pageTitle}
Domain: ${escalationData.metadata.domain}
USER INFORMATION:
${escalationData.userInfo ? JSON.stringify(escalationData.userInfo, null, 2) : 'No user information available'}
SCREENSHOT: ${escalationData.screenshot ? 'Available' : 'Not available'}
MODERATOR INFO:
Browser: ${escalationData.moderator.browser}
Extension Version: ${escalationData.moderator.extensionVersion}
---
Generated by Content Moderation Assistant
`;
}
async captureScreenshot(tabId) {
try {
return await chrome.tabs.captureVisibleTab(null, { format: 'png' });
} catch (error) {
console.error('Error capturing screenshot:', error);
return null;
}
}
async getPageContent(tabId) {
try {
// Check if scripting API is available
if (!chrome.scripting || !chrome.scripting.executeScript) {
console.warn('Chrome scripting API not available for page content extraction');
return null;
}
const results = await chrome.scripting.executeScript({
target: { tabId: tabId },
func: () => ({
title: document.title,
url: window.location.href,
textContent: document.body.textContent.substring(0, 1000)
})
});
return results[0]?.result || null;
} catch (error) {
console.error('Error getting page content:', error);
return null;
}
}
async extractUserInfo(tabId) {
try {
// Check if scripting API is available
if (!chrome.scripting || !chrome.scripting.executeScript) {
console.warn('Chrome scripting API not available for user info extraction');
return null;
}
const results = await chrome.scripting.executeScript({
target: { tabId: tabId },
func: () => {
// Look for common user identification patterns
const userSelectors = [
'[data-user-id]', '[data-username]', '.username',
'.user-name', '.author', '.poster', '.user'
];
for (const selector of userSelectors) {
const element = document.querySelector(selector);
if (element) {
return {
username: element.textContent?.trim(),
userId: element.getAttribute('data-user-id'),
element: selector
};
}
}
return null;
}
});
return results[0]?.result || null;
} catch (error) {
console.error('Error extracting user info:', error);
return null;
}
}
async getStoredData(key) {
try {
const result = await chrome.storage.local.get([key]);
return result[key];
} catch (error) {
console.error('Error getting stored data:', error);
return null;
}
}
async setStoredData(key, value) {
try {
await chrome.storage.local.set({ [key]: value });
} catch (error) {
console.error('Error setting stored data:', error);
}
}
// Configuration methods
updateConfig(newConfig) {
this.config = { ...this.config, ...newConfig };
}
getConfig() {
return this.config;
}
}
// Export for use in other scripts
if (typeof module !== 'undefined' && module.exports) {
module.exports = EscalationHandler;
} else {
window.EscalationHandler = EscalationHandler;
}