-
Notifications
You must be signed in to change notification settings - Fork 88
添加Scbackend扩展 #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
添加Scbackend扩展 #318
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| // Name: Scbackend SDK | ||
| // ID: scbackendsdk | ||
| // Description: SDK for Scbackend | ||
| // By: XQYWorld | ||
| // Original: FurryR | ||
| // License: MPL-2.0 | ||
|
|
||
| (() => { | ||
| // src/l10n/index.js | ||
| var l10n_default = { | ||
| zh: { | ||
| "scbackendsdk.connect": "\u8FDE\u63A5\u5230 [remaddr] \u670D\u52A1\u5668\u4E0A\u7684 [instname] \u5B9E\u4F8B", | ||
| "scbackendsdk.connectwait": "\u8FDE\u63A5\u5230 [remaddr] \u670D\u52A1\u5668\u7684 [instname] \u5B9E\u4F8B \u5E76\u7B49\u5F85\u8FDE\u63A5\u5B8C\u6210", | ||
| "scbackendsdk.disconnect": "\u65AD\u5F00\u8FDE\u63A5", | ||
| "scbackendsdk.isconnected": "\u5DF2\u8FDE\u63A5\uFF1F", | ||
| "scbackendsdk.send": "\u53D1\u9001\u6D88\u606F [msg]", | ||
| "scbackendsdk.whenmessage": "\u5F53\u6536\u5230\u6D88\u606F", | ||
| "scbackendsdk.getmsg": "\u6536\u5230\u7684\u6D88\u606F" | ||
| }, | ||
| en: { | ||
| "scbackendsdk.connect": "Connect to [remaddr] server [instname] instance", | ||
| "scbackendsdk.connectwait": "Connect to [remaddr] server [instname] instance and wait", | ||
| "scbackendsdk.disconnect": "Disconnect", | ||
| "scbackendsdk.isconnected": "Is connected?", | ||
| "scbackendsdk.send": "Send message [msg]", | ||
| "scbackendsdk.whenmessage": "When message received", | ||
| "scbackendsdk.getmsg": "Received message" | ||
| } | ||
| }; | ||
|
|
||
| // src/index.js | ||
| (function(Scratch2) { | ||
| if (Scratch2.extensions.unsandboxed === false) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议直接感叹号
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tsup自动打包成这样的 |
||
| throw new Error("Sandboxed mode is not supported"); | ||
| } | ||
| const translate = function(key) { | ||
| const locale = globalThis.navigator.language.slice(0, 2); | ||
| if (l10n_default[locale] && l10n_default[locale][key]) { | ||
| return l10n_default[locale][key]; | ||
| } else { | ||
| return l10n_default["en"][key] || key; | ||
| } | ||
| }; | ||
|
Comment on lines
+38
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有runtime.getFormatMessage不用?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这玩意我真不知道我去改改 |
||
| class ScbackendSDK { | ||
| constructor() { | ||
| this.ws = null; | ||
| this.isopened = false; | ||
| } | ||
| getInfo() { | ||
| return { | ||
| id: "scbackendsdk", | ||
| name: "Scbackend SDK", | ||
| blocks: [ | ||
| { | ||
| blockType: Scratch2.BlockType.COMMAND, | ||
| opcode: "connect", | ||
| text: translate("scbackendsdk.connect"), | ||
| arguments: { | ||
| remaddr: { | ||
| type: Scratch2.ArgumentType.STRING, | ||
| defaultValue: "ws://localhost:3031" | ||
| }, | ||
| instname: { | ||
| type: Scratch2.ArgumentType.STRING, | ||
| defaultValue: "test" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| blockType: Scratch2.BlockType.COMMAND, | ||
| opcode: "connectwait", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 驼峰命名法这么难吗
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我记得opcode不能大写字母 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以的 |
||
| text: translate("scbackendsdk.connectwait"), | ||
| arguments: { | ||
| remaddr: { | ||
| type: Scratch2.ArgumentType.STRING, | ||
| defaultValue: "ws://localhost:3031" | ||
| }, | ||
| instname: { | ||
| type: Scratch2.ArgumentType.STRING, | ||
| defaultValue: "test" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| blockType: Scratch2.BlockType.COMMAND, | ||
| opcode: "disconnect", | ||
| text: translate("scbackendsdk.disconnect") | ||
| }, | ||
| { | ||
| blockType: Scratch2.BlockType.BOOLEAN, | ||
| opcode: "isconnected", | ||
| text: translate("scbackendsdk.isconnected") | ||
| }, | ||
| { | ||
| blockType: Scratch2.BlockType.COMMAND, | ||
| opcode: "send", | ||
| text: translate("scbackendsdk.send"), | ||
| arguments: { | ||
| msg: { | ||
| type: Scratch2.ArgumentType.STRING, | ||
| defaultValue: "Hello, world!" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| blockType: Scratch2.BlockType.EVENT, | ||
| opcode: "whenmessage", | ||
| text: translate("scbackendsdk.whenmessage"), | ||
| isEdgeActivated: false | ||
| }, | ||
| { | ||
| blockType: Scratch2.BlockType.REPORTER, | ||
| opcode: "getmsg", | ||
| text: translate("scbackendsdk.getmsg") | ||
| } | ||
| ], | ||
| menus: { | ||
| blockMenu: { | ||
| acceptReporters: true, | ||
| items: [{ text: translate("scbackendsdk.connect.wait"), value: true }, { text: translate("scbackendsdk.connect.dontwait"), value: false }] | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 啥玩意,都没用上
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 忘了删了 |
||
| } | ||
| }; | ||
| } | ||
| _doconnect(remaddr, dst, util, resolve, reject) { | ||
| this.ws = new WebSocket(remaddr); | ||
| this.ws.onopen = () => { | ||
| this.ws.send(JSON.stringify({ type: "handshake", dst: dst.toString() })); | ||
| }; | ||
| this.ws.onmessage = (event) => { | ||
| const msg = event.data; | ||
| const data = JSON.parse(msg); | ||
| if (data.type === "handshake" && data.status === "ok") { | ||
| this.isopened = true; | ||
| this.sessionid = data.sessionId; | ||
| if (resolve) resolve(); | ||
| } else if (data.type === "message") { | ||
| util.startHats("scbackendsdk_whenmessage").forEach((t) => { | ||
| t.initParams(); | ||
| t.pushParam("data", data.message); | ||
| }); | ||
| } | ||
| }; | ||
| this.ws.onclose = (event) => { | ||
| this.ws = null; | ||
| this.isopened = false; | ||
| if (reject) reject(event); | ||
| }; | ||
| this.ws.onerror = (error) => { | ||
| if (reject) reject(error); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议添加兜底的console error
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 嗯 |
||
| }; | ||
| } | ||
| connect(args, util) { | ||
| if (this.ws) { | ||
| this.ws.close(); | ||
| this.ws = null; | ||
| this.isopened = false; | ||
| } | ||
| this._doconnect(args.remaddr, args.instname, util); | ||
| } | ||
| async connectwait(args, util) { | ||
| await new Promise((resolve, reject) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 直接return这个promise呗
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 对吼 |
||
| this._doconnect(args.remaddr, args.instname, util, resolve, reject); | ||
| }); | ||
| } | ||
| disconnect() { | ||
| if (this.ws) { | ||
| this.ws.close(); | ||
| this.ws = null; | ||
| this.isopened = false; | ||
| } | ||
| } | ||
| isconnected() { | ||
| return this.isopened; | ||
| } | ||
| send(args) { | ||
| if (this.ws && this.isopened) { | ||
| this.ws.send(JSON.stringify({ type: "message", body: args.msg })); | ||
| } | ||
| } | ||
| getmsg(_args, util) { | ||
| return util.thread.getParam("data") || ""; | ||
| } | ||
| } | ||
| Scratch2.extensions.register(new ScbackendSDK()); | ||
| })(Scratch); | ||
| })(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不建议unicode编码,不太便于检查
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tsup打包出来就是这样的,你加入编辑器里就可以正常显示了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
网上有配置,直接放config里
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
请参考 https://esbuild.github.io/api/#charset