微信读书网页版复制代码

19 min read

微信读书默认复制的代码是不带换行的

使用如下代码在页面中创建文本框获取带换行符的原始代码片段

document.oncopy = function (e) {
    let textarea = document.querySelector("#txa")
    if(textarea){
        document.querySelector(".app_content").removeChild(textarea)
    }
    textarea = document.createElement('textarea')
    e.stopPropagation();
    textarea.style.position = 'fixed'
    textarea.style.zIndex = "9999999"
    textarea.style.top='100px'
    textarea.style.right='100px'
    textarea.style.width='200px'
    textarea.style.height='200px'
    textarea.id = 'txa'
    
    textarea.value = e.target.defaultValue
    document.querySelector(".app_content").appendChild(textarea)
    // textarea.select()
    // console.log(txa.value)
    e.clipboardData.setData('text/html', txa.value)

}

1662083918109.png