Skip to content

浏览器的 window 和 worker 环境中由CthulhuRs服务器提供的变量、api一致

scopeId

ts
self['CTHULHU_SCOPE_ID'];//当前scope的Id

CthulhuServer

ts
function post(url, json) {
    return new Promise((rs, rj) => {
        let httpRequest = new XMLHttpRequest();
        httpRequest.open("POST", url, true);
        httpRequest.setRequestHeader("Content-Type", "application/json");
        httpRequest.send(json);
        httpRequest.onreadystatechange = () => {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    let data = JSON.parse(httpRequest.responseText);
                    rs(data)
                } else {
                    rj(httpRequest.response)
                }
            }
        }
    })
};
self.CthulhuServer = class Server {
    constructor(id) {
        if (!id) throw new Error("插件id不能为空")
        this.url = `"https://${id}.plugin.cthulhu.server"`;
        this.scopeId = self['CTHULHU_SCOPE_ID'] || ''
    }

    ask(key, data) {
        let json = JSON.stringify(data);
        let url = `${this.url}/ask?key=${key}&scopeId=${this.scopeId || ''}`
        return post(url, json)
    }
};

Released under the MIT License.