Skip to content

Interface: CharlotteAPI

addons/api/api.CharlotteAPI

Table of contents

Methods

Methods

appendToSharedSpace

appendToSharedSpace(element, space, order?): boolean

Append a element to a specific position in Scratch editor.

Parameters

NameTypeDescription
elementHTMLElementThe element you want to append to
spaceSharedSpaceWhere do you want to append
order?numberThe the order of the added element.

Returns

boolean

Whether element is applied successfully.

Example

ts
const button = document.createElement('button');
button.className = 'charlotteButton';
button.innerHTML = '🌠 Charlotte';
button.addEventListener('click', () => {
    addon.app.openFrontend();
});

addon.api.appendToSharedSpace(button, 'afterSoundTab');

createBlockContextMenu

createBlockContextMenu(callback, options): any

Creates an item in the editor Blockly context menu by callback. Available only when Blockly exists.

Parameters

NameTypeDescription
callbackContextMenuCallbackA function to modify context menu.
optionsPartial<BaseContextMenuOptions>Specify the callback's scope.

Returns

any

Example

Here's a example to modify context menu in Blockly:

ts
addon.api.createBlockContextMenu((items: ContextMenuItem[], block: Blockly.Block, event: Blockly.Event) => {
    items.push({
        {
            enabled: true, // Whether the option is available
            text: '🌠 Meteor shower begins', // The display text of the option
            callback: () => console.log('🌠🌠🌠🌠'), // Triggers when option clicked
            separator: false // Whether displays a separator at the bottom of current option
        }
    });
}, {blocks: true}); // Only display in block's context menu

getBlockly

getBlockly(): Promise<RealBlockly>

Get Scratch's Blockly instance asynchronously. This won't available if you're in project page.

Returns

Promise<RealBlockly>

a promise that returns Blockly instance


getPlatform

getPlatform(): string

Get current platform.

Returns

string

platform alias, such as cc, cocrea


getReactInternalPrefix

getReactInternalPrefix(): string

Get current page's most likely react internal key. This method does not mean that react exists in this interface. This method is just a simple inference based on the platform.

Returns

string

React's internal prefix


getRedux

getRedux(): Promise<CharlotteRedux>

Get Charlotte's redux instance asynchronously. This won't available if current page doesn't have redux or hide it (Eg: Codingclip)

Returns

Promise<CharlotteRedux>

a promise that returns Charlotte's redux instance


getVM

getVM(): Promise<VM>

Get Scratch's VM instance asynchronously.

Returns

Promise<VM>

a promise that returns VM instance


hashedScratchClass

hashedScratchClass(...possibleClassNames): string

Get hashed className from unhashed className.

Parameters

NameTypeDescription
...possibleClassNamesstring[]all possible classNames

Returns

string

hashed className


pendingReduxState

pendingReduxState(condition, scope?): Promise<void>

Waiting until redux state marches condition.

Parameters

NameTypeDescription
conditionStatePendingConditionThe function to judge whether current state matches condition.
scope?string[]Which actions will trigger condition function.

Returns

Promise<void>

Example

ts
await addon.api.pendingReduxState((state) => state.scratchGui?.mode?.isFullScreen);
console.log('The stage is full-screen!');

waitForElement

waitForElement(selector, options?): Promise<Element>

Waiting until selected element rendered.

Parameters

NameTypeDescription
selectorstringSelector string, syntax is same as querySelector.
options?WaitForElementOptions-

Returns

Promise<Element>

a promise that resolves requested element.


xmlEscape

xmlEscape(unsafe): string

Escape a string to be safe to use in XML content. CC-BY-SA: hgoebl https://stackoverflow.com/questions/7918868/ how-to-escape-xml-entities-in-javascript

Parameters

NameTypeDescription
unsafestringUnsafe string.

Returns

string

XML-escaped string, for use within an XML tag.

Powered by TypeDoc & VitePress.