gpt4 book ai didi

javascript - 对于每个循环检索页面中的多条消息

转载 作者:搜寻专家 更新时间:2023-10-31 08:51:07 24 4
gpt4 key购买 nike

对于 Chrome 插件,我需要检索消息。这些消息是通过 html 变量提供给我的。

下面提供的示例中有 2 条消息,它们都以:<tr bgcolor="#FFFFFF"> 开头并以:</tr> 结尾

我检索了第一条消息数据,但现在我需要让它从每个 <tr bgcolor="#FFFFFF"> </tr> 中检索所有数据消息。

我得到的是:

<!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>

下面是我运行的代码,用于从第一条消息中检索数据。请注意,在这种情况下有 2 条消息,但有些人有 54 条消息,因此需要循环多次。这部分代码:

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

是什么为我提供了这个数字。

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);
}
});

上面的代码给我这个控制台日志:

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__:Object2.  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__:Object2.  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__:Object3. 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>在第一个之后。

内容略有编辑以隐藏个人信息。

最佳答案

你可能会这样做;

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);

关于javascript - 对于每个循环检索页面中的多条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46072978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com