@@ -231,19 +231,51 @@ async function alertForUpdates() {
231231 }
232232}
233233
234- function getScratchBlocks ( ) {
235- var blocksWrapper = document . querySelector (
236- 'div[class^="gui_blocks-wrapper"]'
237- ) ;
238- var key = Object . keys ( blocksWrapper ) . find ( ( key ) =>
239- key . startsWith ( "__reactInternalInstance$" )
240- ) ;
241- const internal = blocksWrapper [ key ] ;
242- var recent = internal . child ;
243- while ( ! recent . stateNode ?. ScratchBlocks ) {
244- recent = recent . child ;
234+ window . __steScratchBlocks = null
235+
236+ async function _getBlocksWrapperComponent ( ) {
237+ const BLOCKS_CLASS = '[class^="gui_blocks-wrapper"]' ;
238+ let elem = document . querySelector ( BLOCKS_CLASS ) ;
239+ if ( ! elem ) {
240+ elem = document . querySelector ( BLOCKS_CLASS ) ;
245241 }
246- return recent . stateNode . ScratchBlocks || null ;
242+ return _getBlocksComponent ( elem ) ;
243+ }
244+
245+ function _getBlocksComponent ( wrapper ) {
246+ const internal = wrapper [ getInternalKey ( wrapper ) ] ;
247+ let childable = internal ;
248+ while ( ( ( childable = childable . child ) , ! childable || ! childable . stateNode || ! childable . stateNode . ScratchBlocks ) ) { }
249+ return childable ;
250+ }
251+
252+ function getInternalKey ( elem ) {
253+ return Object . keys ( elem ) . find ( ( key ) => key . startsWith ( "__react" ) )
254+ }
255+
256+ function _getBlocksComponent ( wrapper ) {
257+ const internal = wrapper [ getInternalKey ( wrapper ) ] ;
258+ let childable = internal ;
259+ while ( ( ( childable = childable . child ) , ! childable || ! childable . stateNode || ! childable . stateNode . ScratchBlocks ) ) { }
260+ return childable ;
261+ }
262+
263+ async function getBlockly ( ) {
264+ const childable = await _getBlocksWrapperComponent ( ) ;
265+ return childable . stateNode . ScratchBlocks
266+ }
267+
268+ window . __steRedux . target . addEventListener ( "statechanged" , async function ( ) {
269+ try {
270+ let blockly = await getBlockly ( )
271+ if ( blockly ) {
272+ window . __steScratchBlocks = blockly
273+ }
274+ } catch ( err ) { }
275+ } )
276+
277+ function getScratchBlocks ( ) {
278+ return window . __steScratchBlocks
247279}
248280
249281ScratchTools . waitForElements (
0 commit comments