-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstats.js
121 lines (105 loc) · 3.98 KB
/
stats.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
document.addEventListener('DOMContentLoaded', async () => {
const statsCards = document.getElementById('statsCards');
const postsContainer = document.getElementById('postsContainer');
// Get filtered posts from storage
const { filteredPosts = [] } = await chrome.storage.local.get(['filteredPosts']);
// Filter posts from the last 24 hours and ensure they were actually blocked
const last24Hours = Date.now() - (24 * 60 * 60 * 1000);
const recentPosts = filteredPosts.filter(post =>
post.timestamp > last24Hours &&
post.filterType && // Must have a filter type (meaning it was blocked)
post.scores && // Must have scores
Object.entries(post.scores).some(([type, score]) => {
// Check if the score that triggered the block was actually above threshold
if (type === post.filterType) {
return score >= 0.5; // Using the same threshold as in content.js
}
return false;
})
);
// Calculate statistics
const stats = {
total: recentPosts.length,
cynical: 0,
sarcastic: 0,
threatening: 0,
politics: 0,
racism: 0
};
// Count posts by type (only counting valid blocks)
recentPosts.forEach(post => {
if (post.filterType && post.scores && post.scores[post.filterType] >= 0.5) {
stats[post.filterType]++;
}
});
// Create stats cards
Object.entries(stats).forEach(([key, value]) => {
const card = document.createElement('div');
card.className = 'stat-card';
card.innerHTML = `
<div class="stat-number">${value}</div>
<div class="stat-label">${key === 'total' ? 'Total Blocked' : `${key} Blocked`}</div>
`;
statsCards.appendChild(card);
});
// Display posts
if (recentPosts.length === 0) {
postsContainer.innerHTML = `
<div class="no-posts">
No posts have been blocked in the last 24 hours.
</div>
`;
} else {
// Sort posts by timestamp, most recent first
recentPosts.sort((a, b) => b.timestamp - a.timestamp);
recentPosts.forEach(post => {
const postElement = document.createElement('div');
postElement.className = 'post';
// Format timestamp
const date = new Date(post.timestamp);
const timeAgo = getTimeAgo(date);
// Create scores display
const scoresHtml = Object.entries(post.scores || {})
.map(([key, value]) => `
<div class="score-item">
<span>${key}:</span>
<div class="score-bar">
<div class="score-fill" style="width: ${value * 100}%"></div>
</div>
<span>${(value * 100).toFixed(0)}%</span>
</div>
`).join('');
postElement.innerHTML = `
<div class="post-content">${escapeHtml(post.text)}</div>
<div class="post-scores">${scoresHtml}</div>
`;
postsContainer.appendChild(postElement);
});
}
});
// Helper function to format time ago
function getTimeAgo(date) {
const seconds = Math.floor((Date.now() - date) / 1000);
const intervals = {
year: 31536000,
month: 2592000,
week: 604800,
day: 86400,
hour: 3600,
minute: 60,
second: 1
};
for (const [unit, secondsInUnit] of Object.entries(intervals)) {
const interval = Math.floor(seconds / secondsInUnit);
if (interval >= 1) {
return `${interval} ${unit}${interval === 1 ? '' : 's'} ago`;
}
}
return 'just now';
}
// Helper function to escape HTML
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}