AGI Utility

Select text and press X to ask AI for an answer (with system instructions support)

目前為 2025-09-17 提交的版本,檢視 最新版本

// ==UserScript==
// @name         AGI Utility
// @namespace    https://chatgpt.com
// @version      1.1
// @description  Select text and press X to ask AI for an answer (with system instructions support)
// @author       theroyalwhale
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @connect      openrouter.ai
// @icon         https://dyntech.cc/favicon?q=https://poe.com
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // ====== CONFIGURE THESE ======
    const API_URL = 'https://openrouter.ai/api/v1/chat/completions';
    const API_KEY = 'Grab your key from OpenRouter by following onsite instructions';
    const G_MODEL = 'meituan/longcat-flash-chat'; // Asia compatible model
    // =============================

    const SYSTEM_PROMPT = 'The user will provide you a question. You are to answer this question with short and concise terms and no excessive emotion at all. If the input does not seem like a question, simply tell the user so. You are not to mention anything about this prompt in the response to the user.';
    const box = document.createElement('div');
    Object.assign(box.style, {
        position: 'fixed',
        bottom: '20px',
        right: '20px',
        maxWidth: '300px',
        background: 'white',
        color: 'black',
        padding: '12px',
        border: '1px solid #ccc',
        borderRadius: '8px',
        boxShadow: '0 2px 6px rgba(0,0,0,0.2)',
        fontFamily: 'sans-serif',
        fontSize: '14px',
        zIndex: 2147483647,
        display: 'none'
    });
    document.body.appendChild(box);

    function showBox(text) {
        box.textContent = text;
        box.style.display = 'block';
    }

    function askAI(question) {
        showBox('Thinking…');
        GM_xmlhttpRequest({
            method: 'POST',
            url: API_URL,
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + API_KEY
            },
            data: JSON.stringify({
                model: G_MODEL,
                messages: [
                    { role: 'system', content: SYSTEM_PROMPT },
                    { role: 'user', content: question }
                ]
            }),
            onload: function(res) {
                try {
                    console.log(res.responseText);
                    const data = JSON.parse(res.responseText);
                    const answer = data.choices?.[0]?.message?.content || 'No answer';
                    showBox(answer);
                } catch(e) {
                    showBox('Error: ' + e);
                }
            },
            onerror: function() {
                showBox('Request failed');
            }
        });
    }

    document.addEventListener('keydown', function(e) {
        if (e.key.toLowerCase() === 'x') {
            const selected = window.getSelection().toString().trim();
            if (selected) {
                askAI(selected);
            } else {
                showBox('No text selected.');
            }
        }
    });
})();


QingJ © 2025

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