我正在尝试编写一个Thunderbird扩展程序,它可以让你撰写邮件,但它会在发送之前处理邮件文本。所以我需要访问电子邮件正文的纯文本内容。
这是我到目前为止所做的,就像Extension Developer Javascript控制台中的一些测试代码一样。
var composer = document.getElementById('msgcomposeWindow');
var frame = composer.getElementsByAttribute('id', 'content-frame').item(0);
if(frame.editortype != 'textmail') {
print('Sorry, you are not composing in plain text.');
return;
}
var doc = frame.contentDocument.documentElement;
// XXX: This does not work because newlines are not in the string!
var text = doc.textContent;
print('Message content:');
print(text);
print('');
// Do a TreeWalker through the composition window DOM instead.
var body = doc.getElementsByTagName('body').item(0);
var acceptAllNodes = function(node) { return NodeFilter.FILTER_ACCEPT; };
var walker = document.createTreeWalker(body, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT, { acceptNode: acceptAllNodes }, false);
var lines = [];
var justDidNewline = false;
while(walker.nextNode()) {
if(walker.currentNode.nodeName == '#text') {
lines.push(walker.currentNode.nodeValue);
justDidNewline = false;
}
else if(walker.currentNode.nodeName == 'BR') {
if(justDidNewline)
// This indicates back-to-back newlines in the message text.
lines.push('');
justDidNewline = true;
}
}
for(a in lines) {
print(a + ': ' + lines[a]);
}
如果我能走上正轨,我将不胜感激。我也有一些具体的问题:
doc.textContent
真的没有换行符吗?这有多愚蠢?我希望它只是Javascript控制台的一个错误,但我怀疑不是。NodeFilter.SHOW_TEXT
,但它没有遍历回复中包含引用材料的<SPAN>
。同样地,FILTER_ACCEPT
每个节点看起来很有趣,然后手动挑选它,但我有同样的问题,如果我拒绝SPAN
节点,那么助行器就不会进入。<BR>
打破了天真的实现,因为它们之间没有#text
个节点。所以我手动检测它们并在我的阵列上推空线。是否真的有必要做那么多的手工工作来访问消息内容?答案 0 :(得分:5)
嗯,不是每个人都会立刻进入!
我将其发布为mozilla.dev.extensions thread,并进行了一些富有成果的讨论。我一直在Venkman玩,解决方法是抛弃我的DOM / DHTML习惯并写入正确的API。
var editor = window.gMsgCompose.editor;
// 'text/html' works here too
var text = editor.outputToString('text/plain', editor.eNone)
现在text
具有正在撰写的电子邮件正文的纯文本版本。