PageSourceSearch

https://rokg.ai/public/websocket-interceptor.js

js rokg.ai collected 2026-10-05 14:42:27 UTC 2,652 bytes, 65 lines download raw bytes

1(function() {
2  const TARGET_WS_HOST = 'generativelanguage.googleapis.com'; // Host to intercept
3  const originalWebSocket = window.WebSocket;
4
5  if (!originalWebSocket) {
6    console.error('[WebSocketInterceptor] Original window.WebSocket not found. Cannot apply interceptor.');
7    return;
8  }
9
10  const handler = {
11    construct(target, args) {
12      let [url, protocols] = args;
13      //stringify url's if necessary for parsing
14      let newUrlString = typeof url === 'string' ? url : (url && typeof url.toString === 'function' ? url.toString() : null);
15      //get ready to check for host to proxy
16      let isTarget = false;
17
18      if (newUrlString) {
19        try {
20          // For full URLs, parse string and check the host
21          if (newUrlString.startsWith('ws://') || newUrlString.startsWith('wss://')) {
22            //URL object again
23            const parsedUrl = new URL(newUrlString);
24            if (parsedUrl.host === TARGET_WS_HOST) {
25              isTarget = true;
26              //use wss if https, else ws
27              const proxyScheme = window.location.protocol === 'https:' ? 'wss' : 'ws';
28              const proxyHost = window.location.host;
29              newUrlString = `${proxyScheme}://${proxyHost}/api-proxy${parsedUrl.pathname}${parsedUrl.search}`;
30            }
31          }
32        } catch (e) {
33          console.warn('[WebSocketInterceptor-Proxy] Error parsing WebSocket URL, using original:', url, e);
34        }
35      } else {
36          console.warn('[WebSocketInterceptor-Proxy] WebSocket URL is not a string or stringifiable. Using original.');
37      }
38
39      if (isTarget) {
40        console.log('[WebSocketInterceptor-Proxy] Original WebSocket URL:', url);
41        console.log('[WebSocketInterceptor-Proxy] Redirecting to proxy URL:', newUrlString);
42      }
43
44      // Call the original constructor with potentially modified arguments
45      // Reflect.construct ensures 'new target(...)' behavior and correct prototype chain
46      if (protocols) {
47        return Reflect.construct(target, [newUrlString, protocols]);
48      } else {
49        return Reflect.construct(target, [newUrlString]);
50      }
51    },
52    get(target, prop, receiver) {
53      // Forward static property access (e.g., WebSocket.OPEN, WebSocket.CONNECTING)
54      // and prototype access to the original WebSocket constructor/prototype
55      if (prop === 'prototype') {
56        return target.prototype; 
57      }
58      return Reflect.get(target, prop, receiver);
59    }
60  };
61
62  window.WebSocket = new Proxy(originalWebSocket, handler);
63
64  console.log('[WebSocketInterceptor-Proxy] Global WebSocket constructor has been wrapped using Proxy.');
65})();

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.