腾讯文档自动选择浏览器打开

自动选择在浏览器中打开腾讯文档链接

目前為 2025-01-02 提交的版本,檢視 最新版本

// ==UserScript==
// @name         腾讯文档自动选择浏览器打开
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  自动选择在浏览器中打开腾讯文档链接
// @author       微信11208596
// @license      UNLICENSED
// @match        *://docs.qq.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function findAndClickLink() {
        // 查找所有可能的元素
        const elements = document.querySelectorAll('*');

        for (const element of elements) {
            // 检查元素的文本内容
            const text = element.textContent.trim();

            // 如果找到包含目标文本的元素
            if (text === '继续在浏览器中查看') {
                console.log('找到目标文本:', text, '元素类型:', element.tagName);

                // 尝试点击元素本身
                try {
                    element.click();
                    console.log('点击元素本身');
                    return true;
                } catch (e) {
                    console.log('点击元素失败,尝试其他方法');
                }

                // 尝试点击父元素
                try {
                    const parent = element.parentElement;
                    if (parent) {
                        parent.click();
                        console.log('点击父元素');
                        return true;
                    }
                } catch (e) {
                    console.log('点击父元素失败');
                }

                // 查找附近的所有可点击元素
                const nearbyElements = element.parentElement?.querySelectorAll('a, button, [role="button"], [class*="button"], [class*="link"]');
                if (nearbyElements) {
                    for (const nearby of nearbyElements) {
                        try {
                            nearby.click();
                            console.log('点击附近元素');
                            return true;
                        } catch (e) {
                            console.log('点击附近元素失败');
                        }
                    }
                }

                // 如果都失败了,尝试模拟点击事件
                try {
                    const clickEvent = new MouseEvent('click', {
                        bubbles: true,
                        cancelable: true,
                        view: window
                    });
                    element.dispatchEvent(clickEvent);
                    console.log('派发点击事件');
                    return true;
                } catch (e) {
                    console.log('派发点击事件失败');
                }
            }
        }
        return false;
    }

    // 创建一个更积极的定期检查函数
    function checkPeriodically(count = 0) {
        if (count >= 50) return; // 增加检查次数到50次

        if (!findAndClickLink()) {
            setTimeout(() => checkPeriodically(count + 1), 100); // 缩短间隔到100ms
        }
    }

    function init() {
        // 立即执行一次检查
        findAndClickLink();

        // 启动定期检查
        checkPeriodically();

        // 监听DOM变化
        const observer = new MutationObserver((mutations) => {
            for (const mutation of mutations) {
                if (mutation.type === 'childList' || mutation.type === 'characterData') {
                    findAndClickLink();
                }
            }
        });

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

        // 开始观察文档中的变化
        observer.observe(document.body, config);
    }

    // 确保DOM加载完成后执行
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址