Skip to content

Commit

Permalink
✨ feat: sideload from file temporarily
Browse files Browse the repository at this point in the history
Signed-off-by: SimonShiki <[email protected]>
  • Loading branch information
SimonShiki committed Nov 4, 2023
1 parent e6cf786 commit e13dd3a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/injector/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ export function inject (vm: ChibiCompatibleVM) {
const envs: Record<string, string> = {};
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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions src/l10n/l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"zh-cn": {
"chibi.openFrontend": "打开面板",
"chibi.sideload": "从 URL 侧载扩展",
"chibi.sideloadTemporarily": "从本地临时侧载扩展",
"chibi.errorIgnored": "在加载扩展扩展时出现错误。为了避免加载进程的中断,此错误已被忽略。",
"chibi.tryLoad": "🤨 项目正从 {url} 加载扩展 {extensionURL}。要加载么?",
"chibi.tryLoadInEnv": "🤨 项目正以 {env} 模式从 {url} 加载扩展 {extensionURL}。要加载么?",
Expand All @@ -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?",
Expand Down

0 comments on commit e13dd3a

Please sign in to comment.