From 8aa54f34a635f06f9e2662ce73d3cc8b7bf34cd4 Mon Sep 17 00:00:00 2001 From: SimonShiki Date: Fri, 27 Oct 2023 15:01:12 +0000 Subject: [PATCH] :sparkles: feat: sideload from url in editor Signed-off-by: SimonShiki --- README-zh_CN.md | 2 +- README.md | 2 +- package.json | 2 +- src/injector/inject.ts | 46 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/README-zh_CN.md b/README-zh_CN.md index cd79d9c..10aab24 100644 --- a/README-zh_CN.md +++ b/README-zh_CN.md @@ -31,7 +31,7 @@ Chibi 是一个用户脚本,可以在任何基于 Scratch 的编辑器中加 # 🔥 使用方法 1. 安装一个用户脚本管理器扩展, 例如 Tampermonkey 或 Greasymonkey。 2. 打开[发布页](https://github.com/SimonShiki/chibi/releases), 点击一个版本来安装。 -3. 在'自定义积木'分类栏中找到'😎 Chibi Management'按钮,点击它即可侧载你的扩展。(你可能需要 5 秒来等待按钮出现) +3. 在'自定义积木'分类栏中找到'Open Frontend'按钮,点击它即可侧载你的扩展。(你可能需要 5 秒来等待按钮出现) > 嗯...也许因为编辑器差异,按钮并不会正常显示。不过别担心,你还可以通过其他的方式来侧载你的扩展! diff --git a/README.md b/README.md index 9111b40..534fd3b 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Chibi is a userscript which can load 3rd-party extensions in any Scratch-based e # 🔥 Usage 1. Install UserScript Manager like Tampermonkey or Greasymonkey. 2. Open [release](https://github.com/SimonShiki/chibi/releases), Then click one release to install. -3. Find '😎 Chibi Management' button in 'My Blocks' category. you can sideload your extension by clicking it. You may have to wait 5 seconds to make the button appeared. +3. Find 'Open Frontend' button in 'My Blocks' category. you can sideload your extension by clicking it. You may have to wait 5 seconds to make the button appeared. > Or... Due to editor differences, the button may not appear. There are other ways you can sideload extensions. diff --git a/package.json b/package.json index 22acba9..bb7cc13 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "chibi", "displayName": "Chibi", - "version": "3", + "version": "4", "description": "Load scratch extension everywhere.", "repository": "https://github.com/SimonShiki/chibi", "author": "SimonShiki", diff --git a/src/injector/inject.ts b/src/injector/inject.ts index 1eafe51..0bc8555 100644 --- a/src/injector/inject.ts +++ b/src/injector/inject.ts @@ -191,15 +191,35 @@ export function inject (vm: ChibiCompatibleVM) { ...args: unknown[] ) { const xmlList = originalProcedureCallback.call(this, workspace, ...args); + // Add separator and label + const sep = document.createElement('sep'); + sep.setAttribute('gap', '36'); + xmlList.push(sep); + const label = document.createElement('label'); + label.setAttribute('text', '😎 Chibi'); + xmlList.push(label); + // Add dashboard button const dashboardButton = document.createElement('button'); - dashboardButton.setAttribute('text', '😎 Chibi Management'); + dashboardButton.setAttribute('text', 'Open Frontend'); dashboardButton.setAttribute('callbackKey', 'CHIBI_FRONTEND'); workspace.registerButtonCallback('CHIBI_FRONTEND', () => { window.chibi.openFrontend(); }); xmlList.push(dashboardButton); + // Add load from url button + const sideloadButton = document.createElement('button'); + sideloadButton.setAttribute('text', 'Sideload from URL'); + sideloadButton.setAttribute('callbackKey', 'CHIBI_SIDELOAD_FROM_URL'); + workspace.registerButtonCallback('CHIBI_SIDELOAD_FROM_URL', () => { + const url = prompt('Enter URL'); + if (!url) return; + const mode = confirm('Running in sandbox?') ? 'sandboxed' : 'unsandboxed'; + window.chibi.loader.load(url, mode); + }); + xmlList.push(sideloadButton); + // Add chibi detection const mutation = document.createElement('mutation'); mutation.setAttribute('chibi', 'installed'); @@ -226,15 +246,36 @@ export function inject (vm: ChibiCompatibleVM) { xmlList: unknown[], ...args: unknown[] ) { + originalAddCreateButton_.call(this, workspace, xmlList, ...args); + // Add separator and label + const sep = document.createElement('sep'); + sep.setAttribute('gap', '36'); + xmlList.push(sep); + const label = document.createElement('label'); + label.setAttribute('text', '😎 Chibi'); + xmlList.push(label); + // Add dashboard button const dashboardButton = document.createElement('button'); - dashboardButton.setAttribute('text', '😎 Chibi Management'); + dashboardButton.setAttribute('text', 'Open Frontend'); dashboardButton.setAttribute('callbackKey', 'CHIBI_FRONTEND'); workspace.registerButtonCallback('CHIBI_FRONTEND', () => { window.chibi.openFrontend(); }); xmlList.push(dashboardButton); + // Add load from url button + const sideloadButton = document.createElement('button'); + sideloadButton.setAttribute('text', 'Sideload from URL'); + sideloadButton.setAttribute('callbackKey', 'CHIBI_SIDELOAD_FROM_URL'); + workspace.registerButtonCallback('CHIBI_SIDELOAD_FROM_URL', () => { + const url = prompt('Enter URL'); + if (!url) return; + const mode = confirm('Running in sandbox?') ? 'sandboxed' : 'unsandboxed'; + window.chibi.loader.load(url, mode); + }); + xmlList.push(sideloadButton); + // Add chibi detection const mutation = document.createElement('mutation'); mutation.setAttribute('chibi', 'installed'); @@ -247,7 +288,6 @@ export function inject (vm: ChibiCompatibleVM) { block.appendChild(field); block.appendChild(mutation); xmlList.push(block); - originalAddCreateButton_.call(this, workspace, xmlList, ...args); }; const workspace = blockly.getMainWorkspace(); workspace.getToolbox().refreshSelection();