QinSeHm

导航

js_hook_网页跳转_油猴

// ==UserScript==
// @name         页面跳转监控
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  监控和捕获页面跳转行为
// @author       Your name
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 保存原始的window.open方法
    const originalWindowOpen = window.open;

    // 重写window.open方法
    window.open = function() {
        console.log('捕获到window.open调用:', {
            url: arguments[0],
            timestamp: new Date().toISOString()
        });
        return originalWindowOpen.apply(this, arguments);
    };

    // 监听点击事件
    document.addEventListener('click', function(e) {
        const target = e.target.closest('a');
        if (target) {
            console.log('捕获到链接点击:', {
                element: target,
                href: target.href,
                timestamp: new Date().toISOString()
            });
        }
    }, true);

    // 使用MutationObserver监听DOM变化
    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList') {
                const addedNodes = Array.from(mutation.addedNodes);
                addedNodes.forEach(node => {
                    if (node.nodeType === 1) { // 元素节点
                        const links = node.getElementsByTagName('a');
                        Array.from(links).forEach(link => {
                            console.log('发现新添加的链接:', {
                                element: link,
                                href: link.href,
                                timestamp: new Date().toISOString()
                            });
                        });
                    }
                });
            }
        });
    });

    // 配置观察选项
    const config = {
        childList: true,
        subtree: true
    };

    // 开始观察整个文档
    observer.observe(document.documentElement, config);

    // 监听history API
    const originalPushState = history.pushState;
    const originalReplaceState = history.replaceState;

    history.pushState = function() {
        console.log('捕获到pushState:', {
            arguments: arguments,
            timestamp: new Date().toISOString()
        });
        return originalPushState.apply(this, arguments);
    };

    history.replaceState = function() {
        console.log('捕获到replaceState:', {
            arguments: arguments,
            timestamp: new Date().toISOString()
        });
        return originalReplaceState.apply(this, arguments);
    };

    // 监听popstate事件
    window.addEventListener('popstate', function(event) {
        console.log('捕获到popstate事件:', {
            state: event.state,
            timestamp: new Date().toISOString()
        });
    });

    console.log('页面跳转监控脚本已启动');
})();

posted on 2025-03-18 17:59  QinSe  阅读(69)  评论(0)    收藏  举报