From 641c880d11c7a47ebc946e141026eeeecd7a73ab Mon Sep 17 00:00:00 2001 From: shuaikangzhou <863909694@qq.com> Date: Tue, 28 Nov 2023 21:30:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dstr=5Fcontent=E8=BD=AC?= =?UTF-8?q?=E4=B9=89=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/DataBase/output_pc.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/app/DataBase/output_pc.py b/app/DataBase/output_pc.py index 30c1520..7bf36e0 100644 --- a/app/DataBase/output_pc.py +++ b/app/DataBase/output_pc.py @@ -1,4 +1,5 @@ import csv +import html import os from PyQt5.QtCore import pyqtSignal, QThread @@ -10,6 +11,24 @@ if not os.path.exists('./data/聊天记录'): os.mkdir('./data/聊天记录') +def escape_js_and_html(input_str): + # 转义HTML特殊字符 + html_escaped = html.escape(input_str, quote=False) + + # 手动处理JavaScript转义字符 + js_escaped = ( + html_escaped + .replace("\\", "\\\\") + .replace("'", r"\'") + .replace('"', r'\"') + .replace("\n", r'\n') + .replace("\r", r'\r') + .replace("\t", r'\t') + ) + + return js_escaped + + class Output(QThread): """ 发送信息线程 @@ -638,9 +657,7 @@ const chatMessages = [ avatar = 'myhead.png' if is_send else 'tahead.png' timestamp = message[5] self.progressSignal.emit(index) - str_content = str_content.replace('"', '\\"').replace('{', '\\{').replace('}', '\\}').replace('\n', - '\\n').replace( - "'", "\\'") + str_content = escape_js_and_html(str_content) if type_ == 1: if self.is_5_min(timestamp): f.write(