150 lines
5.8 KiB
JavaScript
150 lines
5.8 KiB
JavaScript
// 主函数:拦截所有跳转行为并处理
|
|
function interceptRedirects() {
|
|
// 拦截点击和 JavaScript 触发的跳转
|
|
document.addEventListener('click', function (event) {
|
|
if (event.target.tagName === 'A' && event.target.href && !isJavascriptUrl(event.target.href)) {
|
|
event.preventDefault(); // 阻止默认跳转
|
|
handleRedirect(event.target.href); // 处理跳转
|
|
}
|
|
});
|
|
|
|
// 拦截 location.href 或其他形式的直接跳转
|
|
const originalPushState = history.pushState;
|
|
const originalReplaceState = history.replaceState;
|
|
|
|
// 重写 pushState 以监听 pushState 跳转
|
|
history.pushState = function () {
|
|
originalPushState.apply(history, arguments);
|
|
handleRedirect(location.href); // 处理跳转
|
|
};
|
|
|
|
// 重写 replaceState 以监听 replaceState 跳转
|
|
history.replaceState = function () {
|
|
originalReplaceState.apply(history, arguments);
|
|
handleRedirect(location.href); // 处理跳转
|
|
};
|
|
|
|
// 监听 hash 变化的跳转
|
|
window.addEventListener('hashchange', function () {
|
|
handleRedirect(location.href); // 处理跳转
|
|
});
|
|
|
|
// 处理通过 <meta> 标签的跳转 (http-equiv="refresh")
|
|
function observeMetaTags() {
|
|
const metaObserver = new MutationObserver(function (mutations) {
|
|
mutations.forEach(function (mutation) {
|
|
mutation.addedNodes.forEach(function (node) {
|
|
// 检查添加的节点及其子节点中是否包含 <meta> 标签
|
|
if (node.nodeType === 1) { // 确保是元素节点
|
|
// 检查当前节点是否是 <meta> 标签
|
|
if (node.tagName === 'META' && node.httpEquiv === 'refresh') {
|
|
handleMetaRefresh(node);
|
|
}
|
|
|
|
// 检查子节点中是否包含 <meta> 标签
|
|
node.querySelectorAll('meta[http-equiv="refresh"]').forEach(meta => {
|
|
handleMetaRefresh(meta);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
// 监听整个文档的变化,捕捉 <meta> 标签的插入
|
|
metaObserver.observe(document.documentElement, { childList: true, subtree: true });
|
|
}
|
|
|
|
// 处理 <meta> 标签的刷新
|
|
function handleMetaRefresh(meta) {
|
|
const content = meta.content;
|
|
const timeout = parseInt(content.split(';')[0].trim()) * 1000 || 0;
|
|
const url = content.split('url=')[1]?.trim();
|
|
if (url && !isJavascriptUrl(url)) {
|
|
// 使用 setTimeout 延时处理跳转
|
|
setTimeout(() => {
|
|
handleRedirect(url); // 处理跳转
|
|
}, timeout);
|
|
}
|
|
}
|
|
|
|
observeMetaTags(); // 开始监听 <meta> 标签
|
|
|
|
// 处理跳转逻辑
|
|
function handleRedirect(url) {
|
|
// 给 body 添加 class
|
|
document.body.classList.add('rk-jump-out');
|
|
|
|
rk.tip('Loading...', '<div class="mdui-progress"><div class="mdui-progress-indeterminate"></div></div>', 6000);
|
|
// 延迟 0.5 秒后执行跳转
|
|
setTimeout(() => {
|
|
window.location.href = url; // 跳转到目标 URL
|
|
}, 500);
|
|
|
|
// 在跳转后移除类名
|
|
setTimeout(() => {
|
|
document.body.classList.remove('rk-jump-out');
|
|
}, 2000); // 确保跳转完成后移除类名
|
|
}
|
|
|
|
// 判断 URL 是否以 "javascript:" 开头
|
|
function isJavascriptUrl(url) {
|
|
return url.trim().toLowerCase().startsWith("javascript:");
|
|
}
|
|
|
|
// 监听动态生成的元素(包括 <a> 和其他可能的跳转元素)
|
|
const observer = new MutationObserver(function (mutations) {
|
|
mutations.forEach(function (mutation) {
|
|
mutation.addedNodes.forEach(function (node) {
|
|
// 动态生成的 <a> 标签
|
|
if (node.tagName === 'A' && node.href && !isJavascriptUrl(node.href)) {
|
|
node.addEventListener('click', function (event) {
|
|
event.preventDefault();
|
|
handleRedirect(node.href); // 处理跳转
|
|
});
|
|
}
|
|
|
|
// 动态生成的其他元素(可能包含跳转逻辑的元素)
|
|
if (node.nodeType === 1) {
|
|
node.querySelectorAll('a').forEach(a => {
|
|
if (!isJavascriptUrl(a.href)) {
|
|
a.addEventListener('click', function (event) {
|
|
event.preventDefault();
|
|
handleRedirect(a.href); // 处理跳转
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
observer.observe(document.body, { childList: true, subtree: true });
|
|
|
|
// 专门监听 mdui-dialog 的展示和链接点击行为
|
|
function observeDialogLinks() {
|
|
const dialogs = document.querySelectorAll('.mdui-dialog');
|
|
|
|
dialogs.forEach(dialog => {
|
|
dialog.addEventListener('open.mdui.dialog', function() {
|
|
// 在对话框打开时,绑定其内的 <a> 标签的点击事件
|
|
dialog.querySelectorAll('a').forEach(a => {
|
|
if (!isJavascriptUrl(a.href)) {
|
|
a.addEventListener('click', function(event) {
|
|
event.preventDefault();
|
|
handleRedirect(a.href); // 处理跳转
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
// 初始化时调用一次,监听所有 .mdui-dialog
|
|
observeDialogLinks();
|
|
}
|
|
|
|
// 在页面加载完成时初始化拦截器
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
interceptRedirects();
|
|
});
|