【发布时间】:2021-02-09 23:50:55
【问题描述】:
我正在使用本地消息传递 (following the ping pong example in python) 在 Thunderbird 中编写一个插件,以调用 Delphi 程序将电子邮件复制为本地“.eml”文件。我面临的问题似乎是编码。此外,生成的文件在文件的开头和结尾包含双引号 ("") 以及转义的双引号 (\")。我只想有一个 1 对 1 的副本,而不是更改其内容。
邮件内容示例:
"test"
€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ
éöàäèüâêû
但是,在文件中,它看起来更像这样:
\"test\"
€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“â€â€¢â€“—˜™š›œžŸ
éöà äèüâêû
我可能已经找到了问题所在,这里对此进行了解释:
https://www.i18nqa.com/debug/utf8-debug.html
但是,我真的不知道如何调整我的代码来解决这个问题。
感谢您的帮助!
这是我的 background.js:
async function main() {
messenger.menus.create({
contexts : ["message_list"],
id: "copy@mail.lu",
onclick : passMsg,
title: messenger.i18n.getMessage("lang.menuTitle")
});
}
async function passMsg(OnClickData) {
if (OnClickData.selectedMessages && OnClickData.selectedMessages.messages.length > 0) {
let MessageHeader = OnClickData.selectedMessages.messages[0];
let raw = await messenger.messages.getRaw(MessageHeader.id);
let port=browser.runtime.connectNative("copymail");
port.onMessage.addListener((message) => {
port.disconnect();
});
port.postMessage(raw);
} else {
console.log("No message selected");
}
}
main();
这是我的 Delphi 代码:
procedure WriteSTDInputToFile(const Filename: String);
var
Buffer: array [0 .. 3] of Byte;
msgLen: LongInt;
msg: UTF8String;
myFile: TextFile;
StdIn: THandleStream;
jsonValue: TJSONValue;
begin
StdIn := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));
try
msgLen := 0;
if StdIn.Read(Buffer, SizeOf(msgLen)) > 0 then
msgLen := PLongInt(@Buffer)^;
if msgLen > 0 then
begin
SetLength(msg, msgLen);
StdIn.Read(PUTF8Char(msg)^, msgLen);
if msg <> '' then
begin
AssignFile(myFile, Filename, CP_UTF8);
ReWrite(myFile);
jsonValue := TJSONObject.ParseJSONValue(msg);
try
write(myFile, UTF8Encode(jsonValue.ToString));
finally
jsonValue.Free;
end;
CloseFile(myFile);
end;
end;
finally
if Assigned(StdIn) then
StdIn.Free;
end;
end;
生成的文件内容:
"X-MDAV-Result: clean
X-MDAV-Processed: mail.test.lu, Wed, 28 Oct 2020 08:13:22 +0100
X-Spam-Processed: mail.test.lu, Wed, 28 Oct 2020 08:13:22 +0100
Return-path: <copy@mail.lu>
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on MAIL01E
X-Spam-Level:
X-Spam-Status: No, score=0.7 required=10.0 tests=HTML_MESSAGE,MPART_ALT_DIFF
shortcircuit=no autolearn=disabled version=3.4.2
Authentication-Results: test.lu;
auth=pass (plain) smtp.auth=ascholtes@test.lu
Received: from [172.16.17.35] [(172.16.17.35)] by test.lu (172.31.3.6) with ESMTPSA id md50033234892.msg;
Wed, 28 Oct 2020 08:13:21 +0100
X-MDRemoteIP: 172.16.17.35
X-MDArrival-Date: Wed, 28 Oct 2020 08:13:21 +0100
X-Authenticated-Sender: ascholtes@test.lu
X-Rcpt-To: copy@mail.lu
X-MDRcpt-To: copy@mail.lu
X-Return-Path: copy@mail.lu
X-Envelope-From: copy@mail.lu
X-MDaemon-Deliver-To: ascholtes@test.lu
To: Ayuth Scholtes <copy@mail.lu>
From: Ayuth Scholtes <copy@mail.lu>
Subject: Test
Organization: CISS
Message-ID: <7eb36f7c-a7af-c272-c189-eded642c3e1c@test.lu>
Date: Wed, 28 Oct 2020 08:13:21 +0100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary=\"------------6068A746223BB2C9F1771938\"
Content-Language: lb-LU
This is a multi-part message in MIME format.
--------------6068A746223BB2C9F1771938
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
|\"test\" â¬âÆââ¦â â¡Ëâ°Å â¹ÅŽâââââ¢ââËâ¢Å¡âºÅžŸ éöà äèüâêû|
--------------6068A746223BB2C9F1771938
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">
</head>
<body>
<pre class=\"lang-pascal s-code-block hljs delphi\"><code>\"test\"
â¬âÆââ¦â â¡Ëâ°Å â¹ÅŽâââââ¢ââËâ¢Å¡âºÅžŸ
éöà äèüâêû</code></pre>
</body>
</html>
--------------6068A746223BB2C9F1771938--
"
【问题讨论】:
-
欢迎来到 StackOverflow!我不明白,在您的问题中,.eml 文件和 JSON 之间的关系是什么。您能否编辑您的消息以显示您从 StdIn 读取的测试消息数据是什么?
-
您希望通过 STD_INPUT_HANDLE 收到什么?您是否尝试将其保存到文件并检查了内容?也许 Json 消息不正确...
标签: json delphi encoding thunderbird chrome-native-messaging