Can JavaScript extensions load on import?

Hello! I've been looking into using JavaScript extensions in Snap, and I was wondering if there's a way to make JavaScript load when you import the library. So far, the only way to load the JS that I can tell is to use the src_load primitive function, which can only be done through custom blocks. Is there a way to load JavaScript when you import the library? I'd like to be able to make custom block dropdown menus and add custom buttons to a custom category, which JS extensions can do, but seemingly not on library load? I'm kinda confused here, so if someone who knows more about this could help me out, I'd appreciate it.

If you mean turning js extensions on when you import a library, then there's no way of doing it. The reason behind this, is because it could lead to security issues. What if you load a library that does some shady things that you don't know about? You'd be able to look through the code and see what the library does before it runs. And if it's loaded through src…load(), then just copy and paste that url into the browser to see the js code it runs.

Also,

It loads only from a small set of chunks of JS code that belong to specific libraries. In user projects, the user has to click on something, or type something, to start a script running.

(But when you switch to another scene in a multi-scene project, you can arrange to autorun something in the new scene.)

Yes! As of a week ago in v8.1 extensions can be specified to auto-load a JS file if they have a global variable whose name is prefixed with __module__ storing the url of a JS extension. This lets lets extensions set up their environment (e.g. add buttons to the palette) and initialize right away without requiring the user to click on an "init" block first to load something external. Since libraries can also contain hidden blocks this global variable can be invisible to the user. We've added this so the official extensions (TuneScope, Beetleblocks, Turtlestitch, SciSnap, Netsblox, EDC Early Maths etc. etc.) can be made to be more accessible to their target audiences. Note that only urls that are "blessed" and allow-listed by us can be auto-loaded this way to guard you against security implications. So, it's probably not the feature you've been wanting or looking for. At this moment, no official extension is making use of this feature, but we're already working on some that will in a few months.

I've been wondering for a long time, how do I create hidden global variables?

To hide a variable, click on the menu item Hide blocks and then tick the variable

image

But do you mean something else?

Huh, I didn't know that. I'm gonna try and use that in the future when creating libraries.

It's a recent addition.

Awesome, this is exactly what I was looking for! (setting up custom buttons, block dropdowns, etc.) Thanks for the response, understandable that only allowed URLs can access it though.

If I have a Custom Block that needs javascript enabled, how would I go about that?
How would I enable the "JavaScript Extensions" option when the user clicks the green flag?

You don't. The whole idea is to protect users against malicious programs by require the actual human user to click an actual checkbox with their actual hand and mouse.