AGI Utility

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         AGI Utility
// @namespace    https://chatgpt.com
// @version      1.2
// @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 this from OpenRouter in the API section';
    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.';

    // === Create popup ===
    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'
    });

    // add close button
    const closeBtn = document.createElement('span');
    closeBtn.textContent = '×';
    Object.assign(closeBtn.style, {
        position: 'absolute',
        top: '4px',
        right: '8px',
        cursor: 'pointer',
        fontWeight: 'bold',
        fontSize: '16px'
    });
    closeBtn.addEventListener('click', () => { box.style.display = 'none'; });
    box.appendChild(closeBtn);

    const content = document.createElement('div');
    box.appendChild(content);

    document.body.appendChild(box);

    function showBox(text) {
        content.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 {
                    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.');
            }
        }
    });
})();