// ==UserScript== // @name 编辑网页 // @namespace http://tampermonkey.net/ // @version 1.1.0-pre.1 // @description 这是一个能编辑任何网页文本的扩展 // @author yhu // @license GPL-3.0 // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; let currentElement = null; let originalColor = null; function makeEditable(element) { if (element.isContentEditable) return; if (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA') return; if (element.closest('button, a, [role="button"]')) return; if (currentElement) { currentElement.contentEditable = 'false'; currentElement.style.outline = ''; currentElement.style.backgroundColor = ''; if (currentElement.style.color === 'yellow') { currentElement.style.color = originalColor || ''; } } originalColor = getComputedStyle(element).color; element.contentEditable = 'true'; element.style.outline = '2px dashed #4a9eff'; element.style.backgroundColor = 'rgba(74, 158, 255, 0.1)'; element.style.color = 'yellow'; element.focus(); currentElement = element; } document.body.addEventListener('click', function(e) { if (e.detail === 2) return; const target = e.target; if (target.isContentEditable) return; if (currentElement && !currentElement.contains(target)) { currentElement.contentEditable = 'false'; currentElement.style.outline = ''; currentElement.style.backgroundColor = ''; if (currentElement.style.color === 'yellow') { currentElement.style.color = originalColor || ''; } currentElement = null; } let textNode = target; while (textNode && textNode.nodeType === Node.TEXT_NODE) { textNode = textNode.parentElement; } if (!textNode) return; const editableElement = textNode.closest('p, h1, h2, h3, h4, h5, h6, li, span, div, label, td, th, figcaption, legend'); if (editableElement) { e.preventDefault(); e.stopPropagation(); makeEditable(editableElement); } }, true); })();