For each cycle to receive multiple messages per page

For the Chrome plugin, I need to receive messages. These messages are passed to me through the html variable.

In the example below, there are 2 messages, and both of them start with: <tr bgcolor="#FFFFFF">and end with:</tr>

I received the first data of the message, but now I need to make it retrieve all the data from each of these messages <tr bgcolor="#FFFFFF"> </tr>.

What is provided to me:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
  <link rel="STYLESHEET" type="text/css" href="xxx">
  <title>Untitled</title>
</head>

<body class="bodySinScrollHor">


  <table width="95%" align="center">
    <tr>
      <td class="etiquetaIzquierda" colspan=6>
        <a class="etiquetaIzquierda"><img border=0 height=15 src="xxx"> &nbsp;Comunicaciones (2)</a>
      </td>
      </td>
    </tr>
    <tr>
      <td colspan=6>
        <hr size=1 width="100%">
      </td>
    </tr>
    <tr id="comunicaciones">
      <td colspan=6>
        <table width="100%" border=0 bordercolor="#000000" cellspacing=0 cellpadding=0>

          <tr bgcolor="#FFFFFF">
            <td width="9%" class="valorCampoSinTamFijoPeque">2017-08-31T00:00:00</td>
            <td width="9%" class="valorCampoSinTamFijoPeque">13:22</td>
            <td width="4%" align=left class="valorcampoSinTamFijoPeque">
              <img src="xxx" title=" Out"> &nbsp; &nbsp;
            </td>
            <td width="11%" class="valorCampoSinTamFijoPeque" valign=top>
              <font class="valorcampoSinTamFijoPeque"><a title="clientname" class="valorcampoSinTamFijoPeque">ClientName</a></font>
            </td>
            <td width="14%" class="valorCampoSinTamFijoPeque" width="100%">Subject</td>
            <!-- 					<td width="61%"class="valorCampoSinTamFijoPeque">message text here</td> -->
          </tr>

          <tr bgcolor="#FFFFFF">
            <td width="9%" class="valorCampoSinTamFijoPeque">2017-08-31T00:00:00</td>
            <td width="9%" class="valorCampoSinTamFijoPeque">13:21</td>
            <td width="4%" align=left class="valorcampoSinTamFijoPeque">
              <img src="xxx" title=" Out"> &nbsp; &nbsp;
            </td>
            <td width="11%" class="valorCampoSinTamFijoPeque" valign=top>
              <font class="valorcampoSinTamFijoPeque"><a title="clientname" class="valorcampoSinTamFijoPeque">ClientName</a></font>
            </td>
            <td width="14%" class="valorCampoSinTamFijoPeque" width="100%">Subject</td>
            <!-- 					<td width="61%"class="valorCampoSinTamFijoPeque">Message Text Here</td> -->
          </tr>

      </td>
    </tr>
    </table>

    </td>
    </tr>
  </table>

</body>

</html>
Run codeHide result

Below is the code that I run to extract data from the first message. Please note that in this case there are 2 posts, but there are people who have 54 posts, so it needs a lot of loops. This piece of code:

var count = (html1.match(/<tr bgcolor="#FFFFFF">/g) || []).length;
console.log(count);

This is what this number provides me.

matches[0].forEach(function(match, index) {
  var cleintcode = /<div\s*class="t_seg_codCliente">(.*?)<\/div>/.exec(match)[1];
  var cleintname = /<div\s*class="t_seg_nomCliente">(.*?)<\/div>/.exec(match)[1];
  var taxId = /<div\s*class="t_seg_nifCliente">(.*?)<\/div>/.exec(match)[1];
  var date = /<div\s*class="t_seg_fechaPresCliente">(.*?)<\/div>/.exec(match)[1];
  var state = /<div\s*class="t_seg_estadoCliente">(.*?)<\/div>/.exec(match)[1];
  var expirydate = /<div\s*class="t_seg_fechaCadCliente">(.*?)<\/div>/.exec(match)[1];
  var communications = /<div\s*class="t_seg_comCliente"><a .*;">(.*?)<\/a>/.exec(match)[1];
  var comclient = /<div\s*class="t_seg_comCliente"><a href="javaScript:popupComs\('(.*?)'/.exec(match)[1];
  var messages = "xxx" + comclient;

  var html1 = httpGet(messages);

  //console.log(html1);

  const cleanupDocString = html1.replace(/(?:<!--|-->)/gm, '');

  parser = new DOMParser();

  htmlDoc = parser.parseFromString(cleanupDocString, "text/html");

  //console.log(htmlDoc);

  var communicationsvalue = htmlDoc.getElementsByClassName("valorCampoSinTamFijoPeque")[0].textContent;

  if (communicationsvalue.indexOf('No existen comunicaciones asociadas a este cliente.') !== -1) {
    console.log("This chat does not contain any communiction!");
  } else {

    //Get count of regex matches. (amount of messages)
    var count = (html1.match(/<tr bgcolor="#FFFFFF">/g) || []).length;
    console.log(count);

    var comDate = htmlDoc.getElementsByClassName("valorCampoSinTamFijoPeque")[0].textContent;
    var comTime = htmlDoc.getElementsByClassName("valorCampoSinTamFijoPeque")[1].textContent;
    var comType = htmlDoc.getElementsByTagName('img')[1].src;
    var comClient = htmlDoc.getElementsByTagName('a')[1].textContent;
    var comSubject = htmlDoc.getElementsByClassName("valorCampoSinTamFijoPeque")[6].textContent;

    const element = htmlDoc.getElementsByClassName('valorCampoSinTamFijoPeque')[7];

    var pulledMessage = element.innerHTML;

    var messageData = [{
        clientCode: cleintcode,
        clientName: cleintname,
        taxID: taxId,
        cleintDate: date,
        cleintState: state,
        cleintExpirydate: expirydate
      },
      {
        mesDate: comDate,
        mesTime: comTime,
        mesType: comType,
        mesClient: comClient,
        mesSubject: comSubject,
        mesText: pulledMessage
      }
    ];
    console.log(messageData);
  }
});
Run codeHide result

