Skip to content

Commit

Permalink
变更页面的 URL 检测机制
Browse files Browse the repository at this point in the history
GitHub.com 基于Turbo 机制进行页面切换,当`turbo:before-fetch-response`触发即表示页面进行切换 由于`updatePageConfig`基于<head>中的相关标记 判断页面类型, 当 `turbo:before-fetch-response`触发时<head>并未更新完毕,故不直接在`turbo:before-fetch-response`触发时直接处理页面URL, 而是延迟到第一次突变发生时处理
  • Loading branch information
maboloshi committed Feb 10, 2025
1 parent 9b54f9e commit b0f06b3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions main.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
};

let pageConfig = {};
let firstChangeURL = false;

// 更新页面设置
function updatePageConfig() {
Expand Down Expand Up @@ -114,7 +115,8 @@
if (currentURL !== previousURL) {
previousURL = currentURL;
updatePageConfig();
console.log(`【Debug】DOM变化触发: 链接变化 pageType= ${pageConfig.currentPageType}`);
console.log(`【Debug】页面切换 pageType= ${pageConfig.currentPageType}`);
firstChangeURL = false; // 重置 firstChangeURL
}
}

Expand Down Expand Up @@ -148,9 +150,8 @@

// 监听 document.body 下 DOM 变化,用于处理节点变化
new MutationObserver(mutations => {
handleUrlChange();
if (!pageConfig.currentPageType) return;
processMutations(mutations);
if (firstChangeURL) handleUrlChange();
if (pageConfig.currentPageType) processMutations(mutations);
}).observe(document.body, CONFIG.OBSERVER_CONFIG);
}

Expand Down Expand Up @@ -601,6 +602,11 @@
attributeFilter: ['lang']
});

// 监听 Turbo 获取响应之前事件
document.addEventListener('turbo:before-fetch-response', () => {
firstChangeURL = true; // 页面开始切换前设置为 true
});

// 监听 Turbo 完成事件
document.addEventListener('turbo:load', () => {
if (!pageConfig.currentPageType) return;
Expand Down

0 comments on commit b0f06b3

Please sign in to comment.