微信图文带货,复制AI生成的文本到微信编辑器后格式乱了,图文的编辑器使用的是div,只能保存纯文本格式,换行符不会被识别,解决方法是把换行符替换为br标签进行替换
- 直接复制过来的文本
- 手动换行之后的文本
解决方法
- 将文本中的换行符替换为br标签,然后插入到dom中
const text = this.text.replace(/(\r\n|\n|\r)/g, '<br>');
const div = document.querySelector('.ProseMirror');
div.innerHTML = text;
const titleDiv = document.querySelector('#title')
titleDiv.value = this.title
将功能做成一个chrome插件
最后效果:
也可以使用油猴脚本
(() => {
console.log('hello...world...,I am from ',location.href);
async function getClipboardContents() {
try {
let text = await navigator.clipboard.readText();
console.log('Pasted content: ', text);
text = text.replace(/(\r\n|\n|\r)/g, '<br>')
let div = document.querySelector('.ProseMirror')
div.innerHTML= text
} catch (err) {
console.error('Failed to read clipboard contents: ', err);
}
}
getClipboardContents();
})();
这里复制文本后需要重新打开添加图文页面,页面打开后会自动填充复制的文本