网页备忘录

Show customizable prompt suggestions on any webpage, with resizable and draggable functionality

目前为 2024-08-02 提交的版本。查看 最新版本

// ==UserScript==
// @name         网页备忘录
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Show customizable prompt suggestions on any webpage, with resizable and draggable functionality
// @author       而今迈步从头越
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const defaultPrompts = [
        '按不同的域名存储内容',
        '可以在不同的网站上显示不同的内容',

    ];

    const domain = window.location.hostname;

    // Function to get prompts from local storage or use default prompts
    function getPrompts() {
        const savedPrompts = localStorage.getItem(`prompts${domain}`);
        return savedPrompts ? JSON.parse(savedPrompts) : defaultPrompts;
    }

    // Function to save prompts to local storage
    function savePrompts(prompts) {
        localStorage.setItem(`prompts${domain}`, JSON.stringify(prompts));
    }

    // Function to create and display the prompt suggestions
    function createPromptSuggestions() {
        const container = document.createElement('div');
        container.style.position = 'fixed';
        container.style.bottom = '10px';
        container.style.left = '300px';
        container.style.background = 'rgba(255, 255, 255, 0.5)';
        container.style.border = '1px solid #ccc';
        container.style.padding = '10px';
        container.style.zIndex = '9999';
        container.style.boxShadow = '0px 0px 10px rgba(0, 0, 0, 0.1)';
        container.style.width = '300px';
        container.style.maxHeight = '400px';
        container.style.overflowY = 'auto';
        container.style.borderRadius = '5px';
        container.style.resize = 'both';
        container.style.overflow = 'auto';

        const title = document.createElement('strong');
        title.textContent = '常用提示词:';
        container.appendChild(title);
        container.appendChild(document.createElement('br'));

        const prompts = getPrompts();
        prompts.forEach(prompt => {
            const item = document.createElement('div');
            item.textContent = prompt;
            item.style.marginBottom = '5px';
            container.appendChild(item);
        });

        document.body.appendChild(container);

        container.addEventListener('mousedown', function(e) {
            if (e.target === container || e.target === title) {
                let offsetX = e.clientX - container.getBoundingClientRect().left;
                let offsetY = e.clientY - container.getBoundingClientRect().top;

                function mouseMoveHandler(e) {
                    container.style.left = `${e.clientX - offsetX}px`;
                    container.style.top = `${e.clientY - offsetY}px`;
                }

                function mouseUpHandler() {
                    document.removeEventListener('mousemove', mouseMoveHandler);
                    document.removeEventListener('mouseup', mouseUpHandler);
                }

                document.addEventListener('mousemove', mouseMoveHandler);
                document.addEventListener('mouseup', mouseUpHandler);
            }
        });

        const editButton = document.createElement('button');
        editButton.textContent = '编辑';
        editButton.style.marginTop = '10px';
        container.appendChild(editButton);

        editButton.addEventListener('click', function() {
            const newPrompts = prompt('编辑提示词,用逗号分隔:', prompts.join(','));
            if (newPrompts !== null) {
                const updatedPrompts = newPrompts.split(',').map(p => p.trim());
                savePrompts(updatedPrompts);
                container.innerHTML = '';
                container.appendChild(title);
                container.appendChild(document.createElement('br'));
                updatedPrompts.forEach(prompt => {
                    const item = document.createElement('div');
                    item.textContent = prompt;
                    item.style.marginBottom = '5px';
                    container.appendChild(item);
                });
                container.appendChild(editButton);
            }
        });
    }

    setTimeout(createPromptSuggestions, 3000); // 3 seconds delay
})();