Conditional scripting with browsing searches

I would like to map my F17 key to do the following: open a librewolf window if nothing is selected (meaning that the home tab of librewolf is opened (open -b librewolf)) or if I have something selected, look that selection using librewolf and brave search engine (which I know how to do with the open URL with selection action). I tried a couple of things by using the run Real JavaScript with following JavaScript code but this always seems to give the LibreWolf home page, even when a selection is made, any help?

JavaScript code:

(async () => {
  let selection = await get_string_variable({ variable_name: "selected_text" });

  if (selection) {
    let shellScript = '/opt/homebrew/bin/librewolf --search',
      selection;

    let shellScriptWrapper = {
      script: shellScript,
    };

    let result = await runShellScript(shellScriptWrapper);
  } else {
    let shellScript = '/usr/bin/open -b org.mozilla.librewolf',
      selection;

    let shellScriptWrapper = {
      script: shellScript,
    };

    // this will execute the Apple Script and store the result in the result variable.
    let result = await runShellScript(shellScriptWrapper);
  }

  returnToBTT(result);
})();

Flow Chart:

if (selection is empty)
open -a Librewolf;
else
lookup selection

I haven't tried it, but this looks wrong (in both parts where it is used), what are you trying to do there?

 let shellScript = '/opt/homebrew/bin/librewolf --search',
      selection;

I think (if I understand correctly) you would want something like this:

let shellScript = `/opt/homebrew/bin/librewolf --search "${selection}"`

Hi, thanks for your fast response, I'm not so fast ;). Applying your suggestion to the code made it work just as I wanted. Thanks a lot and keep up the great work man, this app is really impressive!

1 Like