Ko09 userscript ;; possible to include one userscript in another by reference?


Is it possible to create a userscript and use it as an inclusion library in one or more other userscripts, kind of like the python import statement?

While this is technically possible I would recommend strongly against it, as that is not how userscripts are intended to be used. Instead what you should do is create an addon.

If you still want to use userscripts, you can get the path of the currently executed userscript with komodo.macro.path.

Thanks for the reply.

As I am slowly developing a list of utility functions for use in my current and future userscripts, I would like to consolidate those utility functions in a single JavaScript file, and then somehow include that in every userscript that needs those utility functions, to minimize redundant code. I have searched online and found countless discussions as to various ways to do something like this, but all of the solutions I have found involve either assuming that the JavaScript file is being pulled into an HTML file, or are far more complex solutions that don’t look like they would even be allowed in a Komodo IDE userscript. So my need is similar or identical to what @dreftymac was inquiring about.

I don’t see how knowing the path of the currently executed userscript helps with including a second userscript. (I’m very new to userscripts.) Does anyone know how to do this? Thanks!

1 Like

Well if you know the path of the current userscript then you have the relative path to the other userscript as well.

var koFile = require("ko/file");
var userscript1 = komodo.macro.path;
var userscript2 = koFile.join(koFile.dirname(userscript1), "userscript2.komodotool");

Thank you for explaining that. It appears that the code is assuming that the second userscript is in the same directory as the current one. That would work in my situation, because I currently just have a few userscripts and they are all in the same (default) directory.

Now that we know the location of that second userscript, how do we include it in the current one so that its functions can be utilized in the current script?

That’s not how it works, you can execute it, you cannot include a userscript. Userscripts are formatted with additional meta data, you cannot just read them as JavaScript files.

It really doesnt sound like what you’re doing lends itself to UserScripts. You’re using UserScripts for something that they were not designed for. Have a look at addons instead, in particular check out the addon bootstrapper:

Yes, thanks, I had seen, in your first reply to the OP, your recommendation to use add-ons instead, but I also saw the mention of it being technically possible using userscripts, and was hoping to do it that way with minimal work or complications.

Also, I recently read and followed the documentation for creating a preliminary Komodo add-on, and noticed that the files generated are using the legacy JAR/XUL API for Mozilla add-ons, which I believe is now deprecated – at least based upon all the Mozilla documentation I was reading a couple weeks ago when creating a couple Firefox add-ons using the (newer) WebExtensions API.

1 Like

They are deprecated for Firefox, they are not deprecated for us. Firefox is going in a very different direction with their developers than we are. We want developers to be empowered in writing their addons, whereas Firefox is going more the Chrome route.

I was wrong in my communication before; you can call UserScript from one another, but you cannot include them. I apologise, I did not think this through properly. That said creating an addon should be very simple with the Addon Bootstrapper I linked.

Thanks for the clarifications!