Greasy Fork

来自缓存

Greasy Fork is available in English.

关键词叠词生成器

输入关键词生成组合叠词并添加复制按钮

当前为 2024-11-15 提交的版本,查看 最新版本

// ==UserScript==
// @name         关键词叠词生成器
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  输入关键词生成组合叠词并添加复制按钮
// @author       You
// @match        https://www.amazon.com/*
// @match        https://www.amazon.co.uk/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // 创建样式,确保输入框和按钮显示在页面顶部
    GM_addStyle(`
        #wordGeneratorContainer {
            position: fixed;
            top: 10px;
            left: 10px;
            z-index: 9999;
            background-color: white;
            padding: 20px;
            border: 2px solid #ccc;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }
        #wordGeneratorContainer input,
        #wordGeneratorContainer button {
            margin: 5px;
            padding: 8px;
        }
        #outputArea {
            white-space: pre-wrap;
            word-wrap: break-word;
            margin-top: 10px;
            border: 1px solid #ccc;
            padding: 10px;
            max-height: 300px;
            overflow-y: scroll;
            width: 300px;
        }
    `);

    // 创建容器和元素
    const container = document.createElement('div');
    container.id = 'wordGeneratorContainer';

    const inputBox = document.createElement('input');
    inputBox.type = 'text';
    inputBox.placeholder = '';  // 删除提示文字

    const generateButton = document.createElement('button');
    generateButton.textContent = '生成叠词';

    const copyButton = document.createElement('button');
    copyButton.textContent = '一键复制';

    const outputArea = document.createElement('pre');
    outputArea.id = 'outputArea';

    // 将元素添加到容器
    container.appendChild(inputBox);
    container.appendChild(generateButton);
    container.appendChild(copyButton);
    container.appendChild(outputArea);
    
    // 将容器添加到页面中
    document.body.appendChild(container);

    // 生成叠词组合
    function generateCombinations(input) {
        const words = input.trim().split(' ');
        const length = words.length;
        let combinations = [];

        if (length === 2) {
            // 处理 AB 形式
            combinations.push(`${words[0]} ${words[0]} ${words[1]} ${words[1]}`);
            combinations.push(`${words[0]} ${words[1]} ${words[0]} ${words[1]}`);
            combinations.push(`${words[0]} ${words[0]} ${words[1]}`);
            combinations.push(`${words[0]} ${words[1]} ${words[1]}`);
        } else if (length === 3) {
            // 处理 ABC 形式
            combinations.push(`${words[0]} ${words[1]} ${words[2]} ${words[0]} ${words[1]} ${words[2]}`);
            combinations.push(`${words[0]} ${words[0]} ${words[1]} ${words[1]} ${words[2]} ${words[2]}`);
            combinations.push(`${words[0]} ${words[0]} ${words[1]} ${words[2]}`);
            combinations.push(`${words[0]} ${words[1]} ${words[1]} ${words[2]}`);
            combinations.push(`${words[0]} ${words[1]} ${words[2]} ${words[2]}`);
        }

        return combinations.join('\n');
    }

    // 生成叠词按钮点击事件
    generateButton.addEventListener('click', () => {
        const input = inputBox.value;
        if (input) {
            outputArea.textContent = generateCombinations(input);
        } else {
            outputArea.textContent = '请输入关键词!';
        }
    });

    // 复制按钮点击事件
    copyButton.addEventListener('click', () => {
        const textToCopy = outputArea.textContent;
        if (textToCopy) {
            navigator.clipboard.writeText(textToCopy).then(() => {
                alert('已复制到剪贴板!');
            });
        } else {
            alert('没有生成叠词可复制!');
        }
    });

})();