0

I'm trying to make sense of an API that listen if a "TAG" is clicked within a media player(called Wirewax) and return data on this element. My aim is to check if there is a certain string in the returned data and play an alert if that's the case.

1) I'm not familiar with the handling of the data parameter here but I managed to display the returned data on an html element:

  <ul id="console"><ul>

and javascript:

const consoleOnScreen = document.getElementById("console");
const printOnScreen = data => {
  const li = document.createElement("li");
  li.innerHTML = `${data.name}: ${
    data.data === undefined ? "no payload" : JSON.stringify(data.data)
  }`;
  consoleOnScreen.appendChild(li);
};

2) With the API reference for the eventListener:

`window.wirewax.addEventListener(
  window.wirewax.events.listeners.TAG_CLICK,
  function(data) {
    printOnScreen(data);
  }
);`

3) I tried a quick and dirty method to check if the html contained a certain string in the console element with


    if (document.getElementById("console").innerHTML.includes("words")) {
    alert('yep!');
}

but it doesn't find anything (probably because the content is not created yet).

Any ideas on how to manage that?

massakre
  • 1
  • 1
  • Best way to check for substring is `fooString.indexOf('string-to-search')` – Justinas Jun 01 '20 at 12:30
  • Why not check `JSON.stringify(data.data)` in the event listener? – Heretic Monkey Jun 01 '20 at 12:33
  • @Justinas Not any more. ES2015 brought [`String.prototype.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) which returns a Boolean instead of a number. – Heretic Monkey Jun 01 '20 at 12:37
  • @HereticMonkey, thank for your answers. Would you mind elaborating on how to check `JSON.stringify(data.data)` in the event listener? It seems I can't get this working. – massakre Jun 01 '20 at 15:03
  • Well, right now your event listener is `function(data) { printOnScreen(data); }`, right? So, just make it `function(data) { if (JSON.stringify(data.data).include("words")) { alert("yep!"); } printOnScreen(data); }` (maybe with some line breaks since I can't make those in comments). – Heretic Monkey Jun 01 '20 at 15:09
  • @HereticMonkey I did try similar solutions for 3 hours and just figured there was a typo in my code... Thank you so much! – massakre Jun 01 '20 at 22:26

0 Answers0