The code above gives me this console log:

10:
 1. cleintDate: "31/08/17"
 2. cleintExpirydate:"29/11/17"
 3. cleintState:"Subject"
 4. clientCode:"xxxxxx"
 5. clientName:"clientName"
 6. taxID:""
 7. __proto__:Object
2.  1:
 1. mesClient:"ClientName"
 2. mesDate:"2017-08-31T00:00:00"
 3. mesSubject:"Subject "
 4. mesText:"Message text Here"
 5. mesTime:"13:22"
 6. mesType:"link"
 7. __proto__:Object

, 0 , . . , 1, .

:

1.  0:
 1. cleintDate:"31/08/17"
 2. cleintExpirydate:"29/11/17"
 3. cleintState:"Subject"
 4. clientCode:"xxxxxx"
 5. clientName:"clientName"
 6. taxID:""
 7. __proto__:Object
2.  1:
 1. mesClient:"ClientName"
 2. mesDate:"2017-08-31T00:00:00"
 3. mesSubject:"Subject "
 4. mesText:"Message text Here"
 5. mesTime:"13:22"
 6. mesType:"link"
 7. __proto__:Object
3. 3:
 1. mesClient:"ClientName"
 2. mesDate:"2017-08-31T00:00:00"
 3. mesSubject:"Subject "
 4. mesText:"Message text Here"
 5. mesTime:"13:22"
 6. mesType:"link"
 7. __proto__:Object

, for, , , <tr bgcolor="#FFFFFF"> </tr> .

, .

+4
2

:

var data = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link rel="STYLESHEET" type="text/css" href="xxx"> <title>Untitled</title> </head> <body class="bodySinScrollHor"> <table width="95%" align="center"> <tr> <td class="etiquetaIzquierda" colspan=6> <a class="etiquetaIzquierda"><img border=0 height=15 src="xxx"> &nbsp;Comunicaciones (2)</a> </td> </td> </tr> <tr> <td colspan=6> <hr size=1 width="100%"> </td> </tr> <tr id="comunicaciones"> <td colspan=6> <table width="100%" border=0 bordercolor="#000000" cellspacing=0 cellpadding=0> <tr bgcolor="#FFFFFF"> <td width="9%" class="valorCampoSinTamFijoPeque">2017-08-31T00:00:00</td> <td width="9%" class="valorCampoSinTamFijoPeque">13:22</td> <td width="4%" align=left class="valorcampoSinTamFijoPeque"> <img src="xxx" title=" Out"> &nbsp; &nbsp; </td> <td width="11%" class="valorCampoSinTamFijoPeque" valign=top> <font class="valorcampoSinTamFijoPeque"><a title="clientname" class="valorcampoSinTamFijoPeque">ClientName</a></font> </td> <td width="14%" class="valorCampoSinTamFijoPeque" width="100%">Subject</td> <!-- <td width="61%"class="valorCampoSinTamFijoPeque">message text here</td> --> </tr> <tr bgcolor="#FFFFFF"> <td width="9%" class="valorCampoSinTamFijoPeque">2017-08-31T00:00:00</td> <td width="9%" class="valorCampoSinTamFijoPeque">13:21</td> <td width="4%" align=left class="valorcampoSinTamFijoPeque"> <img src="xxx" title=" Out"> &nbsp; &nbsp; </td> <td width="11%" class="valorCampoSinTamFijoPeque" valign=top> <font class="valorcampoSinTamFijoPeque"><a title="clientname" class="valorcampoSinTamFijoPeque">ClientName</a></font> </td> <td width="14%" class="valorCampoSinTamFijoPeque" width="100%">Subject</td> <!-- <td width="61%"class="valorCampoSinTamFijoPeque">Message Text Here</td> --> </tr> </td> </tr> </table> </td> </tr> </table> </body> </html>';

var adiv = document.createElement("div"),
    msgs = [],
    trs;

adiv.innerHTML = data;
trs = adiv.querySelectorAll('tr[bgcolor="#FFFFFF"]');
trs.forEach(function(tr){
              var d = [];
              tr.querySelectorAll("td")
                .forEach(function(td){
                           var img = td.querySelector("img"),
                               src = img && img.attributes.getNamedItem("src").value;
                           d.push(src || td.textContent);
                         });
              msgs.push(d);
            });
console.log(msgs);
Hide result
+1

?

var trs = $('tr[bgcolor="#FFFFFF"]');
var output = [];
for(var i=0;i<trs.length;i++){
var currentOutput = {};
currentOutput.cleintDate=trs.eq(i).find("td").eq(0).text();
//other properties like this
currentOutput.cleintExpirydate=trs.eq(i).find("td").eq(1).text();
//add all your required properties
output.push(currentOutput);
}
console.log(output)

jquery

var trs = document.querySelectorAll('tr[bgcolor="#FFFFFF"]');
    var output = [];
    for(var i=0;i<trs.length;i++){
    var currentOutput = {};
    currentOutput.cleintDate=trs[i].getElementsByTagName('td')[0].innerText;
    //other properties like this
    currentOutput.cleintExpirydate=trs[i].getElementsByTagName('td')[0].innerText;
    //add all your required properties
    output.push(currentOutput);
    }
    console.log(output)
0

Source: https://habr.com/ru/post/1685135/


All Articles