WebExtension script support - jobisoft/quicktext GitHub Wiki
Internal Scripts
Quicktext scripts can be created internally in the Quicktext settings window in the scripts tab:
Once you have created an internal script with the name "TestScript" you can call it in one of your templates:
[SCRIPT=TestScript](/jobisoft/quicktext/wiki/SCRIPT=TestScript)
Quicktext scripts are written using the Javascript language, and whatever you return is going to be inserted instead of the SCRIPT tag. A simple script example:
var date = new Date();
if (date.getHours() < 12)
return "Good morning";
else
return "Good afternoon";
External Scripts
Internal scripts are currently still supported in Manifest V2, but not in the new standard Manifest V3. Quicktext will support Manifest V2 (and internal scripts) as long as possible. The alternative approach, which is compatible with Manifest V3, is to use an external script add-on, which hosts and executes the scripts. You might want to try out the community script add-on, which includes popular scripts maintained by the community.
More information about external scripts and how to create your own script add-on can be found in the GitHub repository of the community script add-on.
Passing variables to scripts
If you want to use variables, you can call your template with:
[SCRIPT=TestScript](/jobisoft/quicktext/wiki/myvar)
To get the myvar
-value in the script, access the variables
array:
var variable = this.quicktext.variables[0];
return variable;
You can use as many variables as you want. To go through all variables you can do this:
let vars = [];
for (let variable of this.quicktext.variables) {
vars.push(variable);
}
return vars.length;
Example script using variables
Since you can use tags recursively, the above example script can be extended to:
var date = new Date();
if (date.getHours() < 12)
return "Good morning " + this.quicktext.variables[0];
else
return "Good afternoon " + this.quicktext.variables[0];
Call it in your template like so:
[SCRIPT=TestScript](/jobisoft/quicktext/wiki/[[TO=firstname)]]
Using WebExtension APIs
External scripts can use all documented methods of Thunderbird's WebExtension APIs via the browser.*
namespace. Internal scripts can only use the APIs and methods exposed through the this
object:
Note: All methods of the compose API, which are exposed via this.compose.*
are automatically acting on the currently active compose window, and you must not specify the first tabId
parameter.
Advanced usage
You can also access all the values that you would get by using the tags. There are two types of functions that you can use:
await this.quicktext.getTag(tagname, parameters);
and
await this.quicktext.processTag(tagname, parameters);
with the tag name in lowercase. The get-function is what Quicktext uses to process the tags, so the function needs the same variables as the tag does. It takes an array of variables. For a tag like [TO=firstname](/jobisoft/quicktext/wiki/TO=firstname)
you would get the same value in a script by calling:
let firstname = await this.qicktext.getTag("to", ["firstname"]);
The process-function will provide access to the entire dataset. You can dump it into the console by calling:
console.log(JSON.stringify(await this.quicktext.processTag("to")));
Most process-functions don't need any variables but some do. You will have to try (we do not know).
Executing other scripts
It is also possible to execute another script by calling:
var output = await this.quicktext.getTag("script",["GoodMornig", "John"])
GoodMorning
is the script name, John
is the first parameter.