SkPatcher

修改 Skulpt 以添加加载 js 库功能。

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         SkPatcher
// @version      0.2.4
// @description  修改 Skulpt 以添加加载 js 库功能。
// @author       lrs2187
// @license MIT
// @match        *://code.xueersi.com/*
// @match        *://turtle.codemao.cn/*
// @match        *://coding.qq.com/*
// @grant        none
// @run-at       document-start
// @namespace https://code.xueersi.com/
// ==/UserScript==

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

    function waitForSkulpt() {
        if (typeof window.Sk === 'undefined') {
            counts = counts + 1;
            if (counts >= 60)
            {
                console.log("SkPatcher.waitForSkulpt 一分钟过了 Sk 还没发现,正在取消侦听...");
                return;
            }
            setTimeout(waitForSkulpt, 1000);
        } else {
            console.log("SkPatcher.waitForSkulpt 发现了 Sk 对象!正在注入...");
            injectCustomImport();
        }
    }

    function injectCustomImport() {
        window.Sk.builtins.__import__ = function(lib_url, lib_name) {
            console.log('SkPatcher.__import__ 获取到URL ', lib_url, ' 以及名称 ', 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("SkPatcher.__import__ 成功加载 " + lib_url.v + "!正在添加库...");

                var file_data = lib_url.v.split(".");
                var type = file_data[file_data.length - 1];
                var file_name = "src/lib/" + lib_name.v + "." + type;
                Sk.builtinFiles.files[file_name] = data;
            } else {
                console.log("SkPatcher.__import__ 无法加载 " + lib_url.v + "");
            }
        }
        console.log("SkPatcher.injectCustomImport 成功添加 __import__ 函数!");
    }

    console.log("SkPatcher 尝试侦听 Sk 对象...");
    waitForSkulpt();
})();