0

Following the answer here, I'm trying to check with if there are 2 strings on a page then perform the function.

The below works but once I change it to && which is the operator for AND it doesn't work? - Am I missing something? (This will be my first Greasemonkey script)

Here is my current code for the OR operator which works - I simply want it to be AND (so if both texts exists then perform function)

if  (/(text1) || (text2)/i.test (document.body.innerHTML))
 {
var input=document.createElement("input");
input.type="button";
input.value="Intuit SLI";
input.onclick = redirect;
input.setAttribute("style", "font-size:18px;position:absolute;top:120px;right:40px;");
document.body.appendChild(input);

function redirect()
{
  var URLa = 'https://someurlhere';
  var URL_FINALa = encodeURI(URLa);

  window.open(URL_FINALa, '_blank');
}
 }
Toblerone
  • 77
  • 7
  • do you know what `/(text1) || (text2)/` actually means in RegExp? it's not actually doing what you think. That will match any content of document.body.innerHTML. Anyway, why not just `/text1/.test(document.body.inerrHTML) && /text2/.test(document.body.inerrHTML)` – Jaromanda X Aug 11 '17 at 10:12
  • To explain ... `(test1) || (test2)` match "test 1" or "" or " test2" because that's what the `|` character means in RegExp ... or ... using two `||` means one of the "or" will be an empty string ... and of course, every string matches an empty string – Jaromanda X Aug 11 '17 at 10:15
  • Thanks for the explanation @JaromandaX - your suggestion worked. – Toblerone Aug 11 '17 at 10:22

1 Answers1

1

answered with @JaromandaX 's suggestion.

if (/text1/.test(document.body.innerHTML) && /text2/.test(document.body.innerHTML))
Toblerone
  • 77
  • 7