diff --git a/libs/extension/src/background/index.ts b/libs/extension/src/background/index.ts index 6a8c4591..01e455f8 100644 --- a/libs/extension/src/background/index.ts +++ b/libs/extension/src/background/index.ts @@ -51,6 +51,7 @@ chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { changeSource(message, DevToolBackground); connections[tabId].postMessage(message); } else { + // TODO: 如果查询失败,发送 chrome message,请求 panel 主动建立连接 console.log('Tab not found in connection list.'); } } else { diff --git a/libs/extension/src/injector/index.ts b/libs/extension/src/injector/index.ts index 940f0ab9..73692427 100644 --- a/libs/extension/src/injector/index.ts +++ b/libs/extension/src/injector/index.ts @@ -134,6 +134,7 @@ let helper; function init(horizonHelper) { helper = horizonHelper; + window.__HORIZON_DEV_HOOK__.isInit = true; } function injectHook() { @@ -144,6 +145,7 @@ function injectHook() { enumerable: false, value: { init, + isInit: false, addIfNotInclude, send, deleteVNode, diff --git a/libs/horizon/src/external/devtools.ts b/libs/horizon/src/external/devtools.ts index 38167db5..10e70e80 100644 --- a/libs/horizon/src/external/devtools.ts +++ b/libs/horizon/src/external/devtools.ts @@ -74,7 +74,7 @@ export const helper = { }, }; -function injectUpdater() { +export function injectUpdater() { const hook = window.__HORIZON_DEV_HOOK__; if (hook) { hook.init(helper); diff --git a/libs/horizon/src/renderer/TreeBuilder.ts b/libs/horizon/src/renderer/TreeBuilder.ts index 8fff0435..923b4275 100644 --- a/libs/horizon/src/renderer/TreeBuilder.ts +++ b/libs/horizon/src/renderer/TreeBuilder.ts @@ -36,6 +36,7 @@ import { updateShouldUpdateOfTree } from './vnode/VNodeShouldUpdate'; import { getPathArr } from './utils/vNodePath'; +import { injectUpdater } from '../external/devtools'; // 不可恢复错误 let unrecoverableErrorDuringBuild: any = null; @@ -281,6 +282,11 @@ function renderFromRoot(treeRoot) { if (window.__HORIZON_DEV_HOOK__) { const hook = window.__HORIZON_DEV_HOOK__; + // injector.js 可能在 Horizon 代码之后加载,此时无 __HORIZON_DEV_HOOK__ 全局变量 + // Horizon 代码初次加载时不会初始化 helper + if (!hook.isInit) { + injectUpdater(); + } hook.addIfNotInclude(treeRoot); hook.send(treeRoot); }