Error in condition of if_v2: undefined is not a function

Sometimes when i execute a macro i have the following error

“Error in condition of if_v2: undefined is not a function” its random…

ex:
Line 181: Error in condition of if_v2: undefined is not a function
[info]
Executing: | if_v2 | ${titleFirstPart[${randomTitleFirstPart}]}.includes(“test”) | |

all variable are defined

1 Like

Do you have a test macro for us?

Sometimes when i execute a macro i have the following error

PS: If the error happens only sometimes, then it is not related to the V7.0.7 executescript_sandbox changes!

But please note: .includes is ES6 level Javascript. It can not be used in executeScript_Sandbox and in IF_v2/While/RepeatIF. The ES5 alternative to ES6 .includes is to use .indexOf.

So there are two solutions:

  • Move your code into an executeScript statement and do the check there. Then your code will work without changes.

  • OR: Replace .includes with .indexOf

Both options are shown in this example:

{
  "Name": "indexOf",
  "CreationDate": "2022-4-22",
  "Commands": [
    {
      "Command": "executeScript",
      "Target": "const a1 = [\"hello\",\"guten tag\",\"bonjour\"]; return a1;",
      "Value": "mya1",
      "Description": ""
    },
    {
      "Command": "comment",
      "Target": "To .includes, which is ES6 => we must use executeScript!",
      "Value": "mya1",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "var b = false; if (${mya1}.includes(\"hello\") ) b = true; return b;",
      "Value": "myb",
      "Description": ""
    },
    {
      "Command": "if_v2",
      "Target": "${myb}",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "Test1: yes ",
      "Value": "green",
      "Description": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "test1: no",
      "Value": "pink",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "comment",
      "Target": "Or use use .indexOF - this can be done directly inside IF ",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "if_v2",
      "Target": "${mya1}.indexOf(\"hello\") > -1 ",
      "Value": "IF uses only ES5 Javascript, so we use indexOF instead of include",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "test2: yes ",
      "Value": "yellow",
      "Description": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "test2: no",
      "Value": "brown",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}

PS: See How to replace includes with indexOf and How to replace startswith with indexOf

Why did an upgrade to v.7 mean a downgrade in JS capability/version (ES6 → ES5)?

This really *(#$()@s with my scripts.

The reason for this change is that the new Chrome manifest V3 blocks Javascript “eval” (the heart of executeScript_Sandbox) inside any Chrome extension.

So we only had the option to either remove executeScript_Sandbox totally or move to this new (less powerful) JS interpreter which uses ES5.

executeScript still works fine and unchanged because it runs inside the webpage, not the Chrome extension.