带有Tinymce的电子邮件编辑器:如何导出干净的HTML文件?
我设法创建了一个电子邮件编辑器,该电子邮件编辑器以此示例。在文件的末尾,我添加了下载按钮,以便用户可以检索他编辑的文件。
我的问题是,Tinymce注入了我想在导出期间要删除的大量代码,标签,类,属性和ID。是否有一个函数或插件可以在没有任何引用Tinymce的情况下检索其文件?
目前,我“手动”删除每个元素,这在我看来根本没有最佳状态。元素太多(到处都是属性),我敢肯定有一种更简单的方法。
document.getElementById('btnHtml').addEventListener('click', function() {
let $email = $('.email-container');
let contentToDelete = document.querySelectorAll("script,div.mce-tinymce,#mceDefaultStyles,.mce-widget,#u0,#u1,button");//
contentToDelete.forEach((element) => element.remove());//remove all elements and children that are outside tinymce editors
// Get content from all editors
for (var i = 0; i < tinymce.editors.length; i++) {
let editable = $email.find('.content')[i];
editable.innerHTML = tinymce.editors[i].getContent();
editable.removeAttribute('spellcheck');
// If you remove "contenteditable" then this node will not open TinyMCE when you click on it.
editable.removeAttribute('data-mce-bogus');
editable.removeAttribute('data-mce-style');
editable.removeAttribute('[data-mce-href');
editable.classList.remove('mce-content-body');
editable.classList.remove('mce-item-table');
}
var txtboxes = document.querySelectorAll('.content');
txtboxes.forEach(box => {
box.replaceWith(...box.childNodes);//remove only div.content itself not the children
});
let full = new XMLSerializer().serializeToString(document.doctype);//serialize all the document, get the doctype
let innercontent = document.documentElement.outerHTML;
let content = full + innercontent; // append doctype to html content
let blob = new Blob([content], {
type: 'text/html'
});
// Create download link and then download.
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.download = "index.html";
a.style.display = 'none';
a.href = url;
//document.body.appendChild(a);
// this link will not work here so try it on "codepen.io" or on your computer
a.click();
// Release object URL
window.URL.revokeObjectURL(url);
});
//at the end of my html file
<button type="button" id="btnHtml" type="button">Download html file</button>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,该函数是
getContent
。我可以使用jQuery 3.6.0和Tinymce 5.6.0显示一个示例:请注意,我仅删除Tinymce在我的示例中为我生成的容器的属性(我的意思是
.email-edeedible
元素),因此您也可以删除其他属性。我不使用tinymce.editors [i] .save();
,因为它添加了Tinymce的元数据。该元数据对于将来编辑文本很有用。例如,您可以将文本存储在数据库中,然后将其检索以进行进一步编辑。另请注意,我使用
url.revokeobjecturl
。来自 https:// https://deeverveperer.mozilla.org/en-us us.org/en-us us /docs/web/api/url/revokeObjecturl :您做对了。另一种方法是将jQuery对象扩展到添加功能。在网页上您有一个示例来添加一个函数,以删除删除所有属性节点。也许您可以编辑该功能以添加白名单(字符串数组)作为输入参数。
示例代码(信用geeksforgeeks.org)是:
更新
为每个编辑器分配一个唯一的ID。 Tinymce API具有函数
tinymce.get(id)
(请参阅参考)返回特定的编辑器,所以我的新示例是...Yes, the function is
getContent
. I can show you an example using jQuery 3.6.0 and TinyMCE 5.6.0:Note that I only remove the attributes of containers (i mean
.email-editable
elements) that TinyMCE has generated for me in my example, so you can remove other attributes too. I don't usetinymce.editors[i].save();
because it adds the metadata of Tinymce. This metadata is useful for editing the text in the future. For instance, you can store the text in a database and then retrieve it for further editing.Also note that I use
URL.revokeObjectURL
. From https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL:You are doing right. Another way is by extending the jQuery object to add a function. On the web page https://www.geeksforgeeks.org/how-to-remove-all-attributes-of-an-html-element-using-jquery/ you have an example to add a function that removes all the attributes of a node. Perhaps you can edit that function to add a whitelist (an array of strings) as an input parameter.
The sample code (credits to GeeksForGeeks.org) is:
UPDATE
Assign each editor a unique id. The TinyMCE API has the function
tinymce.get(id)
(see reference) that returns a specific editor, so my new example is...