editor html

try to take over the world!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         editor html
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  try to take over the world!
// @author       Enoch
// @match        https://www.oppo.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zhinan.tech
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const textNodes = [];
    // 遍历 DOM 树,寻找只包含文本的节点
    function findTextNodes(node) {
        if (node.nodeType === Node.TEXT_NODE && node.textContent.trim().length > 0) {
            textNodes.push(node);
        } else {
            for (const childNode of node.childNodes) {
                findTextNodes(childNode);
            }
        }
    }
    // 查找只包含文本的节点
    findTextNodes(document.body);
    // 给满足条件的文本节点的父级元素添加背景颜色
    for (const textNode of textNodes) {
        const parentElement = textNode.parentElement;
        const {width,height} = parentElement.getBoundingClientRect();
        parentElement.style.backgroundColor = '#7ea864';
        parentElement.style.width = `${width}px`;
        parentElement.style.height = `${height}px`;

    }

    // 创建一个开关元素
    const switchElement = document.createElement('input');
    switchElement.type = 'checkbox';
    Object.assign(switchElement.style, {
        marginRight: '5px'
    });

    const labelElement = document.createElement('label');
    labelElement.textContent = '开启编辑模式';
    Object.assign(labelElement.style, {
        cursor: 'pointer',
        contentEditable: 'false'
    });

    const topBar = document.createElement('div');
    Object.assign(topBar.style, {
        position: 'fixed',
        top: '0',
        right: '0',
        backgroundColor: '#f7f7f7',
        padding: '10px',
        zIndex: '100'
    });
    labelElement.appendChild(switchElement);
    topBar.appendChild(labelElement);
    document.body.appendChild(topBar);

    // 处理开关按钮的点击事件
    switchElement.addEventListener('change', function() {
        const bodyEditable = switchElement.checked;
        if(bodyEditable){
          labelElement.textContent='关闭编辑模式';
        }else{
          labelElement.textContent='开启编辑模式';
        }
        document.body.contentEditable = bodyEditable;
    });
})();