-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt.js
More file actions
287 lines (247 loc) · 10.5 KB
/
prompt.js
File metadata and controls
287 lines (247 loc) · 10.5 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
// Parse query parameters
const params = new URLSearchParams(window.location.search);
const intendedUrl = params.get('url');
const cooldownVal = params.get('cooldown');
const msgVal = params.get('msg');
// Use getDomain helper for consistency with background.js
const hostname = intendedUrl ? new URL(intendedUrl).hostname : 'Unknown';
// Replace state to avoid adding to history if possible, or at least mark this entry
if (window.history.replaceState) {
window.history.replaceState(null, '', window.location.href);
}
function getDomain(url) {
try {
const hostname = new URL(url).hostname;
return hostname.replace(/^(www\.|m\.|mobile\.)/, '');
} catch (e) {
return null;
}
}
document.getElementById('target-site-display').textContent = `Accessing: ${hostname}`;
// Main Logic: Check status immediately
init();
async function init() {
const data = await chrome.storage.local.get(['cooldowns', 'inputDelay', 'extensionDuration']);
// Normalize domain to match storage key
const domain = intendedUrl ? getDomain(intendedUrl) : (hostname !== 'Unknown' ? getDomain('http://' + hostname) : null);
if (!domain) {
setupNormalUI();
return;
}
const now = Date.now();
// Check Cooldown in Storage (Priority UI check)
if (data.cooldowns && data.cooldowns[domain]) {
// New structure: { startTime, duration }
// Fallback for migration if old number exists
let endTime;
if (typeof data.cooldowns[domain] === 'number') {
endTime = data.cooldowns[domain];
} else {
const { startTime, duration } = data.cooldowns[domain];
endTime = startTime + duration;
}
if (endTime > now) {
const delay = data.inputDelay || 0;
const extDuration = data.extensionDuration !== undefined ? data.extensionDuration : 30;
showCooldownUI(endTime, data.cooldowns[domain], delay, extDuration);
return;
}
}
// If URL param says cooldown but storage doesn't (weird sync issue), trust params or storage? storage is source of truth.
// If param says cooldown=5 but storage says expired, we should probably check storage.
// But for responsiveness, let's trust storage.
// If NO cooldown in storage, proceed to normal UI.
if (msgVal) {
document.getElementById('error-msg').textContent = decodeURIComponent(msgVal);
}
const delay = data.inputDelay || 0;
// Check if we already have an active session for this domain
// If we do, and it's still valid, redirect immediately (handles 'back' button into prompt)
if (data.activeSessions && data.activeSessions[domain]) {
const session = data.activeSessions[domain];
const now = Date.now();
if (session.type === 'duration' && session.endTime > now) {
window.location.replace(intendedUrl);
return;
}
else if (session.type === 'count' && (!session.cooldownEndTime || session.cooldownEndTime < now)) {
// For count, if not in cooldown, it's technically 'active'
// However, the background script manages the limit.
// If the user backed into here, we should probably let them through if they haven't hit the limit.
if ((session.videosWatched || 0) < session.targetCount) {
window.location.replace(intendedUrl);
return;
}
}
}
setupNormalUI(delay);
}
function isSpecificContent(url) {
if (!url) return false;
try {
const u = new URL(url);
if (u.hostname.includes('youtube.com') || u.hostname.includes('youtu.be')) {
if (u.pathname.startsWith('/shorts/')) return true;
if (u.searchParams.get('v')) return true;
}
if (u.hostname.includes('reddit.com')) {
if (u.pathname.match(/\/r\/[\w-]+\/comments\/[\w]+\/?/)) return true;
}
} catch(e) {}
return false;
}
function showCooldownUI(endTime, cooldownInfo, delay = 0, extensionDuration = 30) {
const minutesLeft = Math.ceil((endTime - Date.now()) / 60000);
const canExtend = cooldownInfo.originalType === 'duration' && extensionDuration > 0;
const canFinish = isSpecificContent(intendedUrl);
const extendLabel = `Use for ${extensionDuration}s`;
const finishLabel = 'Finish Video/Post';
let bypassHtml = '';
if (canExtend || canFinish) {
bypassHtml += `
<div class="extension-section" style="margin-top: 20px; padding-top: 20px; border-top: 1px solid #333;">
`;
if (canExtend) {
bypassHtml += `
<button id="extend-btn" disabled style="background-color: #03dac6; color: #000; padding: 10px 20px; border: none; border-radius: 8px; font-weight: bold; cursor: pointer; margin-bottom: 15px; width: 100%;">
${delay > 0 ? `Wait ${delay}...` : extendLabel}
</button>
`;
}
if (canFinish) {
bypassHtml += `
<button id="finish-btn" disabled style="background-color: #bb86fc; color: #000; padding: 10px 20px; border: none; border-radius: 8px; font-weight: bold; cursor: pointer; margin-bottom: 15px; width: 100%;">
${delay > 0 ? `Wait ${delay}...` : finishLabel}
</button>
`;
}
bypassHtml += `</div>`;
}
document.body.innerHTML = `
<div class="container" style="max-width: 400px;">
<h1 style="color: #cf6679;">Cooldown Active</h1>
<p>You cannot access ${hostname} for another <span id="cd-timer">${minutesLeft}</span> minutes.</p>
<p class="small-text">Go do something else!</p>
${bypassHtml}
</div>
` + '<link rel="stylesheet" href="prompt.css">';
const extendBtn = canExtend ? document.getElementById('extend-btn') : null;
const finishBtn = canFinish ? document.getElementById('finish-btn') : null;
if (delay > 0) {
let timeLeft = delay;
const timer = setInterval(() => {
timeLeft--;
if (timeLeft <= 0) {
clearInterval(timer);
if (extendBtn) {
extendBtn.disabled = false;
extendBtn.textContent = extendLabel;
}
if (finishBtn) {
finishBtn.disabled = false;
finishBtn.textContent = finishLabel;
}
} else {
if (extendBtn) extendBtn.textContent = `Wait ${timeLeft}...`;
if (finishBtn) finishBtn.textContent = `Wait ${timeLeft}...`;
}
}, 1000);
} else {
if (extendBtn) extendBtn.disabled = false;
if (finishBtn) finishBtn.disabled = false;
}
if (extendBtn) {
extendBtn.addEventListener('click', () => {
startSession('duration', extensionDuration / 60); // convert seconds to minutes
});
}
if (finishBtn) {
finishBtn.addEventListener('click', () => {
startSession('single_url', intendedUrl);
});
}
}
function setupNormalUI(delay = 0) {
// show 'Count' only if it is youtube
if (hostname.includes('youtube.com')) {
document.getElementById('count-btn').style.display = 'inline-block';
}
// Default existing setup
document.getElementById('view-count').innerHTML = `
<label for="count-input">Enter number of videos:</label>
<input type="number" id="count-input" min="1" placeholder="e.g. 3">
`;
setupTypeSwitching();
// updateUnlimitedStatus(); // Removed
const confirmBtn = document.getElementById('confirm-btn');
confirmBtn.addEventListener('click', handleConfirm);
if (delay > 0) {
const inputs = document.querySelectorAll('input, button.type-btn');
inputs.forEach(el => el.disabled = true);
confirmBtn.disabled = true;
// Also disable any inputs created above (specifically count-input)
document.getElementById('count-input').disabled = true;
let timeLeft = delay;
confirmBtn.textContent = `Wait ${timeLeft}...`;
const timer = setInterval(() => {
timeLeft--;
if (timeLeft <= 0) {
clearInterval(timer);
inputs.forEach(el => el.disabled = false);
document.getElementById('count-input').disabled = false;
confirmBtn.disabled = false;
confirmBtn.textContent = "Continue to Site";
} else {
confirmBtn.textContent = `Wait ${timeLeft}...`;
}
}, 1000);
}
}
function setupTypeSwitching() {
document.querySelectorAll('.type-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.type-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
document.querySelectorAll('.view').forEach(v => v.classList.remove('active-view'));
const type = btn.getAttribute('data-type');
document.getElementById(`view-${type}`).classList.add('active-view');
window.selectedType = type;
});
});
window.selectedType = 'duration'; // default
document.getElementById('view-duration').classList.add('active-view');
}
function handleConfirm() {
const errorDiv = document.getElementById('error-msg');
errorDiv.textContent = '';
const selectedType = window.selectedType || 'duration';
if (selectedType === 'duration') {
const minutes = parseInt(document.getElementById('duration-input').value, 10);
if (!minutes || minutes <= 0) {
errorDiv.textContent = "Please enter a valid positive duration.";
return;
}
startSession('duration', minutes);
} else if (selectedType === 'count') {
const count = parseInt(document.getElementById('count-input').value, 10);
if (!count || count <= 0) {
errorDiv.textContent = "Please enter a valid positive number of videos.";
return;
}
startSession('count', count);
}
}
function startSession(type, value) {
chrome.runtime.sendMessage({
action: 'startSession',
url: intendedUrl,
type: type,
value: value
}, (response) => {
if (response && response.success) {
window.location.replace(intendedUrl);
} else {
document.getElementById('error-msg').textContent = response.error || "Failed to start session.";
}
});
}