RealRaven2000 / SmartTemplates

Thunderbird Add-on: SmartTemplates
http://smarttemplates.quickfolders.org/
Other
25 stars 15 forks source link

Sandbox script: Support reading async variables without parameters (e.g. `%from%`) within script #329

Closed RealRaven2000 closed 2 weeks ago

RealRaven2000 commented 1 month ago

In the following example we try to read the %identity% variable and do something within the script:

%{%
  (async () => {
  let val = await identity;
  switch(val) {
    case 'email1@acme.com':
      return "<p>The parameter email1@acme.com was read from Clipboard.</p>";
    default:
      return `<p>Other value retrieved from Clipboard: ${val}</p>`;
  }
})()
%}%

Expected behavior: identity should retrieve the same string that %identity% would. The problem here is that the function isn't fully executed. Instead it returns an empty function or throws "ERR: TypeError: can't convert val to string ". The workaround would be to add parentheses, like this:

(async () => {
  let val = await identity();
})()

But this returns the string %identity% - because by design an empty parameter list was not allowed.

As an acceptable workaround I tried supplying an empty string as argument, like this:

(async () => {
  let val = await identity("");
})()

Interestingly, this returns an empty string at the moment. Let's implement this to work so that it returns the variable value like this:

await from("")   =>  %from%
await identity("")   =>  %identity%
await clipboard("")   =>  %clipboard%
RealRaven2000 commented 1 month ago

While testing, I also encountered an interesting behavior of the %clipboard()% function: It will preferably return html as format, so if we copy an email address from composer in the editing area, it will return a string like:

<html><body>
<!--StartFragment-->thunderbirddaily@gmail.com<!--EndFragment-->
</body>
</html>

so we will need a way of dealing with this, maybe by forcing plain text through a parameter. See new issue #330

RealRaven2000 commented 1 month ago

Preview version:

smartTemplate-fx-4.9pre82.zip

this one supports using empty parentheses to return data without passing parameters, in order to get the result string %from% like this:

var t = await from();

To install version above download zip file and drag the file into Thunderbird Add-ons Manager (do not extract contents, it won't install like that)

RealRaven2000 commented 2 weeks ago

Implemented in 4.9 - published 11/11/2024