Greasy Fork is available in English.

Flomo 增强

Flomo 增强功能,自用

目前为 2024-04-06 提交的版本,查看 最新版本

// ==UserScript==
// @name         Flomo 增强
// @namespace    http://tampermonkey.net/
// @version      2024-04-06
// @description  Flomo 增强功能,自用
// @author       Loongphy
// @match        https://v.flomoapp.com/mine
// @icon         https://www.google.com/s2/favicons?sz=64&domain=flomoapp.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // 缓存键,用于存储卡片内容
    const cacheKey = 'card_content';

    // 添加观察者,监视卡片的加载
    observeCardAdditions();

    // 添加观察者,监视 Tooltip 的创建
    observeTooltipAdditions();

    // 监视页面上卡片的添加
    function observeCardAdditions() {
        const cardObserver = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                mutation.addedNodes.forEach((node) => {
                    // 检查节点是否为卡片
                    if (node.nodeType === 1 && node.matches('.memo')) {
                        addHoverListenerToCard(node);
                    }
                });
            });
        });

        cardObserver.observe(document.body, { childList: true, subtree: true });
    }

    // 为卡片添加鼠标悬浮事件,以缓存内容
    function addHoverListenerToCard(cardNode) {
        cardNode.addEventListener('mouseenter', function () {
            const content = cardNode.querySelector('.mainContent').textContent.trim();
            sessionStorage.setItem(cacheKey, content);
        });
    }

    // 监视 Tooltip 的创建
    function observeTooltipAdditions() {
        const tooltipObserver = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                mutation.addedNodes.forEach((node) => {
                    // 检查节点是否为 Tooltip
                    if (node.nodeType === 1 && node.getAttribute('role') === 'tooltip') {
                        enhanceTooltip(node);
                    }
                });
            });
        });

        tooltipObserver.observe(document.documentElement, { childList: true, subtree: true });
    }


    // 增强 Tooltip,添加分享到 X 的选项
    function enhanceTooltip(tooltipNode) {
        if (tooltipNode.getAttribute('data-enhanced') !== 'true') {
            const popoverMenu = tooltipNode.querySelector('.popover-menu');
            if (popoverMenu) {
                const newItem = createShareOption();
                insertShareOption(popoverMenu, newItem);
                tooltipNode.setAttribute('data-enhanced', 'true');
            }
        }
    }

    // 创建分享选项
    function createShareOption() {
        const newItem = document.createElement('div');
        newItem.className = 'item';
        newItem.textContent = '分享到 X';
        shareToX(newItem);
        return newItem;
    }

    // 插入分享选项到 Tooltip
    function insertShareOption(popoverMenu, newItem) {
        const items = popoverMenu.querySelectorAll('.item');
        if (items.length >= 4) {
            popoverMenu.insertBefore(newItem, items[3]);
        } else {
            popoverMenu.appendChild(newItem);
        }
    }

    // 为分享选项添加点击事件
    function shareToX(newItem) {
        newItem.addEventListener('click', () => {
            const content = sessionStorage.getItem(cacheKey);
            if (content) {
                const encodedText = encodeURIComponent(content);
                const twitterShareUrl = `https://twitter.com/intent/post?text=${encodedText}`;
                window.open(twitterShareUrl, '_blank');
            }
        });
    }
})();