UNPKG

@lynker-desktop/devtron

Version:

Electron DevTools Extension to track IPC events

393 lines (379 loc) 13.1 kB
/******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ var __webpack_modules__ = ([ /* 0 */, /* 1 */ /***/ ((module) => { module.exports = require("electron"); /***/ }), /* 2 */, /* 3 */, /* 4 */ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ MSG_TYPE: () => (/* binding */ MSG_TYPE), /* harmony export */ PORT_NAME: () => (/* binding */ PORT_NAME) /* harmony export */ }); const PORT_NAME = { PANEL: 'Lynker', CONTENT_SCRIPT: 'lynker-content-script', }; const MSG_TYPE = { PING: 'ping', PONG: 'pong', KEEP_ALIVE: 'keep-alive', GET_ALL_EVENTS: 'get-all-events', RENDER_EVENT: 'render-event', CLEAR_EVENTS: 'clear-events', EVENTS_CLEARED_ACK: 'events-cleared-ack', ADD_IPC_EVENT: 'add-ipc-event', SEND_TO_PANEL: 'send-to-panel', }; /***/ }), /* 5 */, /* 6 */ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ debug: () => (/* binding */ debug), /* harmony export */ debugError: () => (/* binding */ debugError), /* harmony export */ debugGroup: () => (/* binding */ debugGroup), /* harmony export */ debugGroupEnd: () => (/* binding */ debugGroupEnd), /* harmony export */ debugInfo: () => (/* binding */ debugInfo), /* harmony export */ debugLog: () => (/* binding */ debugLog), /* harmony export */ debugMeasure: () => (/* binding */ debugMeasure), /* harmony export */ debugMeasureAsync: () => (/* binding */ debugMeasureAsync), /* harmony export */ debugTable: () => (/* binding */ debugTable), /* harmony export */ debugTime: () => (/* binding */ debugTime), /* harmony export */ debugTimeEnd: () => (/* binding */ debugTimeEnd), /* harmony export */ debugTrace: () => (/* binding */ debugTrace), /* harmony export */ debugWarn: () => (/* binding */ debugWarn), /* harmony export */ debugWhen: () => (/* binding */ debugWhen) /* harmony export */ }); /** * 调试日志工具 * 只有在开启 debug 模式时才会输出日志 */ class DebugLogger { isDebugEnabled = false; /** * 开启调试模式 */ enable() { this.isDebugEnabled = true; this.log('Debug mode enabled'); } /** * 关闭调试模式 */ disable() { this.isDebugEnabled = false; console.log('Debug mode disabled'); } /** * 切换调试模式状态 */ toggle() { if (this.isDebugEnabled) { this.disable(); } else { this.enable(); } } /** * 检查调试模式是否开启 */ isEnabled() { return this.isDebugEnabled; } /** * 输出调试日志 */ log(...args) { if (this.isDebugEnabled) { console.log('[DEBUG]', ...args); } } /** * 输出调试信息 */ info(...args) { if (this.isDebugEnabled) { console.info('[DEBUG INFO]', ...args); } } /** * 输出调试警告 */ warn(...args) { if (this.isDebugEnabled) { console.warn('[DEBUG WARN]', ...args); } } /** * 输出调试错误 */ error(...args) { if (this.isDebugEnabled) { console.error('[DEBUG ERROR]', ...args); } } /** * 输出调试表格 */ table(data) { if (this.isDebugEnabled) { console.table(data); } } /** * 输出调试分组 */ group(label) { if (this.isDebugEnabled) { console.group(`[DEBUG] ${label}`); } } /** * 结束调试分组 */ groupEnd() { if (this.isDebugEnabled) { console.groupEnd(); } } /** * 输出调试时间 */ time(label) { if (this.isDebugEnabled) { console.time(`[DEBUG] ${label}`); } } /** * 结束调试时间 */ timeEnd(label) { if (this.isDebugEnabled) { console.timeEnd(`[DEBUG] ${label}`); } } /** * 输出调试追踪 */ trace(...args) { if (this.isDebugEnabled) { console.trace('[DEBUG TRACE]', ...args); } } /** * 条件调试日志 - 只在满足条件时输出 */ when(condition, ...args) { if (this.isDebugEnabled && condition) { console.log('[DEBUG CONDITIONAL]', ...args); } } /** * 性能调试 - 测量函数执行时间 */ measure(label, fn) { if (!this.isDebugEnabled) { return fn(); } this.time(label); try { const result = fn(); this.timeEnd(label); return result; } catch (error) { this.timeEnd(label); throw error; } } /** * 异步性能调试 - 测量异步函数执行时间 */ async measureAsync(label, fn) { if (!this.isDebugEnabled) { return fn(); } this.time(label); try { const result = await fn(); this.timeEnd(label); return result; } catch (error) { this.timeEnd(label); throw error; } } } // 创建全局调试日志实例 const debug = new DebugLogger(); // 便捷函数 const debugLog = (...args) => debug.log(...args); const debugInfo = (...args) => debug.info(...args); const debugWarn = (...args) => debug.warn(...args); const debugError = (...args) => debug.error(...args); const debugTable = (data) => debug.table(data); const debugGroup = (label) => debug.group(label); const debugGroupEnd = () => debug.groupEnd(); const debugTime = (label) => debug.time(label); const debugTimeEnd = (label) => debug.timeEnd(label); const debugTrace = (...args) => debug.trace(...args); const debugWhen = (condition, ...args) => debug.when(condition, ...args); const debugMeasure = (label, fn) => debug.measure(label, fn); const debugMeasureAsync = (label, fn) => debug.measureAsync(label, fn); /***/ }) /******/ ]); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /* webpack/runtime/compat get default export */ /******/ (() => { /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = (module) => { /******/ var getter = module && module.__esModule ? /******/ () => (module['default']) : /******/ () => (module); /******/ __webpack_require__.d(getter, { a: getter }); /******/ return getter; /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { /******/ // define getter functions for harmony exports /******/ __webpack_require__.d = (exports, definition) => { /******/ for(var key in definition) { /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); /******/ } /******/ } /******/ }; /******/ })(); /******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); /******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports /******/ __webpack_require__.r = (exports) => { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); /******/ /************************************************************************/ var __webpack_exports__ = {}; // This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk. (() => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ monitorRenderer: () => (/* binding */ monitorRenderer) /* harmony export */ }); /* harmony import */ var electron__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1); /* harmony import */ var electron__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(electron__WEBPACK_IMPORTED_MODULE_0__); /* harmony import */ var _common_constants__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(4); /* harmony import */ var _common_debug__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6); function monitorRenderer(options = { isDebug: false }) { if (options?.isDebug) { _common_debug__WEBPACK_IMPORTED_MODULE_2__.debug.enable(); } else { _common_debug__WEBPACK_IMPORTED_MODULE_2__.debug.disable(); } (0,_common_debug__WEBPACK_IMPORTED_MODULE_2__.debugLog)('Devtron - Renderer tracker: Initializing...'); electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.on(_common_constants__WEBPACK_IMPORTED_MODULE_1__.MSG_TYPE.RENDER_EVENT, (_event, data) => { (0,_common_debug__WEBPACK_IMPORTED_MODULE_2__.debugInfo)('Devtron - Renderer tracker: Received RENDER_EVENT from main:', data); track('renderer-to-main', data.channel, data.args); }); const originalOn = electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.on.bind(electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer); const originalOnce = electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.once.bind(electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer); const originalAddListener = electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.addListener.bind(electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer); const originalInvoke = electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.invoke.bind(electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer); electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.invoke = function (channel, ...args) { track('renderer-to-main', channel, args); return originalInvoke(channel, ...args); }; electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.on = function (channel, listener) { return originalOn(channel, (event, ...args) => { track('main-to-renderer', channel, args); listener(event, ...args); }); }; electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.once = function (channel, listener) { return originalOnce(channel, (event, ...args) => { track('main-to-renderer', channel, args); listener(event, ...args); }); }; electron__WEBPACK_IMPORTED_MODULE_0__.ipcRenderer.addListener = function (channel, listener) { return originalAddListener(channel, (event, ...args) => { track('main-to-renderer', channel, args); listener(event, ...args); }); }; } function track(direction, channel, args) { const event = { timestamp: Date.now(), direction, channel, args: copyArgs(args), }; (0,_common_debug__WEBPACK_IMPORTED_MODULE_2__.debugLog)('Devtron - Renderer tracker: Tracking event:', event); const message = { source: _common_constants__WEBPACK_IMPORTED_MODULE_1__.MSG_TYPE.SEND_TO_PANEL, event, }; (0,_common_debug__WEBPACK_IMPORTED_MODULE_2__.debugLog)('Devtron - Renderer tracker: Sending postMessage:', message); window.postMessage(message, '*'); } function copyArgs(args) { try { return structuredClone(args); } catch (err) { const message = err instanceof Error ? err.message : 'Unknown error'; (0,_common_debug__WEBPACK_IMPORTED_MODULE_2__.debugError)(`Error cloning args: ${message}`); return [`[Could not clone args: ${message}]`]; } } })(); module.exports = __webpack_exports__; /******/ })() ;