gpt4 book ai didi

javascript - php & JS 聊天,在聊天窗口中执行 html

转载 作者:行者123 更新时间:2023-11-30 00:46:31 24 4
gpt4 key购买 nike

我尝试在聊天窗口中获取 html 输出,但它显示 html 代码

这是聊天系统涉及的2个文件

聊天.js

 `function chatHeartbeat(){

var itemsfound = 0;

if (windowFocus == false) {

var blinkNumber = 0;
var titleChanged = 0;
for (x in newMessagesWin) {
if (newMessagesWin[x] == true) {
++blinkNumber;
if (blinkNumber >= blinkOrder) {
document.title = x+' says...';
titleChanged = 1;
break;
}
}
}

if (titleChanged == 0) {
document.title = originalTitle;
blinkOrder = 0;
} else {
++blinkOrder;
}

} else {
for (x in newMessagesWin) {
newMessagesWin[x] = false;
}
}

for (x in newMessages) {
if (newMessages[x] == true) {
if (chatboxFocus[x] == false) {
//FIXME: add toggle all or none policy, otherwise it looks funny
$('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
}
}
}

$.ajax({
url: "chat.php?action=chatheartbeat",
cache: false,
dataType: "json",
success: function(data) {

$.each(data.items, function(i,item){
if (item) { // fix strange ie bug

chatboxtitle = item.f;

if ($("#chatbox_"+chatboxtitle).length <= 0) {
createChatBox(chatboxtitle);
}
if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
$("#chatbox_"+chatboxtitle).css('display','block');
restructureChatBoxes();
}

if (item.s == 1) {
item.f = username;
}

if (item.s == 2) {
$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
} else {
newMessages[chatboxtitle] = true;
newMessagesWin[chatboxtitle] = true;
$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
}

$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
itemsfound += 1;
}
});

chatHeartbeatCount++;

if (itemsfound > 0) {
chatHeartbeatTime = minChatHeartbeat;
chatHeartbeatCount = 1;
} else if (chatHeartbeatCount >= 10) {
chatHeartbeatTime *= 2;
chatHeartbeatCount = 1;
if (chatHeartbeatTime > maxChatHeartbeat) {
chatHeartbeatTime = maxChatHeartbeat;
}
}

setTimeout('chatHeartbeat();',chatHeartbeatTime);
}});
}`

这里 item.m 包含聊天消息,我尝试通过添加 .html() 来获取 html 输出,但它不起作用

这里是 php 文件中的相同函数

     function chatHeartbeat() {

$sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['username'])."' AND recd = 0) order by id ASC";
$query = mysql_query($sql);
$items = '';

$chatBoxes = array();

while ($chat = mysql_fetch_array($query)) {

if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
$items = $_SESSION['chatHistory'][$chat['from']];
}

$chat['message'] = sanitize($chat['message']);

$items .= <<<EOD
{
"s": "0",
"f": "{$chat['from']}",
"m": "{$chat['message']}"
},
EOD;

if (!isset($_SESSION['chatHistory'][$chat['from']])) {
$_SESSION['chatHistory'][$chat['from']] = '';
}

$_SESSION['chatHistory'][$chat['from']] .= <<<EOD
{
"s": "0",
"f": "{$chat['from']}",
"m": "{$chat['message']}"
},
EOD;

unset($_SESSION['tsChatBoxes'][$chat['from']]);
$_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
}

if (!empty($_SESSION['openChatBoxes'])) {
foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
$now = time()-strtotime($time);
$time = date('g:iA M dS', strtotime($time));

$message = "Sent at $time";
if ($now > 180) {
$items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

if (!isset($_SESSION['chatHistory'][$chatbox])) {
$_SESSION['chatHistory'][$chatbox] = '';
}

$_SESSION['chatHistory'][$chatbox] .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
$_SESSION['tsChatBoxes'][$chatbox] = 1;
}
}
}
}

$sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['username'])."' and recd = 0";
$query = mysql_query($sql);

if ($items != '') {
$items = substr($items, 0, -1);
}
header('Content-type: application/json');
?>
{
"items": [
<?php echo $items;?>
]
}

<?php
exit(0);
}

请向我提出任何在聊天窗口中获取 html 输出以及用户可以单击聊天窗口中的链接的想法

谢谢

最佳答案

可能是因为您清理了消息,这会将 HTML 转换为字符。

$chat['message'] = sanitize($chat['message']);

关于javascript - php & JS 聊天,在聊天窗口中执行 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21316980/

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