You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.6 KiB
JavaScript

var _InAppWebViewReady = false;
var titleList = [];
var currentTitle = '_top';
function onReadyStateChange() {
if (_InAppWebViewReady && document.readyState === "complete") {
titleList = document.querySelectorAll('.mw-parser-output h1, .mw-parser-output h2, .mw-parser-output h3, .mw-parser-output h4, .mw-parser-output h5, .mw-parser-output h6');
window.flutter_inappwebview.callHandler('pageLoaded', true);
}
}
window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
_InAppWebViewReady = true;
window.flutter_inappwebview.callHandler('bridgeConnected', true);
onReadyStateChange();
});
document.addEventListener("readystatechange", onReadyStateChange);
var _debouceTimer = null;
function debouce(callback, ms) {
return function() {
var _args = arguments;
if (!_debouceTimer) {
_debouceTimer = setTimeout(function() {
callback.apply(this, _args);
_debouceTimer = null;
}, ms);
}
}
}
document.addEventListener('scroll', debouce(function() {
if (!titleList || titleList.length === 0) return;
var offsetTop = (navigator.safeArea ? navigator.safeArea.top : 0) + 10;
var currentTitleEl = null;
var isFirstSection = false;
for (var i = 0; i < titleList.length; i ++) {
var el = titleList[i];
var pos = el.getBoundingClientRect();
if (pos.top - offsetTop > 0) {
if (i === 0) {
isFirstSection = true;
} else {
currentTitleEl = titleList[i - 1];
}
break;
}
}
if (!currentTitleEl && !isFirstSection) {
currentTitleEl = titleList[titleList.length - 1];
}
var newCurrentTitle;
if (isFirstSection) {
newCurrentTitle = '_firstSection';
} else {
var currentTitleAnchorEl = currentTitleEl.querySelector('.mw-headline');
if (currentTitleAnchorEl) {
newCurrentTitle = currentTitleAnchorEl.id;
}
}
if (newCurrentTitle && newCurrentTitle !== currentTitle) {
currentTitle = newCurrentTitle;
window.flutter_inappwebview.callHandler('sectionChange', newCurrentTitle);
}
}, 200), { passive: true });
var MugenApp = {
scrollToTitle: function (anchor) {
var el = document.getElementById(anchor);
if (el) {
var scrollTop = el.offsetTop;
if (navigator.safeArea) {
scrollTop += navigator.safeArea.top;
}
window.scrollTo({
top: scrollTop,
behavior: "smooth",
});
}
},
};