mirror of
https://github.com/MCKero6423/fuwari.git
synced 2026-10-02 03:15:35 +00:00
feat:尝试修复AI摘要bug
This commit is contained in:
1 parent
efc4e6dc1c
commit
d68fe46acb
1 file changed
+67
-77
@@ -138,86 +138,76 @@ const jsonLd = {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="hongmo-ai-container mt-4">
|
<div class="hongmo-ai-container mt-4">
|
||||||
<link rel="stylesheet" href="https://img.mckero.com/tianli_gpt.min.css">
|
<link rel="stylesheet" href="https://ai.zhheo.com/static/public/tianli_gpt.min.css">
|
||||||
|
|
||||||
<script is:inline>
|
<script is:inline>
|
||||||
function loadHongmoAI() {
|
// 定义核心加载函数
|
||||||
// 检查内容是否存在
|
function loadHongmoAI() {
|
||||||
const contentElement = document.querySelector('.markdown-content');
|
// 1. 找到文章容器
|
||||||
if (!contentElement || !contentElement.textContent.trim()) {
|
const content = document.querySelector('.markdown-content');
|
||||||
console.warn('AI摘要: 内容元素未就绪');
|
if (!content) return;
|
||||||
return;
|
|
||||||
|
// 2.【关键修复】防抽搐:检查是否已经存在摘要?
|
||||||
|
// 如果页面里已经有摘要框了,说明插件正在运行或已完成,直接退出,绝不运行第二次!
|
||||||
|
if (content.querySelector('.post-TianliGPT') || content.querySelector('.tianli-gpt-response')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3.【关键修复】防堆积:清理旧脚本
|
||||||
|
// 这一步至关重要!每次加载前把上一个页面留下的脚本删掉,防止后台积压多个脚本打架
|
||||||
|
const oldScript = document.getElementById('tianli-gpt-script');
|
||||||
|
if (oldScript) { oldScript.remove(); }
|
||||||
|
|
||||||
|
// 4. 重新配置参数
|
||||||
|
window.tianliGPT_postSelector = '.markdown-content';
|
||||||
|
window.tianliGPT_wordLimit = 700;
|
||||||
|
window.tianliGPT_key = 'S-HMJ7UXQGBIAZ0U8Q';
|
||||||
|
|
||||||
|
// 5. 插入新脚本
|
||||||
|
const script = document.createElement('script');
|
||||||
|
script.id = 'tianli-gpt-script'; // 给它个身份证,方便下次删除
|
||||||
|
script.src = 'https://ai.zhheo.com/static/public/tianli_gpt.min.js';
|
||||||
|
script.defer = true;
|
||||||
|
document.body.appendChild(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否已经加载过
|
// 定义触发逻辑:动画监听 + 兜底定时器
|
||||||
if (document.querySelector('.tianli-gpt-response')) {
|
function triggerHongmo() {
|
||||||
console.log('AI摘要: 已存在,跳过');
|
const content = document.querySelector('.markdown-content');
|
||||||
return;
|
if (!content) return;
|
||||||
|
|
||||||
|
// 尝试找到父级动画元素
|
||||||
|
const animated = content.closest('.onload-animation');
|
||||||
|
|
||||||
|
if (animated) {
|
||||||
|
let hasLoaded = false;
|
||||||
|
const start = () => {
|
||||||
|
if (hasLoaded) return;
|
||||||
|
hasLoaded = true;
|
||||||
|
loadHongmoAI();
|
||||||
|
};
|
||||||
|
|
||||||
|
// 保险 A: 监听动画结束事件 (体验最丝滑)
|
||||||
|
animated.addEventListener('animationend', start, { once: true });
|
||||||
|
|
||||||
|
// 保险 B: 兜底定时器 (解决新文章偶尔不加载)
|
||||||
|
// 如果 0.5秒 后动画还没告诉我们可以开始,我们就强制开始,不再傻等
|
||||||
|
setTimeout(start, 500);
|
||||||
|
} else {
|
||||||
|
// 没有动画,直接加载
|
||||||
|
loadHongmoAI();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('AI摘要: 开始加载');
|
// 绑定 Swup 和 页面加载事件
|
||||||
|
document.addEventListener('swup:page:view', triggerHongmo);
|
||||||
// 配置
|
|
||||||
window.tianliGPT_postSelector = '.markdown-content';
|
if (document.readyState === 'loading') {
|
||||||
window.tianliGPT_wordLimit = 700;
|
document.addEventListener('DOMContentLoaded', triggerHongmo);
|
||||||
window.tianliGPT_key = 'S-HMJ7UXQGBIAZ0U8Q';
|
} else {
|
||||||
|
triggerHongmo();
|
||||||
// 动态加载脚本
|
}
|
||||||
const script = document.createElement('script');
|
</script>
|
||||||
script.src = 'https://img.mckero.com/tianli_gpt.min.js';
|
</div>
|
||||||
script.defer = true;
|
|
||||||
script.onload = () => {
|
|
||||||
console.log('AI摘要: 脚本加载成功');
|
|
||||||
};
|
|
||||||
script.onerror = () => {
|
|
||||||
console.error('AI摘要: 脚本加载失败');
|
|
||||||
};
|
|
||||||
document.body.appendChild(script);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 监听自定义的加载事件
|
|
||||||
document.addEventListener("loadHongmoAI", loadHongmoAI, { once: true });
|
|
||||||
</script>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 触发 AI 摘要加载的脚本 -->
|
|
||||||
<script is:inline>
|
|
||||||
// 确保内容完全加载后再触发 AI 摘要加载
|
|
||||||
function triggerHongmoAILoad() {
|
|
||||||
// 等待动画完成
|
|
||||||
const markdownContent = document.querySelector('.markdown-content');
|
|
||||||
if (!markdownContent) {
|
|
||||||
console.warn('AI摘要触发器: 找不到内容元素');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 监听动画结束
|
|
||||||
const animatedElement = markdownContent.closest('.onload-animation');
|
|
||||||
if (animatedElement) {
|
|
||||||
animatedElement.addEventListener('animationend', () => {
|
|
||||||
console.log('AI摘要触发器: 动画完成,触发加载事件');
|
|
||||||
document.dispatchEvent(new Event('loadHongmoAI'));
|
|
||||||
}, { once: true });
|
|
||||||
} else {
|
|
||||||
// 如果没有动画,直接触发
|
|
||||||
console.log('AI摘要触发器: 无动画,直接触发加载事件');
|
|
||||||
document.dispatchEvent(new Event('loadHongmoAI'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 页面加载时触发
|
|
||||||
if (document.readyState === 'loading') {
|
|
||||||
document.addEventListener('DOMContentLoaded', triggerHongmoAILoad);
|
|
||||||
} else {
|
|
||||||
triggerHongmoAILoad();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swup 页面切换时触发
|
|
||||||
document.addEventListener('swup:page:view', () => {
|
|
||||||
console.log('AI摘要触发器: Swup 页面切换');
|
|
||||||
triggerHongmoAILoad();
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</MainGridLayout>
|
</MainGridLayout>
|
||||||
Reference in new issue
Block a user