-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
346 lines (325 loc) · 12.1 KB
/
background.js
File metadata and controls
346 lines (325 loc) · 12.1 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
// Track tab count and extension state
let tabCount = 0;
let isEnabled = true;
let randomChance = 250; // 1/250 chance by default
let mode = 'roll'; // 'roll', 'interval', 'roulette'
let intervalValue = 50; // Default interval
let rouletteProgress = 0; // Current progress in roulette mode
let rouletteTarget = 0; // Target for roulette mode
let rickrollCount = 0; // Number of times rickroll has occurred
let intervalProgressVisible = true; // Show interval progress bar in popup
let chanceManualEnabled = false;
let intervalManualEnabled = false;
let rouletteManualEnabled = false;
let accent1 = undefined;
let accent2 = undefined;
// Config and update metadata
let configJson = null; // Persisted combined config/stats JSON
let remoteConfigUrl = 'https://raw.githubusercontent.com/GXboy12345/5rick/main/config.json';
let lastConfigCheck = 0;
// Initialize from storage
chrome.storage.local.get(['tabCount', 'isEnabled', 'randomChance', 'mode', 'intervalValue', 'rouletteProgress', 'rouletteTarget', 'rickrollCount', 'intervalProgressVisible', 'chanceManualEnabled', 'intervalManualEnabled', 'rouletteManualEnabled', 'accent1', 'accent2', 'configJson', 'remoteConfigUrl', 'lastConfigCheck'], (result) => {
tabCount = result.tabCount || 0;
isEnabled = result.isEnabled !== undefined ? result.isEnabled : true;
randomChance = result.randomChance || 250;
mode = result.mode || 'roll';
intervalValue = result.intervalValue || 50;
rouletteProgress = result.rouletteProgress || 0;
rouletteTarget = result.rouletteTarget || 0;
rickrollCount = result.rickrollCount || 0;
intervalProgressVisible = result.intervalProgressVisible !== false;
chanceManualEnabled = !!result.chanceManualEnabled;
intervalManualEnabled = !!result.intervalManualEnabled;
rouletteManualEnabled = !!result.rouletteManualEnabled;
accent1 = result.accent1;
accent2 = result.accent2;
configJson = result.configJson || null;
if (typeof result.remoteConfigUrl === 'string' && result.remoteConfigUrl.trim().length > 0) {
remoteConfigUrl = result.remoteConfigUrl.trim();
}
lastConfigCheck = result.lastConfigCheck || 0;
// Initialize roulette target if not set
if (mode === 'roulette' && rouletteTarget === 0) {
rouletteTarget = Math.floor(Math.random() * intervalValue) + 1;
chrome.storage.local.set({ rouletteTarget });
}
updateBadge();
// Initialize config JSON if missing, else apply persisted settings
if (configJson) {
try {
applyConfigJson(configJson, { save: false });
} catch (e) {
console.warn('Failed to apply stored configJson:', e);
}
} else {
saveConfigJson();
}
});
// Build combined config/stats JSON
function buildConfigJson() {
const manifest = chrome.runtime.getManifest();
const extensionVersion = manifest && manifest.version ? manifest.version : '1.0.0';
return {
version: extensionVersion,
updatedAt: Date.now(),
settings: {
isEnabled,
randomChance,
mode,
intervalValue,
intervalProgressVisible,
chanceManualEnabled,
intervalManualEnabled,
rouletteManualEnabled,
accent1,
accent2
},
stats: {
tabCount,
rouletteProgress,
rouletteTarget,
rickrollCount
}
};
}
function saveConfigJson() {
configJson = buildConfigJson();
chrome.storage.local.set({ configJson });
}
// Apply config JSON to in-memory state and storage
function applyConfigJson(json, opts) {
const options = opts || { save: true };
try {
if (json && json.settings) {
const s = json.settings;
if (typeof s.isEnabled === 'boolean') isEnabled = s.isEnabled;
if (Number.isInteger(s.randomChance)) randomChance = s.randomChance;
if (typeof s.mode === 'string') mode = s.mode;
if (Number.isInteger(s.intervalValue)) intervalValue = s.intervalValue;
if (typeof s.intervalProgressVisible === 'boolean') intervalProgressVisible = s.intervalProgressVisible;
if (typeof s.chanceManualEnabled === 'boolean') chanceManualEnabled = s.chanceManualEnabled;
if (typeof s.intervalManualEnabled === 'boolean') intervalManualEnabled = s.intervalManualEnabled;
if (typeof s.rouletteManualEnabled === 'boolean') rouletteManualEnabled = s.rouletteManualEnabled;
if (typeof s.accent1 === 'string') accent1 = s.accent1;
if (typeof s.accent2 === 'string') accent2 = s.accent2;
}
if (json && json.stats) {
const st = json.stats;
if (Number.isInteger(st.tabCount)) tabCount = st.tabCount;
if (Number.isInteger(st.rouletteProgress)) rouletteProgress = st.rouletteProgress;
if (Number.isInteger(st.rouletteTarget)) rouletteTarget = st.rouletteTarget;
if (Number.isInteger(st.rickrollCount)) rickrollCount = st.rickrollCount;
}
if (options.save) {
chrome.storage.local.set({
isEnabled,
randomChance,
mode,
intervalValue,
intervalProgressVisible,
chanceManualEnabled,
intervalManualEnabled,
rouletteManualEnabled,
accent1,
accent2,
tabCount,
rouletteProgress,
rouletteTarget,
rickrollCount
});
saveConfigJson();
updateBadge();
}
} catch (e) {
console.warn('applyConfigJson error:', e);
}
}
function compareVersions(a, b) {
const pa = String(a || '').split('.').map(n => parseInt(n) || 0);
const pb = String(b || '').split('.').map(n => parseInt(n) || 0);
const len = Math.max(pa.length, pb.length);
for (let i = 0; i < len; i++) {
const da = pa[i] || 0;
const db = pb[i] || 0;
if (da > db) return 1;
if (da < db) return -1;
}
return 0;
}
async function checkForRemoteConfigUpdates() {
try {
const resp = await fetch(remoteConfigUrl, { cache: 'no-cache' });
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const remote = await resp.json();
const local = configJson || buildConfigJson();
const remoteVersion = remote && remote.version ? remote.version : '0.0.0';
const localVersion = local && local.version ? local.version : '0.0.0';
if (compareVersions(remoteVersion, localVersion) > 0) {
// Merge: apply remote settings; keep local stats by default
const merged = {
version: remote.version || localVersion,
updatedAt: Date.now(),
settings: Object.assign({}, local.settings, remote.settings || {}),
stats: Object.assign({}, local.stats) // keep stats
};
applyConfigJson(merged);
}
} catch (e) {
console.warn('Remote config check failed:', e);
} finally {
lastConfigCheck = Date.now();
chrome.storage.local.set({ lastConfigCheck });
}
}
// Schedule periodic checks
chrome.runtime.onInstalled.addListener(() => {
chrome.alarms.create('configUpdateAlarm', { periodInMinutes: 180 });
// Do an initial delayed check
setTimeout(() => { checkForRemoteConfigUpdates(); }, 5000);
});
chrome.runtime.onStartup.addListener(() => {
chrome.alarms.create('configUpdateAlarm', { periodInMinutes: 180 });
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm && alarm.name === 'configUpdateAlarm') {
checkForRemoteConfigUpdates();
}
});
function updateBadge() {
const badgeText = isEnabled ? 'ON' : 'OFF';
chrome.action.setBadgeText({ text: badgeText });
chrome.action.setBadgeBackgroundColor({ color: isEnabled ? '#34C759' : '#FF3B30' });
const modeLabel = mode === 'roll' ? 'Roll' : mode === 'interval' ? 'Interval' : 'Roulette';
const title = `Rickroll Extension — ${isEnabled ? 'Enabled' : 'Disabled'} (${modeLabel})`;
chrome.action.setTitle({ title });
}
// Listen for tab creation
chrome.tabs.onCreated.addListener((tab) => {
if (!isEnabled) return;
tabCount++;
chrome.storage.local.set({ tabCount: tabCount });
saveConfigJson();
let shouldRickroll = false;
if (mode === 'roll') {
// Random chance to rickroll (1/randomChance)
shouldRickroll = Math.floor(Math.random() * randomChance) === 0;
} else if (mode === 'interval') {
// Fixed interval rickroll
shouldRickroll = tabCount % intervalValue === 0;
} else if (mode === 'roulette') {
// Roulette mode - progress towards target
rouletteProgress++;
chrome.storage.local.set({ rouletteProgress });
saveConfigJson();
shouldRickroll = rouletteProgress === rouletteTarget;
// Reset for next cycle if rickroll occurred
if (shouldRickroll) {
rouletteProgress = 0;
rouletteTarget = Math.floor(Math.random() * intervalValue) + 1;
chrome.storage.local.set({ rouletteProgress, rouletteTarget });
}
}
if (shouldRickroll) {
rickrollCount++;
chrome.storage.local.set({ rickrollCount });
saveConfigJson();
// Wait a moment for the tab to fully load
setTimeout(() => {
chrome.tabs.update(tab.id, {
url: chrome.runtime.getURL('rickroll.html')
});
}, 100);
}
});
// Listen for messages from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'getState') {
sendResponse({
tabCount,
isEnabled,
randomChance,
mode,
intervalValue,
rouletteProgress,
rouletteTarget,
rickrollCount,
intervalProgressVisible,
chanceManualEnabled,
intervalManualEnabled,
rouletteManualEnabled,
accent1,
accent2
});
} else if (request.action === 'toggleEnabled') {
isEnabled = request.isEnabled;
chrome.storage.local.set({ isEnabled });
updateBadge();
saveConfigJson();
sendResponse({ success: true });
} else if (request.action === 'resetCount') {
tabCount = 0;
rouletteProgress = 0;
if (mode === 'roulette') {
rouletteTarget = Math.floor(Math.random() * intervalValue) + 1;
chrome.storage.local.set({ tabCount: 0, rouletteProgress: 0, rouletteTarget });
} else {
chrome.storage.local.set({ tabCount: 0 });
}
saveConfigJson();
sendResponse({ success: true });
} else if (request.action === 'updateChance') {
randomChance = request.randomChance;
chrome.storage.local.set({ randomChance });
saveConfigJson();
sendResponse({ success: true });
} else if (request.action === 'updateMode') {
mode = request.mode;
if (mode === 'roulette') {
rouletteProgress = 0;
rouletteTarget = Math.floor(Math.random() * intervalValue) + 1;
chrome.storage.local.set({ mode, rouletteProgress, rouletteTarget });
} else {
chrome.storage.local.set({ mode });
}
updateBadge();
saveConfigJson();
sendResponse({ success: true });
} else if (request.action === 'updateInterval') {
intervalValue = request.intervalValue;
if (mode === 'roulette') {
rouletteProgress = 0;
rouletteTarget = Math.floor(Math.random() * intervalValue) + 1;
chrome.storage.local.set({ intervalValue, rouletteProgress, rouletteTarget });
} else {
chrome.storage.local.set({ intervalValue });
}
saveConfigJson();
sendResponse({ success: true });
} else if (request.action === 'openRickrollNow') {
chrome.tabs.create({ url: chrome.runtime.getURL('rickroll.html') }, () => {
sendResponse({ success: true });
});
return true; // keep the message channel open for async response
} else if (request.action === 'updateIntervalProgressVisible') {
intervalProgressVisible = !!request.intervalProgressVisible;
chrome.storage.local.set({ intervalProgressVisible });
saveConfigJson();
sendResponse({ success: true });
} else if (request.action === 'setManualToggle') {
const key = request.key;
const value = !!request.value;
if (key === 'chanceManualEnabled') chanceManualEnabled = value;
if (key === 'intervalManualEnabled') intervalManualEnabled = value;
if (key === 'rouletteManualEnabled') rouletteManualEnabled = value;
chrome.storage.local.set({ [key]: value });
saveConfigJson();
sendResponse({ success: true });
} else if (request.action === 'setAccent') {
const key = request.key === 'accent1' ? 'accent1' : 'accent2';
const value = String(request.value || '').trim();
if (key === 'accent1') accent1 = value; else accent2 = value;
chrome.storage.local.set({ [key]: value });
saveConfigJson();
sendResponse({ success: true });
}
});