// ==UserScript== // @name BattleMetrics ID on Steam // @namespace https://github.com/jxtt-dev // @version 1.0 // @description Get a players Battlemetrics ID from their Steam profile. IDs are not guaranteed, and depend on Atlas API. // @author jxtt-dev // @homepageURL https://github.com/jxtt-dev/bm_id_on_steam // @homepage https://github.com/jxtt-dev/bm_id_on_steam // @match https://steamcommunity.com/id/* // @match https://steamcommunity.com/profiles/* // @grant GM.xmlHttpRequest // @grant GM_addStyle // @connect services.atlasrust.com // @run-at document-idle // ==/UserScript== (function () { 'use strict'; const API_PATH = 'https://services.atlasrust.com/api/public/player/'; const BM_PLAYER_PROFILE_URL = 'https://battlemetrics.com/players/'; // CSS selectors for Steam const STEAM_STATUS_INFO_DIV_CLASS = '.responsive_status_info'; const STEAM_RIGHT_COL_DIV_CLASS = '.profile_rightcol'; // Styles GM_addStyle(` #bm-id-title-wrapper { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; color: white; font-size: 12px; line-height: 16px; } #bm-id-title-wrapper span { margin-right: 5px; } #bm-id-title-wrapper a { color: #0d91ec; } #bm-copy-link-wrapper { position: relative; display: flex; border: 1px solid #2c3337; border-radius: 4px; padding: 7px; margin-top: 8px; } #bm-copy-link-wrapper input { flex-grow: 1; width: 100%; box-sizing: border-box; font-weight: 400; font-size: 12px; line-height: 16px; text-align: center; color: #59666e; padding: 0; background-color: transparent; border: none; outline: none; } #bm-copy-link-wrapper button { border: none; padding: 0; cursor: pointer; width: 16px; height: 16px; margin-left: 6px; background-color: transparent; color: #59666e; } #bm-copy-link-wrapper button:hover { color: #fff; } `); const copyButtonSVG = ` `; // Fetch BattleMetrics ID from Atlas API async function fetchBMId(steamId) { try { const response = await GM.xmlHttpRequest({ method: 'GET', url: `${API_PATH}${steamId}`, }); if (response.status !== 200) { return null; } const data = JSON.parse(response.responseText); return data?.player?.bm_player_id ?? null; } catch (error) { console.error(`Failed to fetch BattleMetrics ID:`, error); return null; } } async function renderBMInfo(profileData) { const steamId = profileData?.steamid; if (!steamId) { console.error('Steam ID not found in profile data'); return; } // Fetch BM ID from Atlas API const bmId = await fetchBMId(steamId); if (bmId === null) { console.log('Unable to get BM ID from API'); return; } const bmProfileUrl = `${BM_PLAYER_PROFILE_URL}${bmId}`; // Create BattleMetrics ID title const bmIdWrapper = document.createElement('div'); bmIdWrapper.id = 'bm-id-wrapper'; const bmIdTitle = document.createElement('div'); bmIdTitle.id = 'bm-id-title-wrapper'; bmIdTitle.innerHTML = ` BattleMetrics ID ${bmId} `; // Create BattleMetrics copy input const bmCopyLinkWrapper = document.createElement('div'); bmCopyLinkWrapper.id = 'bm-copy-link-wrapper'; bmCopyLinkWrapper.innerHTML = ` `; // Add title and copy input to BattleMetrics ID wrapper bmIdWrapper.appendChild(bmIdTitle); bmIdWrapper.appendChild(bmCopyLinkWrapper); // Insert BattleMetrics ID wrapper onto page const statusInfoDiv = document.querySelector( STEAM_STATUS_INFO_DIV_CLASS ); const rightColDiv = document.querySelector(STEAM_RIGHT_COL_DIV_CLASS); if (rightColDiv === null) { console.error('Unable to get Steam profile right column'); return; } if (statusInfoDiv) { statusInfoDiv.after(bmIdWrapper); } else { rightColDiv.appendChild(bmIdWrapper); } // Add event listener to copy button const copyBMIdButton = document.getElementById('bm-copy-link-button'); if (copyBMIdButton) { copyBMIdButton.addEventListener('click', () => { navigator.clipboard.writeText(bmProfileUrl).catch((err) => { console.error('Failed to copy BM ID: ', err); }); }); } } // Wait for the window to fully load to ensure the profile data is defined window.addEventListener('load', async function () { if (typeof unsafeWindow.g_rgProfileData !== 'undefined') { const profileData = unsafeWindow.g_rgProfileData; try { await renderBMInfo(profileData); } catch (error) { console.error('Failed to display BattleMetrics ID:', error); } } else { console.error('Unable to get Steam profile data'); } }); })();