feishu viewing

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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