Gitea Markdown Highlight

Highlight all text within == == marks throughout the entire document

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Gitea Markdown Highlight
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Highlight all text within == == marks throughout the entire document
// @author       Noriu
// @match        https://git.noriu.cn/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 定义一个函数来高亮文本
    function highlightText(node) {
        // 使用正则表达式匹配“==文本==”格式
        const regex = /==\s*(.*?)\s*==/g;
        let match;

        // 检查节点是否为文本节点
        if (node.nodeType === Node.TEXT_NODE) {
            let lastIndex = 0;
            while ((match = regex.exec(node.nodeValue)) !== null) {
                const beforeText = document.createTextNode(node.nodeValue.substring(lastIndex, match.index));
                if (beforeText.length) {
                    node.parentNode.insertBefore(beforeText, node);
                }

                const span = document.createElement('span');
                span.style.backgroundColor = 'yellow'; // 设置背景颜色为黄色
                span.textContent = match[1]; // 包含匹配的文本
                node.parentNode.insertBefore(span, node);

                lastIndex = match.index + match[0].length;
            }

            // 添加剩余的文本
            const afterText = document.createTextNode(node.nodeValue.substring(lastIndex));
            if (afterText.length) {
                node.parentNode.insertBefore(afterText, node);
            }

            // 移除原始文本节点
            node.parentNode.removeChild(node);
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            // 特殊处理<p>标签
            if (node.tagName === 'P' && node.querySelector('span.ambiguous-code-point, span.char')) {
                // 合并所有子节点的文本内容
                let textContent = '';
                node.childNodes.forEach(child => {
                    textContent += child.textContent;
                });
                // 将合并后的文本内容替换原<p>标签内容
                const mergedText = document.createTextNode(textContent);
                node.innerHTML = '';
                node.appendChild(mergedText);
                // 重新调用highlightText处理合并后的文本
                highlightText(mergedText);
            } else {
                // 递归处理所有子节点
                node.childNodes.forEach(highlightText);
            }
        }
    }

    // 监听页面加载完成
    window.addEventListener('load', () => {
        document.body.childNodes.forEach(highlightText);
    });
})();