From e13dd3a39113f106147256ce257f96df26a9e21e Mon Sep 17 00:00:00 2001 From: SimonShiki Date: Sat, 4 Nov 2023 16:21:51 +0000 Subject: [PATCH] :sparkles: feat: sideload from file temporarily Signed-off-by: SimonShiki --- src/injector/inject.ts | 53 +++++++++++++++++++++++++++++++++++++++++- src/l10n/l10n.json | 2 ++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/injector/inject.ts b/src/injector/inject.ts index 9df1615..3fc5691 100644 --- a/src/injector/inject.ts +++ b/src/injector/inject.ts @@ -209,6 +209,8 @@ export function inject (vm: ChibiCompatibleVM) { const envs: Record = {}; const sideloadIds: string[] = []; for (const [extId, ext] of window.chibi.loader.loadedScratchExtension.entries()) { + // Ignore object urls since it only works at present. + if (ext.url.startsWith('blob:')) continue; urls[extId] = ext.url; envs[extId] = ext.env; sideloadIds.push(extId); @@ -391,7 +393,31 @@ export function inject (vm: ChibiCompatibleVM) { : 'unsandboxed'; window.chibi.loader.load(url, mode); }); - xmlList.push(sideloadButton); + + // Add temporarily load from file button + const sideloadTempButton = document.createElement('button'); + sideloadTempButton.setAttribute('text', format('chibi.sideloadTemporarily')); + sideloadTempButton.setAttribute('callbackKey', 'CHIBI_SIDELOAD_FROM_FILE_TEMPORAILY'); + workspace.registerButtonCallback('CHIBI_SIDELOAD_FROM_FILE_TEMPORAILY', () => { + const input = document.createElement('input'); + input.setAttribute('type', 'file'); + input.setAttribute('accept', '.js'); + input.setAttribute('multiple', 'true'); + input.onchange = async (event: Event) => { + const files = (event.target as HTMLInputElement).files; + if (!files) return; + for (const file of files) { + const fileName = file.name; + + const url = URL.createObjectURL(file); + const mode = confirm(format('chibi.loadInSandbox')) + ? 'sandboxed' : 'unsandboxed'; + window.chibi.loader.load(url, mode); + } + }; + input.click(); + }); + xmlList.push(sideloadTempButton); // Add chibi detection const mutation = document.createElement('mutation'); @@ -448,6 +474,31 @@ export function inject (vm: ChibiCompatibleVM) { }); xmlList.push(sideloadButton); + // Add temporarily load from file button + const sideloadTempButton = document.createElement('button'); + sideloadTempButton.setAttribute('text', format('chibi.sideloadTemporarily')); + sideloadTempButton.setAttribute('callbackKey', 'CHIBI_SIDELOAD_FROM_FILE_TEMPORAILY'); + workspace.registerButtonCallback('CHIBI_SIDELOAD_FROM_FILE_TEMPORAILY', () => { + const input = document.createElement('input'); + input.setAttribute('type', 'file'); + input.setAttribute('accept', '.js'); + input.setAttribute('multiple', 'true'); + input.onchange = async (event: Event) => { + const files = (event.target as HTMLInputElement).files; + if (!files) return; + for (const file of files) { + const fileName = file.name; + + const url = URL.createObjectURL(file); + const mode = confirm(format('chibi.loadInSandbox')) + ? 'sandboxed' : 'unsandboxed'; + window.chibi.loader.load(url, mode); + } + }; + input.click(); + }); + xmlList.push(sideloadTempButton); + // Add chibi detection const mutation = document.createElement('mutation'); mutation.setAttribute('chibi', 'installed'); diff --git a/src/l10n/l10n.json b/src/l10n/l10n.json index 4f701ed..be53274 100644 --- a/src/l10n/l10n.json +++ b/src/l10n/l10n.json @@ -2,6 +2,7 @@ "zh-cn": { "chibi.openFrontend": "打开面板", "chibi.sideload": "从 URL 侧载扩展", + "chibi.sideloadTemporarily": "从本地临时侧载扩展", "chibi.errorIgnored": "在加载扩展扩展时出现错误。为了避免加载进程的中断,此错误已被忽略。", "chibi.tryLoad": "🤨 项目正从 {url} 加载扩展 {extensionURL}。要加载么?", "chibi.tryLoadInEnv": "🤨 项目正以 {env} 模式从 {url} 加载扩展 {extensionURL}。要加载么?", @@ -11,6 +12,7 @@ "en": { "chibi.openFrontend": "Open Frontend", "chibi.sideload": "Sideload from URL", + "chibi.sideloadTemporarily": "Sideload from File temporarily", "chibi.errorIgnored": "Error occurred while sideloading extension. To avoid interrupting the loading process, we chose to ignore this error.", "chibi.tryLoad": "🤨 Project is trying to sideloading {extensionURL} from {url}. Do you want to load?", "chibi.tryLoadInEnv": "🤨 Project is trying to sideloading {extensionURL} from {url} in {env} mode. Do you want to load?",