我使用了这个稳定文档的示例
let attachments = await browser.messages.listAttachments(messageId);
for (let att of attachments) {
let file = await browser.messages.getAttachmentFile(
messageId,
att.partName
);
let content = await file.text();
}
这不起作用。
https://webextension-api.thunderbird.net/en/stable/messages.html
我的扩展的background.js中的其他源代码
async function mail_display(tab, message) {
console.log(`Message displayed in tab ${message.messageId}: ${message.subject}`);
console.log(message.id);
try {
let attachments = await browser.messages.listAttachments(message.id);
console.log(attachments);
for (let att of attachments) {
let file = await browser.messages.getAttachmentFile(messageId, att.partName);
console.log(file);
let content = await file.text();
}
} catch(error) {
console.log(`Fehler beim Abrufen der Attachments: ${error}`);
}
}
browser.messageDisplay.onMessageDisplayed.addListener(mail_display);
它返回一个没有附件的空数组。
好吧,我找到了一个有效的解决方案:
// background.js in Thunderbird extension
async function mail_display(tab, message) {
// Log message: "Message displayed in tab {message.id}: {message.subject}"
console.log(`Message displayed in tab ${message.id}: ${message.subject}`);
// Try to load the full message
try {
// Save the full message in the message_full variable
message_full = await browser.messages.getFull(message.id);
// Log the message (optional)
//console.log(message);
// Call the listAttachments() method to get the list of attachments
let attachments = await browser.messages.listAttachments(message.id);
// Log the attachments
console.log(attachments);
// Iterate over the attachments
for (let att of attachments) {
// Call the getAttachmentFile() method to get the file for the attachment
let file = await browser.messages.getAttachmentFile(message.id, att.partName);
// Call the text() method to get the attachment content as text
let content = await file.text();
// Log the attachment content
console.log(content);
}
} catch (error) {
// Log the error message
console.log(`Error with loading the Attachments: ${error}`);
}
}
browser.messageDisplay.onMessageDisplayed.addListener(mail_display);
注: