2024-08-25 16:08:59 +08:00
|
|
|
if (window.innerWidth > 640) { // 自动展开导航栏
|
2024-08-11 21:10:42 +08:00
|
|
|
document.getElementById("toc-drawer").setAttribute('open', true);
|
2024-08-11 15:02:16 +08:00
|
|
|
}
|
2024-08-11 21:10:42 +08:00
|
|
|
|
|
|
|
function prevPage() {
|
|
|
|
var link = document.getElementById("helper_prevpage").href;
|
|
|
|
|
|
|
|
console.log("prev page:", link);
|
|
|
|
changePage(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextPage() {
|
|
|
|
var link = document.getElementById("helper_nextpage").href;
|
|
|
|
|
|
|
|
console.log("next page:", link);
|
|
|
|
changePage(link);
|
|
|
|
}
|
|
|
|
|
2024-08-25 16:08:59 +08:00
|
|
|
function setPage(url) { // 菜单跳转事件
|
2024-08-11 21:10:42 +08:00
|
|
|
if (url === "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("set page:", url);
|
|
|
|
changePage(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
function changePage(url) {
|
2024-08-25 16:08:59 +08:00
|
|
|
// 加载图标
|
2024-08-11 21:10:42 +08:00
|
|
|
var nextbtn = document.getElementsByClassName("next—page")[0]
|
|
|
|
if (nextbtn != undefined) {
|
|
|
|
nextbtn.setAttribute("loading", "");
|
|
|
|
}
|
2024-08-25 16:08:59 +08:00
|
|
|
|
|
|
|
// 发送 XHR 动态更新
|
2024-08-11 21:10:42 +08:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = () => {
|
2024-08-25 16:08:59 +08:00
|
|
|
if (xhr.readyState !== 4) return; // 加载失败
|
2024-08-11 21:10:42 +08:00
|
|
|
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
|
|
|
|
|
2024-08-25 16:08:59 +08:00
|
|
|
// 滚动到顶部
|
2024-08-11 21:10:42 +08:00
|
|
|
document.documentElement.scrollTop = 0;
|
|
|
|
document.body.scrollTop = 0;
|
|
|
|
|
2024-08-25 16:08:59 +08:00
|
|
|
// 更新内容与翻页按钮
|
2024-08-11 21:10:42 +08:00
|
|
|
var old_tag = document.getElementsByClassName("mdui-prose")[0];
|
2024-08-12 14:46:11 +08:00
|
|
|
var old_topbar_tag = document.getElementsByClassName("navbtn")[0];
|
2024-08-12 09:09:16 +08:00
|
|
|
var old_navcard_tag = document.getElementsByClassName("navcard")[0];
|
2024-08-11 21:10:42 +08:00
|
|
|
var virtualtag = document.createElement('div');
|
|
|
|
virtualtag.insertAdjacentHTML('beforeend', xhr.responseText);
|
|
|
|
var title = virtualtag.getElementsByTagName("title")[0].innerText;
|
|
|
|
old_tag.innerHTML = virtualtag.getElementsByClassName("mdui-prose")[0].innerHTML;
|
2024-08-12 14:46:11 +08:00
|
|
|
old_topbar_tag.innerHTML = virtualtag.getElementsByClassName("navbtn")[0].innerHTML;
|
2024-08-12 09:09:16 +08:00
|
|
|
old_navcard_tag.innerHTML = virtualtag.getElementsByClassName("navcard")[0].innerHTML;
|
2024-08-11 21:10:42 +08:00
|
|
|
|
2024-08-25 16:08:59 +08:00
|
|
|
// 更新导航栏当前选择的内容
|
2024-08-11 21:10:42 +08:00
|
|
|
var elements = document.querySelectorAll('[active]');
|
|
|
|
elements.forEach(element => {
|
|
|
|
element.removeAttribute('active');
|
|
|
|
});
|
|
|
|
var elementshighliht = document.querySelectorAll('[search_link="' + url + '"]');
|
|
|
|
elementshighliht.forEach(element => {
|
|
|
|
element.setAttribute('active', "true");
|
|
|
|
});
|
|
|
|
|
2024-08-25 16:08:59 +08:00
|
|
|
// 清除临时标签
|
2024-08-11 21:10:42 +08:00
|
|
|
virtualtag.remove();
|
2024-08-25 16:08:59 +08:00
|
|
|
|
|
|
|
// 添加历史记录
|
2024-08-11 21:10:42 +08:00
|
|
|
history.pushState({ page: title }, title, url);
|
|
|
|
|
2024-08-25 16:08:59 +08:00
|
|
|
// 刷新高亮
|
|
|
|
hljs.highlightAll();
|
2024-08-11 21:10:42 +08:00
|
|
|
}
|
|
|
|
};
|
2024-08-25 16:08:59 +08:00
|
|
|
// 发送 XHR
|
2024-08-11 21:10:42 +08:00
|
|
|
xhr.open('GET', url, true);
|
|
|
|
xhr.send(null);
|
|
|
|
}
|
|
|
|
|
2024-08-25 16:08:59 +08:00
|
|
|
// 当回到上一页时
|
2024-08-11 21:10:42 +08:00
|
|
|
window.addEventListener('popstate', function (event) {
|
|
|
|
console.log('pop state:', window.location.protocol + "//" + window.location.host + location.pathname);
|
|
|
|
changePage(window.location.protocol + "//" + window.location.host + location.pathname);
|
|
|
|
});
|