tools/WeixinBrowserPlugin/URL_CHANGE_DETECTION_ENHANCEMENT.md
本次更新为微信浏览器插件(Senparc.Weixin.AI)添加了智能URL变化检测功能,解决了用户反馈的核心问题:当浮窗被关闭并再次打开时,如果页面URL已变化,iframe应该自动重新加载新URL对应的内容。
// 检查URL是否已变化
hasUrlChanged() {
const currentUrl = window.location.href;
const urlChanged = currentUrl !== this.lastUrl;
return urlChanged;
}
特性:
// 重新加载iframe内容
reloadIframeContent() {
// 构造新的iframe URL
const currentUrl = encodeURIComponent(window.location.href);
const newIframeUrl = `https://sdk.weixin.senparc.com/AiDoc?query=${currentUrl}`;
// 检查是否真的需要重新加载
if (this.lastIframeUrl === newIframeUrl) {
return; // 无需重新加载
}
// 更新iframe的src并显示加载状态
iframe.src = newIframeUrl;
}
特性:
// 带防抖的URL变化检查
debouncedUrlChangeCheck(callback, delay = 300) {
if (this.urlCheckDebounceTimeout) {
clearTimeout(this.urlCheckDebounceTimeout);
}
this.urlCheckDebounceTimeout = setTimeout(() => {
if (this.hasUrlChanged()) {
callback();
}
}, delay);
}
特性:
constructor() {
// ... 现有代码 ...
this.lastUrl = window.location.href; // 记录上次的URL
this.lastIframeUrl = null; // 记录上次iframe的URL
this.urlCheckDebounceTimeout = null; // URL检查防抖计时器
}
浮窗打开时:
openFloatingWindow() {
// 检查URL是否已变化
const urlChanged = this.hasUrlChanged();
if (this.floatingWindow && urlChanged) {
// 重新加载iframe内容
this.reloadIframeContent();
this.updateLastUrl();
}
// ... 继续原有逻辑
}
浮窗关闭时:
closeFloatingWindow() {
// 记录当前URL状态(为下次打开做准备)
this.updateLastUrl();
// ... 继续原有逻辑
}
新浮窗创建时:
// 记录iframe URL和页面URL
this.lastIframeUrl = iframeUrl;
this.updateLastUrl();
test-url-change-detection.js - 自动化测试脚本demo-url-change-feature.html - 功能演示页面URL变化检测测试
hasUrlChanged()方法正确性iframe重新加载测试
浮窗重新打开测试
// 启用调试模式查看详细日志
window.__SENPARC_DEBUG__.enabled = true;
日志类型:
window.runUrlChangeTests() - 运行自动化测试window.testResults - 查看测试结果src/content.js - 主要功能实现这次功能增强显著提升了用户体验,解决了用户反馈的核心痛点。通过智能URL检测和自动iframe重新加载,用户现在可以享受到:
这项增强使得Senparc.Weixin.AI插件在用户体验和技术实现上都达到了新的高度。