SkPatcher

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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();
})();