移除指定标签并保留内容

移除指定标签(如 <b> 和 <strong>)并保留其内容

// ==UserScript==
// @name         移除指定标签并保留内容
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  移除指定标签(如 <b> 和 <strong>)并保留其内容
// @author       Your Name
// @match        *://xueqiu.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // 定义需要移除的标签
    const tagsToRemove = ['b', 'strong'];

    // 遍历页面中的所有需要移除的标签
    tagsToRemove.forEach((tag) => {
        const elements = document.querySelectorAll(tag);
        elements.forEach((element) => {
            // 创建一个文档片段,用于存储标签内的内容
            const fragment = document.createDocumentFragment();
            while (element.firstChild) {
                fragment.appendChild(element.firstChild);
            }
            // 将文档片段插入到父元素中,替换掉原来的标签
            element.parentNode.insertBefore(fragment, element);
            element.remove(); // 移除标签
        });
    });

    console.log('已移除指定标签并保留内容');
})();