算法思维树

添加快捷指令(prompts)

目前为 2023-12-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         算法思维树
// @namespace    http://falcon.ictbda.cn:89/yoriko/
// @version      0.22
// @description  添加快捷指令(prompts)
// @author       TAIST
// @match        https://poe.com/*
// @match        https://chat.openai.com/*
// @match        http://falcon.ictbda.cn:89/yoriko/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @license MIT
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';
    var isDragging = false;
    var offsetX, offsetY;
    // Define the prompts
    var prompts = [
        {'label': "提出一个数学(算法) 问题", 'content': "我想让你充当数学和算法研究专家。当我问你一个数学(算法)问题,而你的工作就是想出为我解决问题的策略。这可能包括建议代码、代码逻辑思路策略和数学推导,你可以帮助我解决问题吗?" },
        {'label': "请给出这个问题的精确解",'content': "请给出这个问题的精确解,若无精确解,回答不存在精确解 " },
        {'label': "该问题是否是优化问题", 'content': "该问题是否是优化问题?"},
        {'label': "该问题是否能分解成子问题?",'content':"该问题是否能分解成子问题?"},
        {'label': '该问题的子问题相互独立,还是有重叠', 'content': "该问题的子问题相互独立,还是有重叠?"},
        {'label': '请使用分治算法解决', 'content': "请使用分治算法解决。"},
        {'label': '请写出这个问题的最优子结构', 'content': "请写出这个问题的最优子结构。"},
        {'label': '该问题的最优解能通过局部最优选择来达成吗', 'content': "该问题的最优解能通过局部最优选择来达成吗?"},
        {'label':'请使用动态规划算法解决','content': `请使用动态规划算法解决。在使用动态规划算法解题时,请先回答以下7个问题,然后根据问题答案给出一个最优的动态规划算法:
            (1)请写出这个问题在算法中的子问题和最优子结构。
            (2)根据这个子问题和最优子结构写出状态转移方程。
            (3)写出算法中最优决策项之间的联系。
            (4)用以算代存的方法节省存储空间修改算法。
            (5)这个算法可以节省时间吗?
            (6)请写出OPT表中的回溯路径。
            (7) 请写出OPT表中的值的规律。是稀疏的吗?
            (8)请判断该算法能否用贪心算法解决?如果可以,请将根据最优关系的规律将动态规划算法改写为贪心算法`},
        {'label':"请使用贪心算法解决",
        'content':`请使用贪心算法解决
            (1) 请将算法目标拆分为多步小目标
            (2)求出每个小目标的局部最优解
            (3)将局部最优解合成为原问题的解`},
        {'label': '请分解解的邻域', 'content': '请分解解的邻域'},
        {'label': '请求出尽量逼近正解的解的邻域', 'content': '请求出尽量逼近正解的解的邻域'},
        {'label':"请使用线性规划算法解决",'content':`请使用线性规划算法解决。
            (1)请将原问题按照if x=1 OR z=0 then y=1的形式描述为自然语言
            (2)将自然语言表述为线性不等式。
            (3)在目标函数中添加配合项`},
        {'label': '权衡问题,是否有好求解的近似解', 'content': '请你权衡问题,是否有好求解的近似解?'},
        {'label': '请使用算法解出一个相对好的近似解', 'content': '请使用算法解出一个相对好的近似解。'},
        {'label': '请将求最优的问题转化为接近最优的并写出算法', 'content': '请将求最优的问题转化为接近最优的并写出算法。'},
        {'label': '请将确定性的问题转化为随机的并写出算法', 'content': '请将确定性的问题转化为随机的并写出算法。'},
        {'label': '请将最坏的案例转化为具体的案例并写出算法', 'content': '请将最坏的案例转化为具体的案例并写出算法。'},
        {'label': '请将求确定理论规则的问题转化为启发式的并写出算法', 'content': '请将求确定理论规则的问题转化为启发式的并写出算法。'},
        {'label': '(按可行方式)求解问题', 'content': '请你写出你认为可行的方式求解此问题'}

    ];


    //创建文本容器
    var container = document.createElement('div');
    container.style.position = 'fixed';
    container.style.top = '50%';
    container.style.right = '20px';
    container.style.transform = 'translateY(-60%)';
    container.style.width = '250px';
    container.style.height = '500px';
    container.style.background = '#E8E8E8';
    container.style.borderRadius = '5px';
    container.style.padding = '10px';
    container.style.display = 'none';
    container.style.overflow = 'auto';
    document.body.appendChild(container);

    GM_addStyle(`
        .custom-button {
            width: 250px; /* 设置统一的宽度,根据需要调整 */
        }
    `);
    // 创建文本块
    prompts.forEach(function(prompt) {
        var promptButton = document.createElement('button');
        promptButton.textContent = prompt.label;
        promptButton.className = 'custom-button';
        promptButton.style.display = 'block';
        promptButton.style.marginBottom = '5px';
        //promptButton.style.background = '#A8A8A8 ';
        promptButton.style.background = '#ffffff ';
        promptButton.style.color = '#4682B4';
        //promptButton.style.color = 'blue';
        promptButton.style.border = 'none';
        promptButton.style.borderRadius = '5px';
        promptButton.style.padding = '5px';
        promptButton.style.cursor = 'pointer';
	promptButton.style.textAlign = "left"
        promptButton.addEventListener('click', function() {
            insertPrompt(prompt.content);
        });
        container.appendChild(promptButton);
    });
    //插入文本框
        function insertPrompt(content) {
        var inputBox = document.querySelector('input[type="text"], textarea');
        const submitButton = document.querySelector('.ChatMessageSendButton_sendButton__4ZyI4');
        if (inputBox) {
            var startPos = inputBox.selectionStart;
            var endPos = inputBox.selectionEnd;
            var text = inputBox.value;
            var newText = text.substring(0, startPos) + content + text.substring(endPos, text.length);
            inputBox.value = newText;
            inputBox.setSelectionRange(startPos + content.length, startPos + content.length);
            inputBox.style.textAlign = 'left';
            inputBox.style.height = inputBox.scrollHeight + 'px';
        }
        // 获取文本输入框和提交按钮元素
        // 移除提交按钮的 disabled 属性
        //submitButton.removeAttribute('disabled');
        //submitButton.click()
        // 移除按钮的禁用状态并模拟点击
        const enterKeyEvent = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', which: 13, keyCode: 13, bubbles: true });
        inputBox.dispatchEvent(enterKeyEvent);
            if (submitButton && submitButton.disabled === true) {
                submitButton.removeAttribute('disabled');
}
            if (submitButton) {
                submitButton.click();
}
    }


    // 创建按钮Algorithm Tree
    var button = document.createElement('textarea');
    button.style.position = 'fixed';
    button.style.bottom = '60px';
    button.style.right = '20px';
    button.style.width = '200px';
    button.style.height = '30px';
    button.style.background = '	#ffffff';
    button.style.color = 'black';
    button.style.border = '2px solid black';
    button.style.borderRadius = '5px';
    button.style.padding = '10px';
    button.style.resize = 'none';
    button.readOnly = true;
    button.style.font = '16px Bodoni';
    button.style.textAlign = 'center';
    //button.placeholder = '       -- Algorithm Tree --';
    button.textContent = ' -- Algorithm co-pilot --';
    document.body.appendChild(button);

    // 添加鼠标悬停事件监听器
    button.addEventListener('mouseenter', function() {
    button.style.cursor = 'move'; // 鼠标形状变为移动行
    });

    button.addEventListener('mouseleave', function() {
    button.style.cursor = 'default'; // 鼠标形状恢复默认
    });

    // 点击按钮
    button.addEventListener('click', function() {
        if (container.style.display === 'none') {
            container.style.display = 'block';
        } else {
            container.style.display = 'none';
        }
    });

    // 移动按钮
    button.addEventListener('mousedown', function (event) {
        isDragging = true;
        offsetX = event.clientX - button.getBoundingClientRect().left;
        offsetY = event.clientY - button.getBoundingClientRect().top;
    });

    document.addEventListener('mousemove', function (event) {
        if (isDragging) {
            button.style.left = event.clientX - offsetX + 'px';
            button.style.top = event.clientY - offsetY + 'px';
        }
    });

    document.addEventListener('mouseup', function () {
        isDragging = false;
    });


    // 使用 Ctrl+Shift+F 或 Cmd+Shift+F 打开或关闭
    document.addEventListener('keydown', function(event) {
        if ((event.ctrlKey || event.metaKey) && event.shiftKey && event.key === 'F') {
            event.preventDefault();
            if (container.style.display === 'none') {
                container.style.display = 'block';
            } else {
                container.style.display = 'none';
            }
        }
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址