From 6dd6e2de5214920c878bc9d3951cfd3e9242c349 Mon Sep 17 00:00:00 2001 From: anghunk Date: Thu, 22 Jan 2026 10:55:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=BC=BA=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=92=8C=E6=8C=82=E8=BD=BD=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E7=9A=84=E5=81=A5=E5=A3=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 处理未定义document或无效元素的情况,避免抛出错误 仅在hostElement存在时执行挂载操作,防止空指针异常 --- docs/widget/src/core/CWDComments.js | 76 ++++++++++++++++------------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/docs/widget/src/core/CWDComments.js b/docs/widget/src/core/CWDComments.js index 45e8e1b..44b3ab9 100644 --- a/docs/widget/src/core/CWDComments.js +++ b/docs/widget/src/core/CWDComments.js @@ -54,17 +54,23 @@ export class CWDComments { * @private */ _resolveElement(el) { + if (typeof document === 'undefined') { + return null; + } + if (!el) { + return null; + } if (typeof el === 'string') { const element = document.querySelector(el); - if (!element) { - throw new Error(`元素未找到:${el}`); - } - if (!(element instanceof HTMLElement)) { - throw new Error(`目标不是 HTMLElement: ${el}`); + if (!element || !(element instanceof HTMLElement)) { + return null; } return element; } - return el; + if (el instanceof HTMLElement) { + return el; + } + return null; } async _loadServerConfig() { @@ -98,27 +104,21 @@ export class CWDComments { if (this._mounted) { return; } - - // 创建 Shadow DOM - this.shadowRoot = this.hostElement.attachShadow({ mode: 'open' }); - - // 注入样式 - const styleElement = document.createElement('style'); - if (typeof styles === 'string') { - styleElement.textContent = styles; - } else if (styles && typeof styles === 'object' && 'default' in styles) { - styleElement.textContent = styles.default; - } - this.shadowRoot.appendChild(styleElement); - - // 创建容器 - this.mountPoint = document.createElement('div'); - this.mountPoint.className = 'cwd-comments-container'; - this.shadowRoot.appendChild(this.mountPoint); - - // 设置主题 - if (this.config.theme) { - this.mountPoint.setAttribute('data-theme', this.config.theme); + if (this.hostElement) { + this.shadowRoot = this.hostElement.attachShadow({ mode: 'open' }); + const styleElement = document.createElement('style'); + if (typeof styles === 'string') { + styleElement.textContent = styles; + } else if (styles && typeof styles === 'object' && 'default' in styles) { + styleElement.textContent = styles.default; + } + this.shadowRoot.appendChild(styleElement); + this.mountPoint = document.createElement('div'); + this.mountPoint.className = 'cwd-comments-container'; + this.shadowRoot.appendChild(this.mountPoint); + if (this.config.theme) { + this.mountPoint.setAttribute('data-theme', this.config.theme); + } } (async () => { @@ -139,11 +139,13 @@ export class CWDComments { }); if (!isAllowed) { - this.mountPoint.innerHTML = ` + if (this.mountPoint) { + this.mountPoint.innerHTML = `
当前域名 (${currentHostname}) 未获得评论组件调用授权
`; + } return; } } @@ -161,14 +163,20 @@ export class CWDComments { const api = createApiClient(this.config); this.api = api; - this.store = createCommentStore(this.config, api.fetchComments.bind(api), api.submitComment.bind(api)); + if (this.hostElement && this.mountPoint) { + this.store = createCommentStore( + this.config, + api.fetchComments.bind(api), + api.submitComment.bind(api) + ); - this.unsubscribe = this.store.store.subscribe((state) => { - this._onStateChange(state); - }); + this.unsubscribe = this.store.store.subscribe((state) => { + this._onStateChange(state); + }); - this._render(); - this.store.loadComments(); + this._render(); + this.store.loadComments(); + } if (this.api && typeof this.api.trackVisit === 'function') { this.api.trackVisit();