-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
112 lines (98 loc) · 4.08 KB
/
Copy pathcontent.js
File metadata and controls
112 lines (98 loc) · 4.08 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
'use strict';
/**
* TelegramGuard Content Script (UTF-8)
* Active ONLY on web.telegram.org. Tracks time and messages background.js.
*/
// Centralize state tracking to match background service worker precisely
let isBlocked = false;
// CSS Injection for full-screen blocking UI
const style = document.createElement('style');
style.textContent = `
#tg-guard-blocker-overlay {
position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
background: radial-gradient(circle at center, #0a1428 0%, #050a14 100%);
z-index: 2147483647; display: flex; align-items: center; justify-content: center;
color: white; font-family: 'Segoe UI', Roboto, sans-serif; text-align: center;
}
.blocker-card {
background: rgba(255, 255, 255, 0.03);
backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 24px; padding: 40px; max-width: 400px;
box-shadow: 0 20px 50px rgba(0,0,0,0.5), 0 0 20px rgba(16, 185, 129, 0.15);
}
.btn-premium {
background: linear-gradient(135deg, #10b981 0%, #047857 100%); color: white;
border: 1px solid rgba(255, 255, 255, 0.1); padding: 12px 30px;
border-radius: 14px; font-weight: 700; cursor: pointer; transition: 0.3s;
font-size: 1rem; margin-top: 25px;
text-shadow: 0 1px 2px rgba(0,0,0,0.3);
box-shadow: 0 4px 15px rgba(16, 185, 129, 0.2);
}
.btn-premium:hover { transform: translateY(-2px); box-shadow: 0 0 25px rgba(16, 185, 129, 0.5); }
.icon-lock { font-size: 4rem; margin-bottom: 20px; display: block; filter: drop-shadow(0 0 10px #fbbf24); }
.modal-title { font-size: 2rem; color: #fbbf24; margin: 0 0 10px; font-weight: 800; }
.modal-subtitle { opacity: 0.7; font-size: 0.95rem; margin: 0 0 15px; }
.no-scroll { overflow: hidden !important; }
`;
document.head.appendChild(style);
// Main Blocking and Extension Enforcement Loop
async function enforceLimits() {
const data = await chrome.storage.local.get(['usageToday', 'dailyLimit', 'isActive', 'extendUsed']);
if (!data.isActive) {
removeBlocker();
return;
}
// Decimal math comparison (minutes decimal)
const isDailyOver = (data.usageToday || 0) >= (data.dailyLimit || 30);
if (isDailyOver) {
showBlocker("Limit Reached", `Used your ${data.dailyLimit} min today.`, !data.extendUsed);
} else {
removeBlocker();
}
}
function showBlocker(title, sub, allowExtend) {
if (isBlocked) return;
isBlocked = true;
const overlay = document.createElement('div');
overlay.id = 'tg-guard-blocker-overlay';
overlay.innerHTML = `
<div class="blocker-card">
<span class="icon-lock">⌛</span> <h2 class="modal-title">${title}</h2>
<p class="modal-subtitle">${sub}</p>
${allowExtend ? '<button class="btn-premium" id="extend-btn">Extend 5 Minutes</button>' : '<p style="color: #fbbf24; font-weight: 600;">Daily limit strictly enforced.</p>'}
</div>
`;
document.body.appendChild(overlay);
document.body.classList.add('no-scroll'); // Premium UX: stop scrolling the background page
if (allowExtend) {
document.getElementById('extend-btn').onclick = async () => {
const d = await chrome.storage.local.get(['dailyLimit']);
const newLimit = (d.dailyLimit || 30) + 5;
await chrome.storage.local.set({ dailyLimit: newLimit, extendUsed: true });
location.reload(); // Refresh the page to clear blockers immediately
};
}
}
function removeBlocker() {
const existingOverlay = document.getElementById('tg-guard-blocker-overlay');
if(existingOverlay) {
existingOverlay.remove();
document.body.classList.remove('no-scroll');
isBlocked = false;
}
}
// Real-Time Time Tracking Loop
// Important: Check visibility to ensure we only track active time.
if (location.href.includes('web.telegram.org')) {
setInterval(async () => {
if (document.visibilityState === 'visible') {
// 1. Send centralized ticking message
try {
await chrome.runtime.sendMessage({ type: "TICK" });
} catch(e) { /* ignore messaging errors when popup closes */ }
// 2. Run blocking check loop
enforceLimits();
}
}, 1000);
}