var _InAppWebViewReady = false; var enableScrollSpy = true; var titleList = []; var currentTitle = '_top'; var titleOffset = 10; 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 (!enableScrollSpy || !titleList || titleList.length === 0) return true; var offsetTop = (navigator.safeArea ? navigator.safeArea.top : 0) + titleOffset; 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 }); window.MugenApp = { scrollToTitle: function (anchor) { var el = document.getElementById(anchor); if (el) { var scrollTop = window.scrollY + el.getBoundingClientRect().top; if (navigator.safeArea) { scrollTop -= navigator.safeArea.top + titleOffset; } scrollTop = Math.max(0, scrollTop); enableScrollSpy = false; window.scrollTo({ top: scrollTop, }); setTimeout(function() { enableScrollSpy = true; }, 50); } }, };