UNPKG

@steambrew/client

Version:
74 lines (73 loc) 2.79 kB
const m_private_context = undefined; export const pluginSelf = m_private_context; class MillenniumChromeDevToolsProtocol { constructor() { this.devTools = window.MILLENNIUM_CHROME_DEV_TOOLS_PROTOCOL_DO_NOT_USE_OR_OVERRIDE_ONMESSAGE; this.currentId = 0; this.pendingRequests = new Map(); // Override the onmessage callback to handle responses this.devTools.onmessage = (message) => { this.handleMessage(message); }; } handleMessage(message) { const data = typeof message === 'string' ? JSON.parse(message) : message; // Check if this is a response to one of our requests if (data.id !== undefined && this.pendingRequests.has(data.id)) { const pending = this.pendingRequests.get(data.id); this.pendingRequests.delete(data.id); if (pending) { const { resolve, reject } = pending; if (data.error) { reject(new Error(`CDP Error: ${data.error.message} (${data.error.code})`)); } else { resolve(data.result); } } } } send(method, params = {}, sessionId) { return new Promise((resolve, reject) => { const id = this.currentId++; // Store the promise resolvers this.pendingRequests.set(id, { resolve, reject }); // Prepare the message const message = { id: id, method: method, }; // Only add params if they're provided and not empty if (params && Object.keys(params).length > 0) { message.params = params; } // If a sessionId is provided, include it in the message if (sessionId) { message.sessionId = sessionId; } // Send the message try { this.devTools.send(JSON.stringify(message)); } catch (error) { // Clean up if send fails this.pendingRequests.delete(id); reject(error); } }); } // Helper method to send without waiting for response (fire and forget) sendNoResponse(method, params = {}) { const message = { id: this.currentId++, method: method, }; if (params && Object.keys(params).length > 0) { message.params = params; } this.devTools.send(JSON.stringify(message)); } } const ChromeDevToolsProtocol = new MillenniumChromeDevToolsProtocol(); const Millennium = window.Millennium; export { ChromeDevToolsProtocol, Millennium, callable, BindPluginSettings, constSysfsExpr };