Get list of all blocks in a Google Blockly program

1,110 views
Skip to first unread message

Spencer Chlebina

unread,
Jun 7, 2018, 9:42:28 PM6/7/18
to Blockly
Hey Guys,

I am trying to write a function which will get a list of all the blocks used in a Google Blockly program when the user saves it, and then save this list to MongoDB. I don't need help with MongoDB or anything, but I cannot find the Blockly function which will give me a list of all the blocks used. I've tried getAllBlocks(), which gives me a list of strings which are like pseudo-code and is not parsed into the individual blocks used, and I have tried getTopBlocks(), which just gives me a single string of pseudo-code. I would prefer not to use regex to try to parse the pseudo-code because there would be tons of ways the users could probably make programs which would break my code/lead to faulty data. Does anyone know if there is a function which I can use that will actually give me a list of the individual blocks used? Or, if there is anyone who has had this problem/done this before? I would really appreciate the help!

Thanks,
SC

Neil Fraser

unread,
Jun 7, 2018, 10:23:06 PM6/7/18
to blo...@googlegroups.com
getAllBlocks() and getTopBlocks() both return an array of the block
objects, not strings.
> --
> You received this message because you are subscribed to the Google Groups "Blockly" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Neil Fraser
https://neil.fraser.name

Spencer Chlebina

unread,
Jun 7, 2018, 11:18:57 PM6/7/18
to Blockly
Nevermind, I found a way. I used Blockly.Xml.workspaceToDom() and Blockly.Xml.domToText() to get the text of the XML and then just search for block type="*".

Neil Fraser

unread,
Jun 8, 2018, 11:34:55 AM6/8/18
to blo...@googlegroups.com
Note that a search like this can be subverted if someone (maliciously) creates a comment on a block like this one:

Screen Shot 2018-06-08 at 08.32.35.png

The resulting XML would be:

​<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="logic_negate" id="F$EEr,@FV7)~V$fZ#BJ)" x="163" y="188">
    <comment pinned="true" h="80" w="160">block type="hello"</comment>
  </block>
</xml>



--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages