1

I have developed a very simple Thunderbird extension. There is one simple .xul file which refers via script-tag to a .js file. In that javascript-file I am implementing an event listener on the compose-send-message event. When the send-button is clicked, I want to encrypt the message in the mail-body and replace it with that newly encrypted text before sending. Replacing text in the body-part of Thunderbird worked well, but I am not able to refer to another javascript file with a simple function call like

var encryptedData = encryption.encrypt(data);

for the file encryption.js, which exclusively handles the encryption of said email-text before sending. Both files are in the same directory, so normally they should be able to refer to each other, shouldn't they? But for me that reference never seems to work. Would you know what I can do to make it work as intended? I don't seem to be able to figure that one out by myself. Thanks in advance.

Sushiman
  • 55
  • 6

1 Answers1

2

I don't have experience with add-ons for Thunderbird, but do with add-ons for Firefox. However, I believe the same mechanisms apply to Thunderbird.

You have two options (or perhaps more, that I am unaware of):

  1. Include the needed extra javascript file in the xul file, before the main javascript file (or did you try this already?):

    <script type="application/javascript" src="chrome://path/to/extra.js"/>
    <script type="application/javascript" src="chrome://path/to/main.js"/>
    
  2. Load the needed extra javascript file from inside the main javascript file, as a subscript, with the subscript loader service, through loadSubScript():

    var mozIJSSubScriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
                                          .getService(Components.interfaces.mozIJSSubScriptLoader);
    
    mozIJSSubScriptLoader.loadSubScript( 'chrome://path/to/extra.js', optionalScope, 'UTF-8' );
    

    The optionalScope lets you load the scripts variables into a designated object. If omitted, the scripts variables will be loaded into the current scope of the loadSubScript() caller.

    The charset argument is optional as well, by the way.

Codifier
  • 354
  • 1
  • 14
  • But that only works if you want to run a specific script as a whole, like some onLoad() function, or am I wrong there? You still can't easily type "var result = javascriptname.functionname(parameter); " or something like this in your main js-file, can you? If something like this was possible, I would not need to rewrite a rather large js-file, which deals with symmetric encryption. – Sushiman Apr 15 '15 at 15:00
  • @Sushiman Not without including the file that defines `javascriptname.functionname(parameter)`, no. But then, I'm not aware of *any* flavor of JavaScript that allows this, to be honest. You always need to include the file(s) that define the functions you want to access, if they're not already included and/or part of the environment you are running your script in. – Codifier Apr 15 '15 at 15:09
  • @Sushiman But perhaps I'm misinterpreting what you are trying to get at here. If you include the file with one of the options I've given above, and that file has `javascriptname.functionname(parameter)` defined in it's main scope, then you can access `javascriptname.functionname(parameter)` from the scope that you've loaded it in. – Codifier Apr 15 '15 at 15:12