乐享编辑界面增强(悬浮大纲)

在乐享编辑界面中增加大纲和定位悬浮栏,在iframe支持的情况下嘎~

当前为 2024-10-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         乐享编辑界面增强(悬浮大纲)
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  在乐享编辑界面中增加大纲和定位悬浮栏,在iframe支持的情况下嘎~
// @author       阿裕Addyu
// @match        https://lexiangla.com/*/edit*
// @grant        none
// @license MIT
// ==/UserScript==


(function() {
    'use strict';

    // 延迟查找 iframe
    function findIframeAndInjectToc() {
        let iframe = document.querySelector('.tox-edit-area__iframe');

        if (!iframe) {
            console.log('iframe未找到,重试中嘎...');
            setTimeout(findIframeAndInjectToc, 1000); // 如果未找到 iframe,每隔1秒重试
            return;
        }

        console.log('iframe已找到,创建悬浮窗口');
            let iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

            // 创建悬浮窗口
            let tocContainer = document.createElement('div');
            tocContainer.id = 'tocContainer';
            tocContainer.style.cssText = `
                position: fixed; top: 100px; right: 8px; width: 200px; max-height: 500px;
                overflow-y: auto; background: white; border: 1px solid #ccc;
                padding: 10px; z-index: 9999; box-shadow: 0 0 10px rgba(0,0,0,0.1);
                cursor: move;
            `;
            document.body.appendChild(tocContainer);

            // 从iframe中寻找所有标题并创建大纲菜单
            let headings = iframeDocument.querySelectorAll('h1, h2, h3');
       if (headings.length === 0) {
            console.log('未找到标题,稍候重试嘎...');
            setTimeout(findIframeAndInjectToc, 3000);
            return;
       }
      tocContainer.innerHTML = `<h3>文档大纲</h3>`; // 大纲标题嘎

            let tocList = document.createElement('ul');
            tocList.style.listStyle = 'none';
            tocList.style.padding = '0';
            tocContainer.appendChild(tocList);

            headings.forEach((heading, index) => {
                const tocItem = document.createElement('li');
                const headingTag = heading.tagName.toLowerCase();

                tocItem.innerHTML = `<a href="javascript:void(0);" data-index="${index}" style="font-size: ${headingTag === 'h1' ? '18px' : headingTag === 'h2' ? '16px' : '14px'};">
                                        ${heading.innerText}
                                     </a>`;
                tocItem.style.margin = '5px 0';

                tocList.appendChild(tocItem);

                tocItem.addEventListener('click', (e) => {
                    e.preventDefault();
                    heading.scrollIntoView({ behavior: 'smooth' });
                });
            });

            tocContainer.ondragstart = function() {
                return false;
            };
    }

    // 执行查找 iframe 和注入大纲
    setTimeout(findIframeAndInjectToc, 3000); // 延迟 3 秒开始查找 iframe
})();

QingJ © 2025

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