Skip to content

Commit

Permalink
✨ feat: settings
Browse files Browse the repository at this point in the history
Signed-off-by: SimonShiki <[email protected]>
  • Loading branch information
SimonShiki committed Nov 2, 2023
1 parent 900d633 commit 2ca2b6f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chibi",
"displayName": "Chibi",
"version": "4",
"version": "5",
"description": "Load scratch extension everywhere.",
"repository": "https://github.com/SimonShiki/chibi",
"author": "SimonShiki",
Expand Down
30 changes: 29 additions & 1 deletion src/frontend/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Settings, getSettingsFromStorage } from '../util/settings';

let dashboardWindow: Window | null = null;

interface MothExtensionInfo {
Expand All @@ -17,7 +19,16 @@ interface MothDispatchedAllocate {
type: 'allocate';
}

type MothDispatched = MothDispatchedAllocate | MothDispatchedLoad;
interface MothDispatchedUpdateSettings {
type: 'updateSettings';
item: {
name: keyof Settings,
value: Settings[keyof Settings]
}
}


type MothDispatched = MothDispatchedAllocate | MothDispatchedLoad | MothDispatchedUpdateSettings;

/**
* Get all extensions.
Expand Down Expand Up @@ -61,6 +72,13 @@ async function messageHandler (event: MessageEvent) {
},
'*'
);
dashboardWindow?.postMessage(
{
type: 'settings',
settings: getSettingsFromStorage()
},
'*'
);
break;
case 'load':
// Load an extension.
Expand All @@ -76,6 +94,16 @@ async function messageHandler (event: MessageEvent) {
'*'
);
break;
case 'updateSettings':
window.chibi.settings[event.data.item.name] = event.data.item.value;
dashboardWindow?.postMessage(
{
type: 'settings',
settings: getSettingsFromStorage()
},
'*'
);
break;
}
}

Expand Down
35 changes: 20 additions & 15 deletions src/injector/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,26 @@ export function inject (vm: ChibiCompatibleVM) {
if (extensionURL in window.chibi.registeredExtension) {
const { url, env } = window.chibi.registeredExtension[extensionURL];
try {
const res = env
? confirm(
format('chibi.tryLoadInEnv', {
extensionURL,
url,
env
})
)
: confirm(
format('chibi.tryLoadInEnv', {
extensionURL,
url
})
);
if (res) {
let whetherSideload: boolean = false;
if (window.chibi.settings.noConfirmDialog) {
whetherSideload = true;
} else {
whetherSideload = env ?
confirm(
format('chibi.tryLoadInEnv', {
extensionURL,
url,
env
})
)
: confirm(
format('chibi.tryLoadInEnv', {
extensionURL,
url
})
);
}
if (whetherSideload) {
await loader.load(
url,
(env
Expand Down
2 changes: 1 addition & 1 deletion src/loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ChibiLoader {
* object on the window, because the extension should not depend on unknown external
* environment.
*/
if (!('Scratch' in window)) {
if (!('Scratch' in window) && !window.chibi.settings.dontExposeCtx) {
window.Scratch = ctx;
}
ctx.extensions.register = (extensionObj: ExtensionClass) => {
Expand Down
6 changes: 2 additions & 4 deletions src/util/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ export interface Settings {
convertProcCall: boolean;
dontExposeCtx: boolean;
noConfirmDialog: boolean;
takeOverUrlLoadRequest: boolean;
}

const puppet: Settings = {
convertProcCall: true,
dontExposeCtx: false,
noConfirmDialog: false,
takeOverUrlLoadRequest: false
noConfirmDialog: false
};

const SETTINGS_KEY = '$CHIBI_SETTINGS';
Expand All @@ -18,7 +16,7 @@ if (!window.localStorage.getItem(SETTINGS_KEY)) {
window.localStorage.setItem(SETTINGS_KEY, JSON.stringify(puppet));
}

function getSettingsFromStorage () {
export function getSettingsFromStorage () {
try {
const item = window.localStorage.getItem(SETTINGS_KEY);
if (!item) return null;
Expand Down

0 comments on commit 2ca2b6f

Please sign in to comment.