# How to make a Snap! block listener in javascript?

Basically I'm wondering is there a way to detect which snap block that is in the script and detect how many blocks is in that script?

Welcome to the forums!
This should be in the "Help With Snap" Category.
As for the problem @sathvikrias has some blocks that detects all blocks in a script. Just drag a "length of" block in the lists category, then drag @sathvikrias's block into the end space.

Ahh I see but I want to implement some kind of event listener in javascript that can detect how many blocks that are placed in that script and possibly detect what kind of snap block is in there

Okay I will check it out, do you perhaps have the link to his block?

Oh sorry, its @sathvikrias's blocks. Here: Snap! 6.8.1 Build Your Own Blocks It will take a while to load.

Not with JavaScript

Thank you for your suggestion but sadly it's not what I'm looking for I'm still pretty novice in Javascript and in snap so I'm sorry if I confused you maybe I should rephrase my question

I'm trying to make a web app that display challenge that need to be solved in Snap! and then the web app will detect how many block that someone use in Snap! so that the web app can compare the result by comparing it with the data of shortest amount of block required to achieve the same result on the challenge.

Example:

The picture above is very roughly what I'm trying to accomplish

I know of three Snap! autograder efforts, and two of them are presenting at Snap!Con this summer. Counting blocks is the least of the issues, since they all want to embed the Snap! instance in a web page that explains the task. @cycomachead, @brollb, any references you could post here?

By the way, number of blocks isn't a very good figure of merit, imho. If a solution has a lot of blocks because it misses opportunities for procedural abstraction, that's bad, but if it has a lot of blocks because it handles more cases, or interacts with the user in a richer way, that's good.

Also, people might try to code golf and create messy solutions

Yeah I do agree that it is not a very good way to grade it this way but the reason I chose this because I thought that this kind of implementation would be simpler to implement quickly and the plan is that later down the line I will expand the grading algorithm, but for now I just want to see if it is possible to do it this way.

I know it is possible. I've seen blocks that report all the scripts in a sprite, and a block that reports the number of blocks in a script. Not to mention, in dev mode, there is a stack size reporter that reports the stack size of the script it's in.

If that's the case is it possible to implement that outside of Snap! In JavaScript? Because with the blocks you have to do it inside of snap right?

I'm not sure about outside of snap, like outside an iframe, but it might be possible in the console.

Please note that I don't know enough javascript to do this. I'm just saying what I think is possible.

If you want to host your own copy of Snap! it can be quite easy. But to use standard Snap! instance every challenge project must be modified to evaluate the solution. For e.g. script running at the stage may check the solution and submit the result to an external website with the URL block.

You can use window.parent to do things in the parent window, e.g. the one the iframe is hosted in.

ok

Yes I intend to integrate my own modified copy of Snap! Into the webapp itself

does this enable you to detect block inside the iframe? I assume iframe is where the block is placed right?

Probably, if you could figure out where the iframe is located in memory.

an iframe has nothing to do with snap. It's just an embedded webpage, like youtube videos in other websites.