RealRaven2000 / SmartTemplates

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

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

Open RealRaven2000 opened 3 hours ago

RealRaven2000 commented 3 hours 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 2 hours 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 hour ago

Preview version:

smartTemplate-fx-4.9pre82.zip


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)