TriX Core Library

Core logic library for TriX Executor with multi-engine Python support.

当前为 2025-07-11 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/541461/1622864/TriX%20Core%20Library.js

// ==UserScript==
// @name        TriX Core Library
// @namespace   https://github.com/YourUsername/TriX-Executor
// @version     3.0.0
// @description Core logic library for TriX Executor with multi-engine Python support.
// @author      You
// @license     MIT
// ==/UserScript==

const TriX_Core = (function() {
    'use strict';
    let _GM;
    const PythonRunners = {
        pyodide: null, pyodideLoading: false, pyodideSkipped: false,
        pyscriptReady: false, pyscriptLoading: false,

        loadPyodideDynamically() { /* ... unchanged ... */ },
        async initPyodide() { /* ... unchanged ... */ },
        async runPyodide(code, packages) { /* ... unchanged ... */ },

        loadPyScriptDynamically() {
            return new Promise((resolve, reject) => {
                if(document.querySelector('script[src*="pyscript.net"]')) return resolve();
                const script = document.createElement('script');
                script.src = 'https://pyscript.net/releases/2024.1.1/core.js';
                script.onload = resolve;
                script.onerror = reject;
                document.head.appendChild(script);
            });
        },
        async initPyScript() {
            if (this.pyscriptReady || this.pyscriptLoading) return;
            this.pyscriptLoading = true;
            TriX_UI.setPythonStatus('loading', 'PyScript');
            TriX_UI.log('Initializing PyScript runtime...', 'info');
            try {
                await this.loadPyScriptDynamically();
                this.pyscriptReady = true;
                TriX_UI.log('PyScript runtime ready.', 'info');
                TriX_UI.setPythonStatus('ready', 'PyScript');
            } catch (err) {
                TriX_UI.log('Error loading PyScript.', 'error');
                TriX_UI.setPythonStatus('error', 'PyScript');
            } finally {
                this.pyscriptLoading = false;
            }
        },
        runPyScript(code, packages) {
            if (!this.pyscriptReady) return TriX_UI.log('PyScript is not ready.', 'warn');
            TriX_UI.setPythonStatus('running', 'PyScript');
            const configEl = document.getElementById('trix-py-config');
            const scriptTargetEl = document.getElementById('trix-py-script-target');
            configEl.innerHTML = `packages = [${packages ? `"${packages.split(',').map(p=>p.trim()).join('", "')}"` : ''}]`;
            scriptTargetEl.code = code;
            scriptTargetEl.pyconfig = configEl;
            scriptTargetEl.run();
            // PyScript doesn't have a simple promise for when it's done,
            // so we just reset the button state after a short delay.
            setTimeout(() => TriX_UI.setPythonStatus('ready', 'PyScript'), 1000);
        },

        runSkulpt(code) {
             if (typeof Sk === 'undefined') return TriX_UI.log('Skulpt not loaded.', 'error');
             TriX_UI.log("Executing Python via Skulpt...", "info");
             TriX_UI.setPythonStatus('running', 'Skulpt');
             Sk.configure({
                 output: (text) => { if(text.trim()) TriX_UI.log(`[Skulpt]: ${text.trim()}`, 'log') },
                 read: (x) => { if (Sk.builtinFiles === undefined || Sk.builtinFiles.files[x] === undefined) throw "File not found: '" + x + "'"; return Sk.builtinFiles.files[x]; }
             });
             Sk.misceval.asyncToPromise(() => Sk.importMainWithBody("<stdin>", false, code, true))
                 .then(res => TriX_UI.log("Skulpt script finished.", "info"))
                 .catch(err => TriX_UI.log(err.toString(), "error"))
                 .finally(() => TriX_UI.setPythonStatus('ready', 'Skulpt'));
        }
    };
    PythonRunners.loadPyodideDynamically = function() { /* ... full function from previous versions ... */ };
    PythonRunners.initPyodide = async function() { /* ... full function from previous versions ... */ };
    PythonRunners.runPyodide = async function(code, packages) { /* ... full function from previous versions ... */ };
    
    // Minified Core Modules...
    const TabManager={/*...*/}; const ScriptManager={/*...*/}; const Executor={/*...*/}; const MultiTab={/*...*/};
    
    function init(username, gmFunctions) {
        _GM = gmFunctions;
        // Inject PyScript CSS here so it's available for the UI
        _GM.GM_addStyle(_GM.PYSCRIPT_CSS);
        TabManager.init(username);
    }
    
    return { init, ScriptManager, Executor, MultiTab, PythonRunners };
})();

QingJ © 2025

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