Skip to content

Commit

Permalink
🔧 chore: split blockly and vm injection
Browse files Browse the repository at this point in the history
Signed-off-by: SimonShiki <[email protected]>
  • Loading branch information
SimonShiki committed Mar 18, 2024
1 parent 8492310 commit 89795af
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/charlotte.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { version } from '../package.json';
export { inject } from './injector/inject';
export { injectVM, injectBlockly } from './injector/inject';
export { settings } from './util/settings';
import openFrontend from './frontend';
export {
Expand Down
2 changes: 2 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable multiline-comment-style */
/* eslint-disable @typescript-eslint/triple-slash-reference */
/// <reference path="node_modules/@turbowarp/types/index.d.ts" />
/// <reference path="node_modules/format-message/types.d.ts" />
/// <reference path="./loader/loader" />
/// <reference path="./loader/make-ctx" />
/// <reference path="./util/settings" />
Expand All @@ -20,6 +21,7 @@ declare interface Window {
env: string;
}
>;
format?: formatMessage;
openFrontend(): void;
};
Scratch?: Context;
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { trap, inject } from './injector/inject';
import { trap, injectVM, injectBlockly } from './injector/inject';
import { log } from './util/log';
const open = window.open;
// @ts-expect-error defined in webpack define plugin
log(`eureka-loader ${__EUREKA_VERSION__}`);
// Try injecting chibi into the page.
const [vm, blockly] = await trap(open);
if (!!vm) {
if (vm) {
// Alright we got the virtual machine, start the injection.
window.eureka.vm = vm;
inject(vm, blockly);
injectVM(vm);
injectBlockly(blockly);
} else {
// This is not a Scratch page, stop injecting.
log(`Cannot find vm in this page, stop injecting.`);
Expand Down
30 changes: 21 additions & 9 deletions src/injector/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,7 @@ export function trap (open: typeof window.open): Promise<[EurekaCompatibleVM | n
return [null, null];
});
}

/**
* Inject into the original virtual machine.
* @param vm {EurekaCompatibleVM} Original virtual machine instance.
*/
export function inject (vm: EurekaCompatibleVM, blockly: any) {
const loader = (window.eureka.loader = new EurekaLoader(vm));
const originalLoadFunc = vm.extensionManager.loadExtensionURL;
function setupFormat (vm: EurekaCompatibleVM) {
const getLocale = vm.getLocale;
const format = formatMessage.namespace();
format.setup({
Expand All @@ -185,6 +178,18 @@ export function inject (vm: EurekaCompatibleVM, blockly: any) {
generateId: (defaultMessage: string) => `${defaultMessage}`,
translations: l10n
});
window.eureka.format = format;
return format;
}

/**
* Inject into the original virtual machine.
* @param vm {EurekaCompatibleVM} Original virtual machine instance.
*/
export function injectVM (vm: EurekaCompatibleVM) {
const loader = (window.eureka.loader = new EurekaLoader(vm));
const originalLoadFunc = vm.extensionManager.loadExtensionURL;
const format = setupFormat(vm);
vm.extensionManager.loadExtensionURL = async function (extensionURL: string, ...args: []) {
if (extensionURL in window.eureka.registeredExtension) {
const { url, env } = window.eureka.registeredExtension[extensionURL];
Expand Down Expand Up @@ -453,7 +458,13 @@ export function inject (vm: EurekaCompatibleVM, blockly: any) {
return originalGetOrderFunc.call(this, extensions, ...args);
};
}
}

export function injectBlockly (blockly: any) {
const format = window.eureka.format;
if (!format) {
return error('You should inject VM first');
}
if (typeof blockly === 'object') {
window.eureka.blockly = blockly;
const originalAddCreateButton_ = blockly.Procedures.addCreateButton_;
Expand All @@ -472,7 +483,7 @@ export function inject (vm: EurekaCompatibleVM, blockly: any) {
warn('Cannot find real blockly instance, try alternative method...');
const originalProcedureCallback =
window.Blockly?.getMainWorkspace()?.toolboxCategoryCallbacks_?.PROCEDURE;
if (!originalProcedureCallback) {
if (typeof originalProcedureCallback !== 'function') {
error('alternative method failed, stop injecting');
return;
}
Expand All @@ -493,6 +504,7 @@ export function inject (vm: EurekaCompatibleVM, blockly: any) {
workspace.toolboxRefreshEnabled_ = true;
}
}

function injectToolbox (
xmlList: HTMLElement[],
workspace: EurekaCompatibleWorkspace,
Expand Down

0 comments on commit 89795af

Please sign in to comment.