Issue with Sandbox in new Firefox release

There seems to be an issue with the executeScript_Sandbox in the new Firefox version released yesterday. Simple scripts (with no ES6 methods) that ran fine in the prior Firefox version, and run correctly in the current Chrome version, are now returning “undefined”.

Consider the following:

{
  "Name": "random 1a",
  "CreationDate": "2024-4-30",
  "Commands": [
    {
      "Command": "executeScript_Sandbox",
      "Target": "var string = '$8,495'; var stripped = string.replace(/[^0-9]/g, \"\"); return stripped;",
      "Value": "test",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${test}",
      "Value": "",
      "Description": ""
    }
  ]
}

In the current version of Chrome and Firefox version 6.2.8, this will return “8495” as expected. In the new Firefox version, Sandbox returns “undefined”. If I change it to just “executeScript” in the new Firefox version, it returns “8495”.

I confirmed the problem. We will fix this asap. Thanks for reporting it!

So after some more research it turns out that we cannot process REGEX in Firefox Manifest V3 with the JS-interpreter library that we are using. The reason is explained here:

Firefox MV3 doesn’t allow ‘unsafe-eval’ or blob: for script-src. But the JS-interpreter is using Worker that uses blob which is not permitted for Firefox MV3. In fact, for evaluating regex they use some kind of vm (browser/node-server) environment. We have also not been able to find another JS library that manages to work around this restriction (yet). If anyone has a suggestion, please let us know.

Technical details aside, this means that executeScript_Sandbox works fine in Firefox except that it can not process any regular expression. If you have regular expression in your Javascript, you will get the error E501.

This macro explains it:

{
  "Name": "executeScript_Sandbox_in_Firefox",
  "CreationDate": "2024-5-5",
  "Commands": [
    {
      "Command": "executeScript_Sandbox",
      "Target": "var stripped = 'hello'.replace('e', 'o') \nreturn stripped",
      "Value": "test",
      "Description": "This works in Firefox"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var stripped = 'hello'.replace(/e/g, 'o') \nreturn stripped",
      "Value": "test",
      "Description": "This fails in Firefox with error code E501"
    },
    {
      "Command": "echo",
      "Target": "${test}",
      "Value": "green",
      "Description": ""
    }
  ]
}

Now back to the original question: How to run your regex “replace” code in Firefox:

    {
      "Command": "executeScript_Sandbox",
      "Target": "var string = '$8,495'; var stripped = string.replace(/[^0-9]/g, \"\"); return stripped;",
      "Value": "test",
      "Description": ""
    },

=> There are two solutions:

(1) use executeScript instead of executeScript_Sandbox.

(2) Or replace whatever the regex does with “normal” Javascript code, then it works. Here is an example:

{
  "Name": "testregex",
  "CreationDate": "2024-5-3",
  "Commands": [
    {
      "Command": "executeScript",
      "Target": "var string = '$8,495'; var stripped = string.replace(/[^0-9]/g, \"\"); return stripped;",
      "Value": "test1",
      "Description": "works in Chrome/Edge and Firefox"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var string = '$8,495';\nvar stripped = '';\n\n// Loop through each character in the string\nfor (var i = 0; i < string.length; i++) {\n    var char = string[i];\n    // Check if the character is a digit\n    if (char >= '0' && char <= '9') {\n        stripped += char; // Add only digits to the stripped string\n    }\n}\n\nreturn stripped;\n",
      "Value": "test2",
      "Description": "regex replaced => works in sandbox in Firefox, too"
    },
    {
      "Command": "echo",
      "Target": "2) _Sandbox (Workaround for FX): ${test2} ",
      "Value": "green",
      "Description": ""
    }
  ]
}

Thank you for the suggestion. Looks like executeScript is the only immediate solution as I have well beyond a thousand regex expressions throughout my macros. While rewriting the scripts would certainly be good practice for my JS skills :grinning:, hopefully there will eventually be a JS interpreter that handles regex (and ES6+) in a way that is V3-compliant for all browsers. Thanks again for the help.

A hint for those that can not use executeScript: ChatGPT solved the “rewrite” problem for me. It translated the old executeScript_sandbox code with regex into perfect executeScript_sandbox code without regex.