feishu viewing

进入飞书文档时自动切换到阅读模式, Lark Viewing mode

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         feishu viewing
// @namespace    http://tampermonkey.net/
// @license MIT 
// @version      2.5
// @description  进入飞书文档时自动切换到阅读模式, Lark Viewing mode
// @author       You
// @match        *://*.feishu.cn/*
// @match        *://*.larkoffice.com/*
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    let hasSwitched = false;

    function switchToReadMode() {
        if (hasSwitched) return;

        try {
            const switchBtn = Array.from(document.querySelectorAll('[data-selector="docs-mode-switch"]'))
                .find(btn => btn.classList.contains('ud__button--filled'));

            if (!switchBtn) return;

            const text = switchBtn.querySelector('.content_text')?.innerText.trim();

            if (text === '阅读' || text === 'Viewing') {
                hasSwitched = true;
                return;
            }

            console.log('[Feishu View Mode] 点击模式按钮...');
            switchBtn.click();

            setTimeout(() => {
                const menuItems = [...document.querySelectorAll('li.docs_mode_switch_menu_item')];

                for (const item of menuItems) {
                    const labelSpan = item.querySelector('.item_text span');
                    const itemText = labelSpan?.innerText.trim();

                    if (labelSpan && (itemText === '阅读' || itemText === 'Viewing')) {
                        item.click();
                        hasSwitched = true;
                        return;
                    }
                }
            }, 1000);

        } catch (e) {
            console.warn('[Feishu View Mode] ', e);
        }
    }

    const interval = setInterval(() => {
        if (hasSwitched) {
            clearInterval(interval);
            return;
        }
        switchToReadMode();
    }, 500);

    setTimeout(() => {
        clearInterval(interval);
    }, 30000);
})();