feishu viewing

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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);
})();