XesSkPatcher

在学而思编程上修改了 Skulpt(WebPy 引擎)以添加加载 js 库功能。

目前為 2024-03-23 提交的版本,檢視 最新版本

// ==UserScript==
// @name         XesSkPatcher
// @namespace    https://code.xueersi.com/
// @version      0.1
// @description  在学而思编程上修改了 Skulpt(WebPy 引擎)以添加加载 js 库功能。
// @author       lrs2187
// @license MIT
// @match        *://code.xueersi.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    var counts = 0;

    function waitForSkulpt() {
        if (typeof Sk === 'undefined') {
            counts = counts + 1;
            if (counts >= 60)
            {
                console.log("XesSkPatcher.waitForSkulpt 1 minutes out. Unlistening...");
                return;
            }
            setTimeout(waitForSkulpt, 1000);
        } else {
            console.log("XesSkPatcher.waitForSkulpt Sk created! Injecting...");
            injectCustomImport();
        }
    }
    function SP_get_lib_code(a)
    {
        var jsThird = Sk._SP_third_lib[a + ".js"];
        if (jsThird) {
            return {
                funcname: "$builtinmodule",
                code: Sk._SP_third_lib[a + ".js"],
            };
        }
        var pyThird = Sk._SP_third_lib[a + ".py"];
        if (pyThird) {
            return Sk.compile(Sk._SP_third_lib[a + ".py"], a + ".py", "exec", true);
        }

        return Sk.origin_import(a);
    }
    function injectCustomImport() {
        Sk._SP_third_lib = {};
        Sk.origin_import = Sk.loadExternalLibrary;
        Sk.loadExternalLibrary = SP_get_lib_code;

        Sk.builtins.__import__ = function(lib_url, lib_name) {
            console.log('XesSkPatcher.__import__ got url ', lib_url, ' name ', lib_name);

            var request = new XMLHttpRequest();
            request.open('GET', lib_url.v, false);
            request.send(null);
            if (request.status === 200) {
                var data = request.responseText;
                console.log("XesSkPatcher.__import__ Loaded " + lib_url.v + "! Adding lib...");

                var file_data = lib_url.v.split(".");
                var type = file_data[file_data.length - 1];
                Sk._SP_third_lib[lib_name.v + "." + type] = data;
            } else {
                console.log("XesSkPatcher.__import__ Can't load " + lib_url.v + ".");
            }
        }
        console.log("XesSkPatcher.injectCustomImport Success to add __import__ function!");
    }

    console.log("XesSkPatcher Try to listen Sk.");
    waitForSkulpt();
})();

QingJ © 2025

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