Importing predefined javascript where needed
The global scripting setting allows to reuse code in different shortcuts.
Problem : when you have shortcuts for a lot of different services, each needing custom code for handling responses, having everything in one place can become cumbersome to edit. In addition it doesn't feel right to evaluate a lot of unnecessary javascript just to use the one needed function.
It could be great to be able to import some targeted javascript in a shortcut context. Maybe via the Scripting Shortcuts (for example link a scripting shortcut to be evaluated prior to the current shortcut script blocks). I tried to do something like this with Multi-Shortcut, but each shortcut being evaluated in it's own context, one can't for example define a function in a scripting shortcut and use it in the following regular shortcut (and anyway it would feel a bit cumbersome to do it this way).
Maybe there is another way to achieve targeted code reusing, but I didn't found it for now.
~After getting a more attentive look at existing feature requests, I think what you do for https://github.com/Waboodoo/HTTP-Shortcuts/issues/253 will resolve this one too...~ this is not so sure after all 🤔
The changes from #253 will make it easier to reuse functionality, as it allows to have one shortcut execute another immediately. This however does not allow to use one shortcut to declare JS functions that another shortcut could then use (as is the case currently with global scripting code), because the 2 shortcuts won't share the same JS scope. So probably it won't significantly improve the situation for your usecase. You can give it a try though, as it's available already in the beta version, which you can install by joining the beta program.
Regarding your original problem description: did you notice any actual performance issue with having many functions defined in global scripting, or is it more of a wish for better organization? I find it difficult to prioritize this, as I have very little insight into how people use the global scripting feature. So far I've operated under the assumption that most users will only need a handful of defined functions in that scope (if any at all).
I took a look at the changelog and doc of executeShortcut and it will certainly be of good use to reuse some code :+1:
I didn't notice any performance issues with global scripting, and to be honest I don't have that many definitions in it. But I have more and more use cases that come to me, and it feels like it could soon become hard to maintain this part.
Anyway as you say it's more a wish for better organization and not at all a priority.
I'll keep it in mind, and I'll keep this issue open. Maybe there's some improvement of this kind that I can do somewhere down the line, but for now I will not prioritize it as it would be more of a "nice to have".
Hello,
I have very little insight into how people use the global scripting feature. So far I've operated under the assumption that most users will only need a handful of defined functions in that scope (if any at all).
I use it myself, to be able to determine if I'm currently connected to one of my own wifi SSID (I have 3, all sharing same prefix as a naming convention, so that's just a getWifiSSID().startsWith("MY_SSID_PREFIX") test)... or not.
As I only use shortcuts to trigger actions (or web server connection) on my home management system, something global does not hurt.
This allows switching URL from internal IP to external domain name depending if I'm at home or not, for some home management I host on my LAN.
I must confess I first copy/pasted the same code in every shortcuts "execute before" before seeing global scripting someday, searching for a (IMO more generic) way to add your own JS functions that may be used from existing "execute before/after" shortcuts scripting options.
Maybe this should be the way to go: Being able to add user JS functions in a common library?
Regards.
Thanks for this insight @yml78
My current thought is to replace the current Global Code feature with a feature that allows to create arbitrarily many separate named pieces of code that can then easily be imported or referenced in any shortcut's scripting. That way code can easily be reused and applied to only those shortcuts that actually need it. I believe that would solve this issue nicely. I have yet to plan this out and find time for it though, so I can't promise any particular timeline.
One solution could be enabling to install and use NPM modules.
Node.js apps have a package.json to specify the necessary modules. Either use that for each script or Maybe an adhoc require. That download the module the first time.
Not completely easy without running NPM itself since there is the need of downloading the dependent modules.
I think bringing NPM into this would be overkill, and probably not technically feasible or suitable. The JS runtime that's currently used is a watered down version of Node, but that is an implementation detail that might change in the future, so I don't want to build more onto that.