修复访客浏览轨迹延迟与排序,增加演示站
保留 SPA hash 以正确区分页面;延迟采集 title 避免慢一页;轨迹改为最新在上;补充 demo-shop 测试页与 Vite 静态路由。
This commit is contained in:
+25
-7
@@ -30,8 +30,9 @@
|
||||
|
||||
var open = false;
|
||||
var iframe = null;
|
||||
var lastSentURL = '';
|
||||
var lastSentKey = '';
|
||||
var lastSentAt = 0;
|
||||
var routeTimer = null;
|
||||
|
||||
var btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
@@ -82,8 +83,10 @@
|
||||
if (!iframe || !iframe.contentWindow) return;
|
||||
var info = hostPageInfo();
|
||||
var now = Date.now();
|
||||
if (!force && info.url === lastSentURL && now - lastSentAt < 800) return;
|
||||
lastSentURL = info.url;
|
||||
// url + title 一起去重:SPA 常先改 hash 再改 title,只比 url 会丢掉正确标题
|
||||
var key = info.url + '\0' + info.title;
|
||||
if (!force && key === lastSentKey && now - lastSentAt < 500) return;
|
||||
lastSentKey = key;
|
||||
lastSentAt = now;
|
||||
try {
|
||||
iframe.contentWindow.postMessage({
|
||||
@@ -95,6 +98,21 @@
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由变化后延迟采集:让宿主 SPA(含 demo-shop)先跑完 render、更新 document.title,
|
||||
* 避免轨迹标题永远慢一页(URL 已是新页、title 仍是上一页)。
|
||||
*/
|
||||
function schedulePostPage(force) {
|
||||
if (!open && !force) return;
|
||||
if (routeTimer) clearTimeout(routeTimer);
|
||||
routeTimer = setTimeout(function () {
|
||||
routeTimer = null;
|
||||
postPageToIframe(!!force);
|
||||
// 再补一帧:部分框架 title 在 paint 后才写
|
||||
setTimeout(function () { postPageToIframe(false); }, 50);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
function ensureIframe() {
|
||||
if (iframe) return;
|
||||
iframe = document.createElement('iframe');
|
||||
@@ -103,7 +121,7 @@
|
||||
iframe.style.cssText = 'width:100%;height:100%;border:0;display:block;background:#fff;';
|
||||
iframe.src = buildEmbedURL();
|
||||
iframe.addEventListener('load', function () {
|
||||
postPageToIframe(true);
|
||||
schedulePostPage(true);
|
||||
});
|
||||
panel.appendChild(iframe);
|
||||
}
|
||||
@@ -114,7 +132,7 @@
|
||||
ensureIframe();
|
||||
panel.style.display = 'block';
|
||||
btn.style.display = 'none';
|
||||
postPageToIframe(true);
|
||||
schedulePostPage(true);
|
||||
} else {
|
||||
panel.style.display = 'none';
|
||||
btn.style.display = 'flex';
|
||||
@@ -130,14 +148,14 @@
|
||||
}
|
||||
// iframe 就绪后可再次同步宿主页
|
||||
if (event.data.type === 'kefu-widget-ready') {
|
||||
postPageToIframe(true);
|
||||
schedulePostPage(true);
|
||||
}
|
||||
});
|
||||
|
||||
// —— SPA / 浏览器导航监听 ——
|
||||
function onRouteMaybeChanged() {
|
||||
if (!open) return;
|
||||
postPageToIframe(false);
|
||||
schedulePostPage(false);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user