# Getting Chrome Extension User ID in Any Tab This guide shows you how to access the Chrome extension user ID from any web page or tab. ## Method 1: Automatic Content Script (Recommended) The content script automatically injects the user ID into every page. You can access it in several ways: ### A. Global Window Variable ```javascript // Check if user ID is available if (window.chromeExtensionUserId) { console.log('User ID:', window.chromeExtensionUserId); } else { console.log('User ID not available yet'); } ``` ### B. Session Storage ```javascript // Get user ID from session storage const userId = sessionStorage.getItem('chromeExtensionUserId'); if (userId) { console.log('User ID from storage:', userId); } ``` ### C. Event Listener (Best for Dynamic Loading) ```javascript // Listen for user ID ready event window.addEventListener('chromeExtensionUserIdReady', function(event) { const userId = event.detail.userId; console.log('User ID received:', userId); // Your code here }); // Also check if it's already available if (window.chromeExtensionUserId) { console.log('User ID already available:', window.chromeExtensionUserId); } ``` ## Method 2: User ID Helper API If the automatic injection doesn't work, you can use the helper API: ### A. Simple Promise-based Access ```javascript // Get user ID asynchronously window.getChromeExtensionUserId().then(userId => { if (userId) { console.log('User ID:', userId); // Your code here } else { console.log('No user ID available'); } }); ``` ### B. Synchronous Access (if already loaded) ```javascript // Get user ID synchronously (only if already available) const userId = window.getChromeExtensionUserIdSync(); if (userId) { console.log('User ID (sync):', userId); } ``` ### C. Callback-based Access ```javascript // Execute callback when user ID becomes available window.ChromeExtensionUserID.onUserIdReady(function(userId) { console.log('User ID ready:', userId); // Your code here }); ``` ## Method 3: Manual Injection You can manually inject the user ID helper into any tab: ### From Extension Popup or Background Script ```javascript // Inject into current active tab chrome.runtime.sendMessage({ type: 'injectUserIdHelper' }, (response) => { if (response.success) { console.log('User ID helper injected:', response.message); } else { console.error('Failed to inject:', response.error); } }); // Inject into specific tab chrome.runtime.sendMessage({ type: 'injectUserIdHelper', tabId: 123 }, (response) => { console.log('Injection result:', response); }); ``` ## Complete Example Here's a complete example for any web page: ```html