1(function() { 2const TARGET_WS_HOST = 3 'generativelanguage.googleapis.com'; // Host to intercept 4const originalWebSocket = window.WebSocket; 5 6if (!originalWebSocket) { 7 console.error( 8 '[WebSocketInterceptor] Original window.WebSocket not found. Cannot apply interceptor.'); 9 return; 10} 11 12const handler = { 13 construct(target, args) { 14 let [url, protocols] = args; 15 // stringify url's if necessary for parsing 16 let newUrlString = typeof url === 'string' ? 17 url : 18 (url && typeof url.toString === 'function' ? url.toString() : null); 19 // get ready to check for host to proxy 20 let isTarget = false; 21 22 if (newUrlString) { 23 try { 24 // For full URLs, parse string and check the host 25 if (newUrlString.startsWith('ws://') || 26 newUrlString.startsWith('wss://')) { 27 // URL object again 28 const parsedUrl = new URL(newUrlString); 29 if (parsedUrl.host === TARGET_WS_HOST) { 30 isTarget = true; 31 // Add internal telemetry marker 32 parsedUrl.searchParams.set('__applet_proxy', 'true'); 33 // use wss if https, else ws 34 const proxyScheme = 35 window.location.protocol === 'https:' ? 'wss' : 'ws'; 36 const proxyHost = window.location.host; 37 newUrlString = `${proxyScheme}://${proxyHost}/api-proxy${ 38 parsedUrl.pathname}${parsedUrl.search}`; 39 } 40 } 41 } catch (e) { 42 console.warn( 43 '[WebSocketInterceptor-Proxy] Error parsing WebSocket URL, using original:', 44 url, e); 45 } 46 } else { 47 console.warn( 48 '[WebSocketInterceptor-Proxy] WebSocket URL is not a string or stringifiable. Using original.'); 49 } 50 51 if (isTarget) { 52 console.log('[WebSocketInterceptor-Proxy] Original WebSocket URL:', url); 53 console.log( 54 '[WebSocketInterceptor-Proxy] Redirecting to proxy URL:', 55 newUrlString); 56 } 57 58 // Call the original constructor with potentially modified arguments 59 // Reflect.construct ensures 'new target(...)' behavior and correct 60 // prototype chain 61 if (protocols) { 62 return Reflect.construct(target, [newUrlString, protocols]); 63 } else { 64 return Reflect.construct(target, [newUrlString]); 65 } 66 }, 67 get(target, prop, receiver) { 68 // Forward static property access (e.g., WebSocket.OPEN, 69 // WebSocket.CONNECTING) and prototype access to the original WebSocket 70 // constructor/prototype 71 if (prop === 'prototype') { 72 return target.prototype; 73 } 74 return Reflect.get(target, prop, receiver); 75 } 76}; 77 78window.WebSocket = new Proxy(originalWebSocket, handler); 79 80console.log( 81 '[WebSocketInterceptor-Proxy] Global WebSocket constructor has been wrapped using Proxy.'); 82})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.