Greasy Fork

Greasy Fork is available in English.

复制中文网址和文本收集

在页面左上角添加书签按钮,并提供跨页面文本收集功能

当前为 2024-10-22 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         复制中文网址和文本收集
// @namespace    http://tampermonkey.net/
// @version      0.7
// @author       dennis
// @description  在页面左上角添加书签按钮,并提供跨页面文本收集功能
// @match        *://*/*
// @grant        GM_setClipboard
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 创建书签按钮
    function createBookmarkButton() {
        const button = document.createElement('div');
        button.id = 'bookmark-button';
        button.style.cssText = `
            position: fixed;
            top: 10px;
            left: 0;
            width: 12px;
            height: 25px;
            border-radius: 0 12px 12px 0;
            background-color: rgba(144, 238, 144, 0.5);
            cursor: pointer;
            z-index: 9999999;
            display: flex;
            justify-content: flex-end;
            align-items: center;
            font-size: 14px;
            color: rgba(0, 0, 0, 0.6);
            transition: all 0.3s ease;
            overflow: hidden;
        `;

        button.innerHTML = '<span style="margin-right: 3px;">❐</span>';

        button.addEventListener('mouseenter', function() {
            this.style.width = '25px';
            this.style.justifyContent = 'center';
            this.innerHTML = '❐+';
        });

        button.addEventListener('mouseleave', function() {
            this.style.width = '12px';
            this.style.justifyContent = 'flex-end';
            this.innerHTML = '<span style="margin-right: 3px;">❐</span>';
        });

        button.addEventListener('click', function() {
            const title = document.title;
            const url = decodeURI(window.location.href);
            const markdown = `[${title}](${url})`;
            GM_setClipboard(markdown);
            alert('已复制到剪贴板!');
        });

        document.body.appendChild(button);
    }

    // 创建文本收集框
    function createTextCollector() {
        const collector = document.createElement('div');
        collector.id = 'text-collector';
        collector.style.cssText = `
            position: fixed;
            bottom: 10px;
            right: 10px;
            width: 300px;
            height: 200px;
            background-color: rgba(255, 255, 255, 0.9);
            border: 1px solid #ccc;
            border-radius: 5px;
            padding: 10px;
            font-size: 14px;
            z-index: 9999999;
            display: none;
            flex-direction: column;
        `;

        const buttonContainer = document.createElement('div');
        buttonContainer.style.cssText = `
            position: absolute;
            top: 5px;
            right: 5px;
            display: flex;
        `;

        const clearButton = document.createElement('div');
        clearButton.innerHTML = '🗑️';
        clearButton.style.cssText = `
            cursor: pointer;
            font-size: 14px;
            margin-right: 10px;
            opacity: 0.4;
            filter: grayscale(100%) brightness(0.8);
            transition: opacity 0.3s ease, filter 0.3s ease;
        `;
        clearButton.addEventListener('mouseenter', function() {
            this.style.opacity = '0.8';
            this.style.filter = 'none';
        });
        clearButton.addEventListener('mouseleave', function() {
            this.style.opacity = '0.4';
            this.style.filter = 'grayscale(100%) brightness(0.8)';
        });
        clearButton.addEventListener('click', () => {
            textArea.value = '';
            GM_setValue('collectedText', '');
        });

        const closeButton = document.createElement('div');
        closeButton.innerHTML = '✖';
        closeButton.style.cssText = `
            cursor: pointer;
            font-size: 14px;
        `;
        closeButton.addEventListener('click', () => {
            collector.style.display = 'none';
            GM_setValue('collectorVisible', false);
        });

        buttonContainer.appendChild(clearButton);
        buttonContainer.appendChild(closeButton);

        const textArea = document.createElement('textarea');
        textArea.style.cssText = `
            flex-grow: 1;
            resize: none;
            border: none;
            outline: none;
            background-color: transparent;
        `;

        // 从存储中加载文本
        textArea.value = GM_getValue('collectedText', '');

        // 监听文本变化并保存
        textArea.addEventListener('input', () => {
            GM_setValue('collectedText', textArea.value);
        });

        collector.appendChild(buttonContainer);
        collector.appendChild(textArea);
        document.body.appendChild(collector);

        // 恢复收集框的可见状态
        if (GM_getValue('collectorVisible', false)) {
            collector.style.display = 'flex';
        }

        return { collector, textArea };
    }

    // 初始化
    createBookmarkButton();
    const { collector, textArea } = createTextCollector();

    // 监听键盘事件
    document.addEventListener('keydown', function(e) {
        if (e.ctrlKey && e.key === 'x') {
            e.preventDefault(); // 阻止默认的剪切行为
            const selectedText = window.getSelection().toString().trim();
            if (selectedText) {
                if (collector.style.display === 'none') {
                    collector.style.display = 'flex';
                    GM_setValue('collectorVisible', true);
                    textArea.value += (textArea.value ? '\n' : '') + selectedText;
                } else {
                    textArea.value += '\n' + selectedText;
                }
                GM_setValue('collectedText', textArea.value);
                GM_setClipboard(textArea.value);
            }
        }
    });

})();