网页文本修改器

允许通过右键菜单修改网页上选中的文本

目前為 2024-12-06 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         网页文本修改器
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  允许通过右键菜单修改网页上选中的文本
// @author       You
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_notification
// ==/UserScript==

(function() {
    'use strict';

    // 添加自定义样式
    GM_addStyle(`
        .text-edit-popup {
            position: fixed;
            background: white;
            border: 1px solid #ccc;
            padding: 10px;
            box-shadow: 2px 2px 5px rgba(0,0,0,0.2);
            z-index: 9999;
        }
        .text-edit-textarea {
            min-width: 200px;
            min-height: 100px;
            margin-bottom: 10px;
        }
        .text-edit-buttons {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
        }
    `);

    // 创建编辑弹窗
    function createEditPopup(x, y, originalText, targetElement) {
        const popup = document.createElement('div');
        popup.className = 'text-edit-popup';
        popup.style.left = x + 'px';
        popup.style.top = y + 'px';

        const textarea = document.createElement('textarea');
        textarea.className = 'text-edit-textarea';
        textarea.value = originalText;

        const buttonsDiv = document.createElement('div');
        buttonsDiv.className = 'text-edit-buttons';

        const saveButton = document.createElement('button');
        saveButton.textContent = '保存';
        saveButton.onclick = () => {
            targetElement.textContent = textarea.value;
            document.body.removeChild(popup);
        };

        const cancelButton = document.createElement('button');
        cancelButton.textContent = '取消';
        cancelButton.onclick = () => {
            document.body.removeChild(popup);
        };

        buttonsDiv.appendChild(saveButton);
        buttonsDiv.appendChild(cancelButton);
        popup.appendChild(textarea);
        popup.appendChild(buttonsDiv);

        return popup;
    }

    // 处理选中文本的函数
    function handleSelectedText() {
        const selection = window.getSelection();
        if (selection.toString().trim()) {
            const range = selection.getRangeAt(0);
            const container = range.commonAncestorContainer;
            const targetElement = container.nodeType === 3 ? container.parentNode : container;

            const rect = range.getBoundingClientRect();
            const x = rect.left + window.scrollX;
            const y = rect.bottom + window.scrollY;

            const popup = createEditPopup(x, y, selection.toString(), targetElement);
            document.body.appendChild(popup);
            return true;
        }
        return false;
    }

    // 注册 Tampermonkey 菜单命令
    GM_registerMenuCommand("修改选中文本", function() {
        if (!handleSelectedText()) {
            GM_notification({
                text: "请先选择要修改的文本",
                timeout: 2000
            });
        }
    });

    // 添加快捷键支持
    document.addEventListener('keydown', function(e) {
        // 支持 Windows(Ctrl+E) 和 Mac(Command+E)
        if ((e.ctrlKey || e.metaKey) && e.key === 'e') {
            e.preventDefault();
            handleSelectedText();
        }
    });
})();