diff --git a/public/Assets/locales/en.json b/public/Assets/locales/en.json index 91241eac9..c01d6c38d 100644 --- a/public/Assets/locales/en.json +++ b/public/Assets/locales/en.json @@ -1680,6 +1680,10 @@ "playerCountUnknownTooltip": "Player count is unknown, this is a Roblox limitation. The thumbnails are not representative of the people in game.", "playerCountUnknownLabel": "Player count unknown" }, + "rejoinServer": { + "tooltip": "Rejoin previous server", + "closed": "Your previous server is no longer available." + }, "recentServers": { "title": "Recent Servers", "refresh": "Refresh", diff --git a/src/content/core/settings/settingConfig.js b/src/content/core/settings/settingConfig.js index 4ef06a8ce..b0c7548eb 100644 --- a/src/content/core/settings/settingConfig.js +++ b/src/content/core/settings/settingConfig.js @@ -699,6 +699,17 @@ export const SETTINGS_CONFIG = { type: 'checkbox', default: true, storageKey: 'rovalra_server_history', + childSettings: { + rejoinServerButtonEnabled: { + label: 'Rejoin Previous Server Button', + description: [ + 'Adds a button next to the Play button that joins the last server you were in for that experience.', + ], + type: 'checkbox', + default: true, + contributors: ['4975603156'], + }, + }, }, TotalServersEnabled: { label: 'Total Servers', diff --git a/src/content/features/games/rejoinServerButton.js b/src/content/features/games/rejoinServerButton.js new file mode 100644 index 000000000..6df72546c --- /dev/null +++ b/src/content/features/games/rejoinServerButton.js @@ -0,0 +1,124 @@ +import { observeElement, observeResize } from '../../core/observer.js'; +import { getPlaceIdFromUrl } from '../../core/idExtractor.js'; +import { settings } from '../../core/settings/getSettings.js'; +import { launchGame } from '../../core/utils/launcher.js'; +import { addTooltip } from '../../core/ui/tooltip.js'; +import { showSystemAlert } from '../../core/ui/roblox/alert.js'; +import { ts } from '../../core/locale/i18n.js'; +import { checkServerIsActive } from './serverlist/recentservers.js'; + +const CONTAINER_SELECTOR = '#game-details-play-button-container'; +const PLAY_BUTTON_SELECTOR = 'button[data-testid="play-button"]'; +const BUTTON_ID = 'rovalra-rejoin-server-button'; +const ICON_PATH = + 'M13 3a9 9 0 0 0-9 9H1l3.89 3.89.07.14L9 12H6c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.93 0-3.68-.79-4.94-2.06l-1.42 1.42A8.954 8.954 0 0 0 13 21a9 9 0 0 0 0-18m-1 5v5l4.28 2.54.72-1.21-3.5-2.08V8z'; + +let listening = false; +let processing = false; + +async function getLastServer(placeId) { + const { rovalra_server_history: history = {} } = + await chrome.storage.local.get({ rovalra_server_history: {} }); + return history[placeId]?.[0]?.presence || null; +} + +function createIcon() { + const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + svg.setAttribute('width', '26'); + svg.setAttribute('height', '26'); + svg.setAttribute('viewBox', '0 0 24 24'); + const path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); + path.setAttribute('d', ICON_PATH); + svg.append(path); + return svg; +} + +async function rejoin(button, placeId) { + if (button.disabled) return; + button.disabled = true; + try { + const server = await getLastServer(placeId); + if ( + !server?.gameId || + !(await checkServerIsActive(placeId, server.gameId)) + ) { + showSystemAlert(ts('rejoinServer.closed'), 'warning'); + return; + } + launchGame(server.rootPlaceId || placeId, server.gameId); + } finally { + button.disabled = false; + } +} + +async function processContainer(container) { + const placeId = getPlaceIdFromUrl(); + if (!placeId || !container.querySelector(PLAY_BUTTON_SELECTOR)) return; + if (processing) return; + + processing = true; + try { + await renderButton(container, placeId); + } finally { + processing = false; + } +} + +async function renderButton(container, placeId) { + const server = await getLastServer(placeId); + const existing = document.getElementById(BUTTON_ID); + if (!server?.gameId) { + existing?.remove(); + return; + } + if (existing) return; + + const button = document.createElement('button'); + button.type = 'button'; + button.id = BUTTON_ID; + button.className = 'btn-primary-md rovalra-rejoin-server-button'; + button.setAttribute('aria-label', ts('rejoinServer.tooltip')); + button.append(createIcon()); + button.addEventListener('click', (event) => { + event.stopPropagation(); + rejoin(button, placeId); + }); + + container.append(button); + const playButton = container.querySelector(PLAY_BUTTON_SELECTOR); + const syncSize = () => { + const { height } = playButton.getBoundingClientRect(); + if (height > 0) + button.style.width = button.style.height = `${height}px`; + }; + syncSize(); + observeResize(playButton, syncSize); + addTooltip(button, ts('rejoinServer.tooltip'), { position: 'top' }); +} + +export async function init() { + if ( + !(await settings.recentServersEnabled) || + (await settings.rejoinServerButtonEnabled) === false + ) { + return; + } + + observeElement(CONTAINER_SELECTOR, processContainer); + observeElement( + `${CONTAINER_SELECTOR} button`, + (button) => { + const container = button.closest(CONTAINER_SELECTOR); + if (container) processContainer(container); + }, + { multiple: true }, + ); + + if (listening) return; + listening = true; + chrome.storage.onChanged.addListener((changes, namespace) => { + if (namespace !== 'local' || !changes.rovalra_server_history) return; + const container = document.querySelector(CONTAINER_SELECTOR); + if (container) processContainer(container); + }); +} diff --git a/src/content/features/games/serverlist/recentservers.js b/src/content/features/games/serverlist/recentservers.js index 0bee26a3e..6b3d4bc8b 100644 --- a/src/content/features/games/serverlist/recentservers.js +++ b/src/content/features/games/serverlist/recentservers.js @@ -159,7 +159,7 @@ function createModernServerItem(serverData, userThumbnailUrl, userId) { return serverItem; } -async function checkServerIsActive(placeId, gameId) { +export async function checkServerIsActive(placeId, gameId) { try { const info = await callRobloxApiJson({ subdomain: 'gamejoin', diff --git a/src/content/index.js b/src/content/index.js index 41dbd93a1..e090f7754 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -117,6 +117,7 @@ import { init as initFreeBadgeQuota } from './features/games/freeBadgeQuota.js'; import { init as initServerList } from './features/games/serverlist/serverlist.js'; import { initRecentServers } from './features/games/serverlist/recentservers.js'; import { init as initRegionPlayButton } from './features/games/RegionPlayButton.js'; +import { init as initRejoinServerButton } from './features/games/rejoinServerButton.js'; import { init as initSubplaces } from './features/games/tab/Subplaces.js'; import { initServerIdExtraction } from './core/games/servers/serverids.js'; import { init as initGameTrailers } from './features/games/thumbnails/gametrailers.js'; @@ -419,6 +420,7 @@ const featureRoutes = [ initBotDetector, initServerList, initRegionPlayButton, + initRejoinServerButton, bannertest, initGameTrailers, quickOutfits, diff --git a/src/css/features/games/rejoin_server_button.scss b/src/css/features/games/rejoin_server_button.scss new file mode 100644 index 000000000..6eeaab473 --- /dev/null +++ b/src/css/features/games/rejoin_server_button.scss @@ -0,0 +1,23 @@ +.rovalra-rejoin-server-button { + display: flex !important; + align-items: center; + justify-content: center; + flex-shrink: 0; + margin-left: 5px !important; + padding: 0 !important; + border: none !important; + border-radius: 12px; + background-color: var(--rovalra-playbutton-color) !important; + color: white !important; + cursor: pointer; + order: 6 !important; + + svg path { + fill: white; + } + + &:disabled { + opacity: 0.6; + cursor: default; + } +} diff --git a/src/css/main.scss b/src/css/main.scss index afdbf02fd..45438152a 100644 --- a/src/css/main.scss +++ b/src/css/main.scss @@ -94,3 +94,4 @@ @use 'features/avatar/game_outfits.scss'; @use 'components/dropdown.scss'; @use 'components/user_card_tooltip.scss'; +@use 'features/games/rejoin_server_button.scss';