-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathprivateserver.js
More file actions
444 lines (390 loc) · 17.1 KB
/
Copy pathprivateserver.js
File metadata and controls
444 lines (390 loc) · 17.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import { observeElement, observeAttributes } from '../../core/observer.js';
import { getPlaceIdFromUrl } from '../../core/idExtractor.js';
import { getAuthenticatedUserId } from '../../core/user.js';
import { createButton } from '../../core/ui/buttons.js';
import { callRobloxApi } from '../../core/api.js';
import { enhanceServer } from '../../core/games/servers/serverdetails.js';
import { loadDatacenterMap, serverIpMap } from '../../core/regions.js';
import { t, ts } from '../../core/locale/i18n.js';
import DOMPurify from 'dompurify';
const privateServerContext = {
serverLocations: {},
serverUptimes: {},
serverPerformanceCache: {},
vipStatusCache: {},
uptimeBatch: new Set(),
serverIpMap: {},
processUptimeBatch: async () => {},
};
const vipServerDetailsCache = new Map();
export async function getVipServerDetails(vipServerId) {
if (vipServerDetailsCache.has(vipServerId))
return vipServerDetailsCache.get(vipServerId);
try {
const res = await callRobloxApi({
subdomain: 'games',
endpoint: `/v1/vip-servers/${vipServerId}`,
});
const data = res.ok ? await res.json() : null;
vipServerDetailsCache.set(vipServerId, data);
return data;
} catch {
vipServerDetailsCache.set(vipServerId, null);
return null;
}
}
export async function init() {
const userId = await getAuthenticatedUserId();
if (!userId) return;
try {
await loadDatacenterMap();
privateServerContext.serverIpMap = serverIpMap;
} catch (e) {}
chrome.storage.local.get(
{ PrivateQuickLinkCopy: true, ServerlistmodificationsEnabled: true },
(settings) => {
const enableControls = settings.PrivateQuickLinkCopy;
const enableDetails = settings.ServerlistmodificationsEnabled;
observeElement(
'.rbx-private-game-server-item',
(serverItem) => {
if (serverItem.dataset.rovalraPrivateEnhanced) return;
serverItem.dataset.rovalraPrivateEnhanced = 'true';
const detailsDiv = serverItem.querySelector(
'.rbx-private-game-server-details',
);
const ownerLink = serverItem.querySelector(
'.rbx-private-owner .avatar-card-fullbody',
);
if (!ownerLink) return;
if (enableDetails) {
enhanceServer(serverItem, privateServerContext);
}
const href = ownerLink.getAttribute('href');
if (!href) return;
const match = href.match(/users\/(\d+)\/profile/);
if (!match) return;
const ownerId = parseInt(match[1], 10);
if (ownerId === userId && enableControls) {
if (serverItem.dataset.privateServerId) {
addOwnerControls(
serverItem,
serverItem.dataset.privateServerId,
);
} else {
const observer =
observeAttributes(serverItem, () => {
if (serverItem.dataset.privateServerId) {
observer.disconnect();
addOwnerControls(
serverItem,
serverItem.dataset.privateServerId,
);
}
}, ['data-private-server-id']);
}
}
},
{ multiple: true },
);
observeElement(
'.flex.items-center.justify-between.padding-y-medium.width-full',
(el) => {
const section = el.closest(
'[data-rovalra-section-type="private"]',
);
if (!section || el.dataset.rovalraPrivateEnhanced) return;
el.dataset.rovalraPrivateEnhanced = 'true';
const check = () => {
const isOwner =
el.getAttribute('data-rovalra-is-owner') === 'true';
const privateServerId = el.getAttribute(
'data-private-server-id',
);
if (privateServerId) {
if (isOwner && enableControls) {
const btnContainer =
el.querySelector(
'.flex.items-center.gap-small.grow-0.shrink-0.basis-auto',
) ||
el.querySelector(
'.flex.flex-col.items-center.gap-xsmall.grow-0.shrink-0.basis-auto',
);
if (btnContainer)
addModernPrivateServerControls(
el,
btnContainer,
getPlaceIdFromUrl(),
);
}
return true;
}
return false;
};
if (!check()) {
const observer = observeAttributes(el, () => {
if (check()) observer.disconnect();
}, ['data-rovalra-is-owner', 'data-private-server-id']);
}
},
{ multiple: true },
);
},
);
}
async function addOwnerControls(serverItem, privateServerId) {
const detailsDiv = serverItem.querySelector(
'.rbx-private-game-server-details',
);
if (
!detailsDiv ||
detailsDiv.querySelector('.rovalra-private-server-controls')
)
return;
if (
serverItem.querySelector('.rbx-private-game-server-copy-link') ||
serverItem.querySelector('.rbx-private-game-server-regenerate-link')
)
return;
let initialData = null;
try {
const res = await callRobloxApi({
subdomain: 'games',
endpoint: `/v1/vip-servers/${privateServerId}`,
method: 'GET',
});
if (res.ok) {
initialData = await res.json();
}
} catch (e) {
console.warn(e);
}
if (initialData?.subscription?.expired) return;
const container = document.createElement('div');
container.className = 'rovalra-private-server-controls';
container.style.marginTop = '5px';
container.style.display = 'flex';
container.style.gap = '5px';
const copyLinkBtn = createButton(
await t('quickPlay.copyLink'),
'secondary',
);
copyLinkBtn.classList.add('btn-control-xs');
copyLinkBtn.style.flex = '1';
copyLinkBtn.style.fontSize = '11px';
copyLinkBtn.style.minWidth = '0';
const generateLinkBtn = createButton(
await t('privateServerPage.regenerateLink'),
'secondary',
);
generateLinkBtn.classList.add('btn-control-xs');
generateLinkBtn.style.flex = '1';
generateLinkBtn.style.fontSize = '11px';
generateLinkBtn.style.minWidth = '0';
container.appendChild(copyLinkBtn);
container.appendChild(generateLinkBtn);
const joinBtnSpan = detailsDiv.querySelector('span[data-placeid]');
if (joinBtnSpan) {
joinBtnSpan.after(container);
} else {
detailsDiv.appendChild(container);
}
if (initialData) {
copyLinkBtn.disabled = !initialData.link;
copyLinkBtn.style.opacity = copyLinkBtn.disabled ? '0.5' : '1';
if (initialData.active === false) {
generateLinkBtn.disabled = true;
}
}
const checkLink = async () => {
try {
const res = await callRobloxApi({
subdomain: 'games',
endpoint: `/v1/vip-servers/${privateServerId}`,
method: 'GET',
});
if (res.ok) {
const data = await res.json();
copyLinkBtn.disabled = !data.link;
copyLinkBtn.style.opacity = copyLinkBtn.disabled ? '0.5' : '1';
if (data.active === false) {
generateLinkBtn.disabled = true;
}
return data.link;
}
} catch (e) {
console.warn(e);
}
return null;
};
copyLinkBtn.onclick = async () => {
if (copyLinkBtn.disabled) return;
const originalText = copyLinkBtn.textContent;
const link = await checkLink();
if (link) {
navigator.clipboard.writeText(link);
copyLinkBtn.textContent = await t('quickPlay.copied');
} else {
copyLinkBtn.textContent = await t('quickPlay.error');
}
setTimeout(() => (copyLinkBtn.textContent = originalText), 1500);
};
generateLinkBtn.onclick = async () => {
const originalText = generateLinkBtn.textContent;
generateLinkBtn.disabled = true;
try {
const res = await callRobloxApi({
subdomain: 'games',
endpoint: `/v1/vip-servers/${privateServerId}`,
method: 'PATCH',
body: { newJoinCode: true },
});
if (res.ok) {
generateLinkBtn.textContent = await t(
'privateServerPage.regenerated',
);
copyLinkBtn.disabled = false;
copyLinkBtn.style.opacity = '1';
} else {
generateLinkBtn.textContent = await t('quickPlay.error');
}
} catch (e) {
generateLinkBtn.textContent = await t('quickPlay.error');
}
setTimeout(() => {
generateLinkBtn.textContent = originalText;
generateLinkBtn.disabled = false;
}, 1500);
};
}
export async function addModernPrivateServerControls(
el,
btnContainer,
placeId,
) {
const privateServerId = el.getAttribute('data-private-server-id');
if (!privateServerId) return;
const isOwner = el.getAttribute('data-rovalra-is-owner') === 'true';
if (!isOwner) return;
const details = await getVipServerDetails(privateServerId);
if (!details || details.subscription?.expired) return;
btnContainer.className =
'flex flex-col items-center gap-xsmall grow-0 shrink-0 basis-auto';
const nativeJoinBtn = btnContainer.querySelector('button');
if (nativeJoinBtn) {
nativeJoinBtn.setAttribute('data-rovalra-join-button', 'true');
}
const configureBtn = btnContainer.querySelector(
'a.foundation-web-icon-button[aria-label="Configure"]',
);
const nativeJoinBtnWrapper = nativeJoinBtn
? nativeJoinBtn.closest('div')
: null;
if (configureBtn && nativeJoinBtnWrapper) {
const configureJoinRow = document.createElement('div');
configureJoinRow.className = 'flex flex-row items-center gap-xsmall';
configureJoinRow.style.cssText =
'width: 200px; min-width: 200px; max-width: 200px;';
configureBtn.remove();
nativeJoinBtnWrapper.remove();
nativeJoinBtnWrapper.style.cssText =
'width: 163px !important; min-width: 163px !important; max-width: 163px !important;';
nativeJoinBtn.style.cssText =
'width: 100% !important; min-width: 100% !important;';
configureJoinRow.appendChild(configureBtn);
configureJoinRow.appendChild(nativeJoinBtnWrapper);
btnContainer.prepend(configureJoinRow);
}
const buttonsRow = document.createElement('div');
buttonsRow.className = 'flex flex-row';
buttonsRow.style.cssText =
'width: 200px; min-width: 200px; max-width: 200px; gap: 5px;';
const shareBtnWrapper = document.createElement('div');
shareBtnWrapper.className = 'rovalra-share-btn-container';
shareBtnWrapper.style.cssText =
'width: 95px; min-width: 95px; max-width: 95px;';
shareBtnWrapper.innerHTML = DOMPurify.sanitize(`
<button type="button" ${!details.link ? 'disabled' : ''} style="opacity: ${!details.link ? '0.5' : '1'}" class="foundation-web-button relative clip group/interactable focus-visible:outline-focus disabled:outline-none cursor-pointer relative flex items-center justify-center stroke-none padding-y-none select-none radius-medium text-label-small height-800 padding-x-small bg-action-standard content-action-standard width-full rovalra-share-btn">
<div role="presentation" class="absolute inset-[0] transition-colors group-hover/interactable:bg-[var(--color-state-hover)] group-active/interactable:bg-[var(--color-state-press)] group-disabled/interactable:bg-none"></div>
<span class="flex items-center min-width-0 gap-xsmall">
<span class="padding-y-xsmall text-truncate-end text-no-wrap">${ts('quickPlay.copyLink', { defaultValue: 'Copy Link' })}</span>
</span>
</button>
`);
const shareBtn = shareBtnWrapper.querySelector('button');
shareBtn.onclick = async (e) => {
e.stopPropagation();
const currentDetails = await getVipServerDetails(privateServerId);
if (currentDetails?.link) {
navigator.clipboard
.writeText(currentDetails.link)
.then(async () => {
const span = shareBtn.querySelector('.text-no-wrap');
const originalText = span.textContent;
span.textContent = await t('serverList.copied', {
defaultValue: 'Copied!',
});
setTimeout(() => {
span.textContent = originalText;
}, 1000);
});
}
};
const generateLinkWrapper = document.createElement('div');
generateLinkWrapper.className = 'rovalra-generate-link-container';
generateLinkWrapper.style.cssText =
'width: 100px; min-width: 100px; max-width: 100px;';
generateLinkWrapper.innerHTML = DOMPurify.sanitize(`
<button type="button" class="foundation-web-button relative clip group/interactable focus-visible:outline-focus disabled:outline-none cursor-pointer relative flex items-center justify-center stroke-none padding-y-none select-none radius-medium text-label-small height-800 padding-x-small bg-action-standard content-action-standard width-full rovalra-generate-link-btn">
<div role="presentation" class="absolute inset-[0] transition-colors group-hover/interactable:bg-[var(--color-state-hover)] group-active/interactable:bg-[var(--color-state-press)] group-disabled/interactable:bg-none"></div>
<span class="flex items-center min-width-0 gap-xsmall">
<span class="padding-y-xsmall text-truncate-end text-no-wrap">${ts('privateServerPage.regenerateLink', { defaultValue: 'Regenerate Link' })}</span>
</span>
</button>
`);
const generateLinkBtn = generateLinkWrapper.querySelector('button');
generateLinkBtn.onclick = async (e) => {
e.stopPropagation();
const span = generateLinkBtn.querySelector('.text-no-wrap');
const originalText = span.textContent;
generateLinkBtn.disabled = true;
try {
const response = await callRobloxApi({
subdomain: 'games',
endpoint: `/v1/vip-servers/${privateServerId}`,
method: 'PATCH',
body: { newJoinCode: true },
});
if (response.ok) {
vipServerDetailsCache.delete(privateServerId);
const newDetails = await getVipServerDetails(privateServerId);
if (newDetails?.accessCode) {
el.setAttribute('data-access-code', newDetails.accessCode);
}
const currentShareBtn =
btnContainer.querySelector('.rovalra-share-btn');
if (currentShareBtn) {
currentShareBtn.disabled = !newDetails?.link;
currentShareBtn.style.opacity = currentShareBtn.disabled
? '0.5'
: '1';
}
span.textContent = await t('privateServerPage.regenerated', {
defaultValue: 'Regenerated!',
});
} else {
span.textContent = await t('quickPlay.error');
}
} catch (error) {
console.error('Error regenerating private server link:', error);
span.textContent = await t('quickPlay.error');
}
setTimeout(() => {
span.textContent = originalText;
generateLinkBtn.disabled = false;
}, 1000);
};
buttonsRow.appendChild(generateLinkWrapper);
buttonsRow.appendChild(shareBtnWrapper);
btnContainer.appendChild(buttonsRow);
}